我有一个通过AVAudioPlayer播放的简单mp3,我希望能够显示还剩多少时间。
我知道答案包括从AVAudioPlayer.currentTime中减去AVAudioPlayer.duration,但我不知道如何实现一个在播放时计算它的函数(我猜就像ActionScript中的onEnterFrame )。目前currentTime是静态的,即零。
发布于 2010-08-26 09:57:13
我会去读NSTimer。将其安排为在媒体播放期间每秒运行一次,以便您可以根据剩余时间更新您的UI。
// Place this where you start to play
NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTimeLeft)
userInfo:nil
repeats:YES];并创建用于更新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];
}发布于 2019-04-12 20:30:51
我在swift中使用它,它起作用了:
// this for show remain time
let duration = Int((player1?.duration - (player1?.currentTime))!)
let minutes2 = duration/60
let seconds2 = duration - minutes2 * 60
durLabel.text = NSString(format: "%02d:%02d", minutes2,seconds2) as String
//this for current time
let currentTime1 = Int((player1?.currentTime)!)
let minutes = currentTime1/60
let seconds = currentTime1 - minutes * 60
curLabel.text = NSString(format: "%02d:%02d", minutes,seconds) as Stringhttps://stackoverflow.com/questions/3571494
复制相似问题