Я использую следующий код для воспроизведения звука, когда пользователь касается любого места на экране, но всякий раз, когда я проверяю его, звук не воспроизводится, а также, когда я использую тот же код на кнопке, он воспроизводится при втором нажатии кнопки, а не по первому клику ??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
if (CGRectContainsPoint(CGRectMake(320,480,0,0),point));//CGRectMake(5, 5, 40, 130), point))
{
AVAudioPlayer *player;
if(!player){
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/try.wav"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;
//Initialize our player pointing to the path to our resource
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
if( err ){
//bail!
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
player.delegate = self;
[player play];
}
}
}
}