AVAudioPlayer currentTime issue

前端 未结 3 1174
甜味超标
甜味超标 2020-12-21 18:48

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

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 19:18

    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.

提交回复
热议问题