gpt4 book ai didi

iphone - AVAudioPlayer 当前时间问题

转载 作者:行者123 更新时间:2023-12-02 15:42:56 25 4
gpt4 key购买 nike

我正在尝试将 AVAudioPlayer 与 slider 一起使用,以便进入轨道(没有什么复杂的)。

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

就好像它无法读取我给它的时间。

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

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

最佳答案

我也很难让音频跳过与“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];
}
}

关于iphone - AVAudioPlayer 当前时间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/882753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com