Unable to Implement Forward Action in AVAudioplayer

不羁的心 提交于 2019-12-19 10:08:06

问题


I am using AVAudioplayer to play some audio files. I have some controls like forward and rewind.

Rewind is working properly but forward is not.

- (void)rewind
{
    AVAudioPlayer *player = _rewTimer.userInfo;
    player.currentTime-= SKIP_TIME;
    [_rewTimer invalidate];
    _rewTimer=nil;
    [self updateCurrentTime];
}

- (void)ffwd
{
    AVAudioPlayer *player = _ffwTimer.userInfo;
    player.currentTime-= SKIP_TIME;
    [player setCurrentTime:cTime];
    [_ffwTimer invalidate];
    _ffwTimer=nil;

    [self updateCurrentTime];
}

As soon as I click on forward button, the audio file just begins playing from the start.

I have followed the sample app avTouch from Apple.


回答1:


you might have done the property declaration of AVAudioPlayer *player; in the h file and also synthesised it. Now for forward, just do,

- (void)ffwd
{
    NSTimeInterval *time = [player currentTime];
    time+=SKIP_TIME;
    [player setCurrentTime:time];    

}

do similarly for rewind+

OR

 - (void)ffwd{
    NSTimeInterval time = avPlayer.currentTime;
 time+=SKIP_TIME;
    if (time > avPLayer.duration)
    {
       //nothing to do

    }
    else
        [player setCurrentTime:time];
    }

this one seems the better way, also for rewind compare to 0 instead of duration




回答2:


The simplest explanation is that cTime ends up either greater than the total time of the audio file or it ends up negative somehow. Both will cause the player instance to set its currentTime property to zero.

As an aside, I think having the player in a timer's userInfo dictionary is fragile. Unless it just absolutely must be embedded in the timer, I would move it to a property of the class so that all the methods can easily access it. The timer can still call the methods but you won't run the risk of the player being lost while swapping from timer to timer.



来源:https://stackoverflow.com/questions/2006757/unable-to-implement-forward-action-in-avaudioplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!