我有一个通过 AVAudioPlayer 播放的简单 mp3,我希望能够显示剩余时间。
我知道答案包括从 AVAudioPlayer.currentTime
中减去 AVAudioPlayer.duration
但我不知道如何实现一个在播放时计算它的函数(比如我猜是 Actionscript 中的 onEnterFrame)。目前 currentTime
是静态的,即零。
最佳答案
我会选择 NSTimer。安排它在播放媒体时每秒运行一次,这样您就可以在剩余时间更新您的 UI。
// Place this where you start to play
NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTimeLeft)
userInfo:nil
repeats:YES];
并创建更新用户界面的方法:
- (void)updateTimeLeft {
NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;
// update your UI with timeLeft
self.timeLeftLabel.text = [NSString stringWithFormat:@"%f seconds left", timeLeft];
}
关于iphone - AVAudioPlayer时间显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3571494/