AVAudioPlayer currentTime问题

Str*_*AbZ 1 iphone audio-player

我正在尝试使用AVAudioPlayer滑块以寻找轨道(没有任何复杂的).

但我有一个奇怪的行为......对于某些值currentTime(在0和trackDuration之间),玩家停止播放音轨,并audioPlayerDidFinishPlaying:successfully: 成功进入NO.它没有进入audioPlayerDecodeErrorDidOccur:error:

这就像它无法读取我给它的时间.

例如,轨道的持续时间为:295.784424秒我将其设置currentTime为55.0s(即:54.963878或54.963900或54.987755等,当打印为%f时)."崩溃"总是发生在currentTime54.987755 ......我真的不明白为什么......

所以如果你有任何想法...... ^^

小智 5

我还努力通过'AVAudioPlayer setCurrentTime:`让音频跳过正常工作

经过大量的实验,我发现了一个在模拟器和设备上可靠运行的序列:(在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];
    }
}  
Run Code Online (Sandbox Code Playgroud)