AVAudioPlayer currentTime 问题 [英] AVAudioPlayer currentTime issue
问题描述
我正在尝试使用带有滑块的 AVAudioPlayer
来寻找轨道(并不复杂).
I'm trying to use the AVAudioPlayer
with a slider in order to seek into a track (nothing complicated).
但我有一个奇怪的行为......对于 currentTime
的某些值(在 0 和 trackDuration 之间),播放器停止播放曲目,并进入 audioPlayerDidFinishPlaying:successfully:代码>成功到NO.它没有进入
audioPlayerDecodeErrorDidOccur:error:
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.
例如曲目的时长为:295.784424 秒我将 currentTime
设置为 55.0s(即:54.963878 或 54.963900 或 54.987755 等......当打印为 %f 时).当 currentTime
为 54.987755 时,崩溃"总是发生......我真的不明白为什么......
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... ^^
推荐答案
我也很难通过 'AVAudioPlayer setCurrentTime:` 使音频跳过正常工作
I also struggled to get audio skipping working properly with 'AVAudioPlayer setCurrentTime:`
经过大量实验,我发现了一个可以在模拟器和设备上可靠运行的序列:(在 OS3.1+ 上测试)
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];
}
}
这篇关于AVAudioPlayer currentTime 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!