我还努力让音频跳过与“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];
}
}