I\'m trying to use the AVAudioPlayer
with a slider in order to seek into a track (nothing complicated).
But I have a weird behavior... for some value of
I've tried with a device, and this is an issue that occurs only with the simulator. All my files are well played on the device, and I can seek easily inside them.
I've tried mp3, aac and wav.
For some reason when I get this error, flag is still set to YES. I've managed to find a workaround by checking currentTime vs. duration and restarting the player immediately if the currentTime isn't 0.0 (the end of the sound). All of my testing is done on the simulator as I do not have a license to test on my phone yet. Hope this helps. See edits for a couple quirks.
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if ([_player currentTime] != [_player duration] && [_player currentTime] != 0.0f) {
[_player play];
return;
}
...
EDIT: Unfortunately, I have still found errors when seeking to the very beginning or very end of a sound (or in a very small increment of either). I've found that if you create special cases that handle those two instances you are generally covered. You should stop the player, set the currentTime to 0.0 and then either start the player again if seeking to the beginning or manually calling the finish delegate if you are seeking to the end (if you implemented it).
If I find a better solution or get more feedback running this on an actual device I'll update.
I also struggled to get audio skipping working properly with 'AVAudioPlayer setCurrentTime:`
After alot of experimentation i've found a sequence that works reliably on the simulator and the device: (tested on OS3.1+)
// Skips to an audio position (in seconds) of the current file on the [AVAudioPlayer* audioPlayer] class instance
// This works correctly for a playing and paused audioPlayer
//
- (void) skipToSeconds:(float)position
{
@synchronized(self)
{
// Negative values skip to start of file
if ( position<0.0f )
position = 0.0f;
// Rounds down to remove sub-second precision
position = (int)position;
// Prevent skipping past end of file
if ( position>=(int)audioPlayer.duration )
{
NSLog( @"Audio: IGNORING skip to <%.02f> (past EOF) of <%.02f> seconds", position, audioPlayer.duration );
return;
}
// See if playback is active prior to skipping
BOOL skipWhilePlaying = audioPlayer.playing;
// Perform skip
NSLog( @"Audio: skip to <%.02f> of <%.02f> seconds", position, audioPlayer.duration );
// NOTE: This stop,set,prepare,(play) sequence produces reliable results on the simulator and device.
[audioPlayer stop];
[audioPlayer setCurrentTime:position];
[audioPlayer prepareToPlay];
// Resume playback if it was active prior to skipping
if ( skipWhilePlaying )
[audioPlayer play];
}
}