AVAudioPlayer 当前时间问题

发布于 2024-07-20 12:42:27 字数 524 浏览 11 评论 0原文

我正在尝试使用带有滑块的 AVAudioPlayer 来寻找轨道(没什么复杂的)。

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

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

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

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

I'm trying to use the AVAudioPlayer 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... ^^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

腹黑女流氓 2024-07-27 12:42:28

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

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];
    }
}  
梦醒时光 2024-07-27 12:42:28

我已经尝试使用设备,这是一个仅在模拟器上出现的问题。
我的所有文件都可以在设备上正常播放,并且我可以在其中轻松查找。

我尝试过 mp3、aac 和 wav。

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.

白日梦 2024-07-27 12:42:28

由于某种原因,当我收到此错误时,标志仍设置为 YES。 我设法通过检查 currentTime 与持续时间并在 currentTime 不是 0.0(声音结束)时立即重新启动播放器来找到解决方法。 我的所有测试都是在模拟器上完成的,因为我还没有在手机上进行测试的许可证。 希望这可以帮助。 请参阅一些怪癖的编辑。

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if ([_player currentTime] != [_player duration] && [_player currentTime] != 0.0f) {
        [_player play];
        return;
    }
...

编辑:不幸的是,当寻找声音的开头或结尾(或两者的很小增量)时,我仍然发现错误。 我发现,如果您创建处理这两个实例的特殊情况,那么您通常会受到保护。 您应该停止播放器,将 currentTime 设置为 0.0,然后如果要查找开头则再次启动播放器,或者如果您要查找结尾(如果您实现了它),则手动调用完成委托。

如果我找到更好的解决方案或在实际设备上运行此程序获得更多反馈,我将进行更新。

For some reason when I get this error, flag is still set to YES. I've managed to find a workaround by checking currentTime vs. duration and restarting the player immediately if the currentTime isn't 0.0 (the end of the sound). All of my testing is done on the simulator as I do not have a license to test on my phone yet. Hope this helps. See edits for a couple quirks.

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if ([_player currentTime] != [_player duration] && [_player currentTime] != 0.0f) {
        [_player play];
        return;
    }
...

EDIT: Unfortunately, I have still found errors when seeking to the very beginning or very end of a sound (or in a very small increment of either). I've found that if you create special cases that handle those two instances you are generally covered. You should stop the player, set the currentTime to 0.0 and then either start the player again if seeking to the beginning or manually calling the finish delegate if you are seeking to the end (if you implemented it).

If I find a better solution or get more feedback running this on an actual device I'll update.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文