Simple Rewind for AVAudioplayer

三世轮回 提交于 2019-12-11 04:39:17

问题


I've got valuechanged event handler like that:

-(void)scrub:(id)sender
{
    if(self.audioPlayer.playing)
    {
        self.audioPlayer.pause;
        self.audioPlayer.currentTime=self.scrubber.value;
        self.audioPlayer.play;
    }
}

Why doesn't rewind happen? what should I add? thanks! Moreover, how to display current time in a label?


回答1:


To set the time, you can do:

           [audioPlayer setTime:self.scrubber.value];

However, you need to verify that that is actually a number. You also need to make sure it has an appropriate time for the file you are using. I don't know what kind of scrubber you are using. If setCurrentTime doesn't work, there is some problem with your scrubber.

If you are using a UISlider, you can do something like this to set the time:

            double value = progressSlider.value;
            double newSeekTime = (value / 100.0) * audioPlayer.duration;

            [audioPlayer setCurrentTime:(int)newSeekTime];

To get the time, you simply do audioPlayer.currentTime. This is an NSTimeInterval, which is just a double. To display the time, you can do:

            NSString *positionString = [NSString stringWithFormat:@"%.2d:%.2d / %.2d:%.2d",
                                                                    (NSInteger)progress/60,
                                                                    (NSInteger)progress%60,
                                                                    (NSInteger)duration/60,
                                                                    (NSInteger)duration%60];
            myLabel.text = positionString;


来源:https://stackoverflow.com/questions/7157533/simple-rewind-for-avaudioplayer

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