问题描述
我有一个简单的mp3通过AVAudioPlayer播放,我希望能够显示剩余的时间。
I have a simple mp3 playing through AVAudioPlayer and I want to be able to display how much time is left.
我知道答案包括从 AVAudioPlayer.currentTime $减去
AVAudioPlayer.duration
c $ c>但是我不知道如何实现一个在播放时计算它的函数(比如我想在的Actionscript中的onEnterFrame)。目前 currentTime
是静态的,即零。
I know the answer includes subtracting AVAudioPlayer.duration
from AVAudioPlayer.currentTime
but I don't know how to implement a function which calculates it while it's playing (like an onEnterFrame in Actionscript I guess). At present currentTime
is static, i.e. zero.
推荐答案
我会去对于NSTimer。安排它在媒体播放时每秒运行一次,这样你就可以用剩下的时间更新你的UI。
I would go for an NSTimer. Schedule it to run every second while the media is played and so you can keep your UI updated with the time left.
// Place this where you start to play
NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTimeLeft)
userInfo:nil
repeats:YES];
并创建更新UI的方法:
And create the method to update you UI:
- (void)updateTimeLeft {
NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;
// update your UI with timeLeft
self.timeLeftLabel.text = [NSString stringWithFormat:@"%f seconds left", timeLeft];
}
这篇关于AVAudioPlayer时间显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!