views:

992

answers:

3

Hi, I'm trying to use the audioplayer with a slider in order to seek into a track (nothing complicated).

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

It's like it can't read the time i'm giving to it.

For exemple the duration of the track is: 295.784424 seconds i set the currentTime to 55.0s (ie: 54.963878 or 54.963900 or 54.987755, etc... when printed as %f). The "crashes" always happen when the currentTime is 54.987755... and I really don't understand why...

So if you have any idea... ^^

A: 

Have you tried with a different video file? Perhaps it's corrupted.

EightyEight
Yes, i've tried with different files, for each mp3 it crashes for the same values :(May be it needs to be a multiple of a specific number or something else, but i can't find anything now :(
StrAbZ
Totally guessing here, but how about casting the time interval into int and then back to double? This will get rid of fractional values. Maybe that'll help.
EightyEight
Already tried that, it always give me this kind of results :(But according to other persons it's may be an issue with the simulator, so I need to try with a device.
StrAbZ
A: 

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.

StrAbZ
A: 

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];
    }
}  
LeoN