Привет, у меня есть приложение, в котором нужно удерживать кнопку и нажать на телефон, и он издает звук, вот код:
#import "AirInstrumentViewController.h"
@implementation AirInstrumentViewController
@synthesize audiOfA;
- (void)didReceiveMemoryWarning
{
/ / Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
} else
NSLog([error description]);
}
- (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];
const double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults > 0.55) {
NSLog(@"Mic blow detected");
if (aIsBeingTouched == YES) {
NSString *musicString = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"aifc"];
audiOfA = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:NULL];
[audiOfA play];
} }else {
[audiOfA stop];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(aImage.frame, touchLocation)) {
aImage.image = [UIImage imageNamed:@"active.png"];
aIsBeingTouched = YES;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[audiOfA stop];
aImage.image = [UIImage imageNamed:@"a.png"];
aIsBeingTouched = NO;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
Я надеюсь, вы понимаете, что я пытаюсьделать.Здесь: if (lowPassResults> 0.55) {NSLog (@ "Удар микрофона обнаружен");
if (aIsBeingTouched == YES) {
NSString *musicString = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"aifc"];
audiOfA = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:NULL];
[audiOfA play];
} }else {
[audiOfA stop];
}
}
Звук дольше продолжается, а затем останавливается.То же самое для прикосновений закончилось.И если я ударю, не удерживая изображение, когда я, наконец, удерживаю изображение, оно воспроизводится, даже если я не сдуваю, потому что я ударил, когда оно не удерживалось, и оно могло записать это или ЧТО-ТО!Я не знаю, как решить все эти проблемы, пожалуйста, помогите!