Current AVAudioPlayer Issue

I am trying to use AVAudioPlayer with a slider to search for a track (nothing complicated).

But I have strange behavior ... for some currentTime value (between 0 and trackDuration), the player stops playing the track and goes to audioPlayerDidFinishPlaying:successfully: with success to NO. And it did not go into audioPlayerDecodeErrorDidOccur:error:

As if he could not read the time that I give him.

For example, track duration: 295.784424 seconds I set currentTime to 55.0s (i.e.: 54.963878 or 54.963900 or 54.987755, etc ... when printing as% f). "Accidents" always occur when currentTime is 54.987755 ... and I really don't understand why ...

So, if you have an idea ... ^^

+1
iphone audio player
May 19, '09 at 13:28
source share
3 answers

I also tried my best to make the audio skip work correctly using "AVAudioPlayer setCurrentTime:

After many experiments, I found a sequence that works reliably on the simulator and 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]; } } 
+5
May 26 '10 at 20:06
source share

I tried with the device, and this is a problem that occurs only with the simulator. All my files play well on the device, and I can easily search them inside.

I tried mp3, aac and wav.

+1
May 25 '09 at 14:52
source share

For some reason, when I get this error, the flag is still set to YES. I managed to find a workaround by checking currentTime vs. duration and restarting the player immediately if currentTime is not 0.0 (end of sound). All my tests are conducted on a simulator, since I do not have a license to test on my phone. Hope this helps. See Edits for a few quirks.

 -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { if ([_player currentTime] != [_player duration] && [_player currentTime] != 0.0f) { [_player play]; return; } ... 

EDIT: Unfortunately, I still found errors finding the very beginning or the very end of the sound (or in a very small increment). I found that if you create special cases that handle these two instances, you are usually covered. You must stop the player, set the current time to 0.0, and then start the player again if you want to start or manually call the finish delegate if you want to finish (if you implemented it).

If I find a better solution or get more feedback running on a real device, I will update.

+1
Jul 07 2018-11-11T00:
source share



All Articles