- (IBAction)triggerSound {
if (player == nil) {
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
} else if ([player isPlaying]) {
[player stop];
player.currentTime = 0;
}
if (![player play]) {
NSLog(@"fail");
}
}
Where player is AVAudioPlayer and this action is linked with a UIButton. The sound can be played, and can be played again after the sound finish, but when I press the button again during the playback, the message “fail” is logged and the sound cannot be played, without any exception.
I and using iPad with iOS 4.2.1.
I have noticed that the audio stream fails to play after calling stop(which according to the reference undoes what prepareToPlay and play does).
Instead of calling stop, setting currentTime to player.duration(end of the stream) worked.