Iphone 音频播放时间显示
我有一个简单的mp3播放通过AVAudioPlayer,我想能够显示多少时间是剩下的Iphone 音频播放时间显示,iphone,objective-c,avaudioplayer,Iphone,Objective C,Avaudioplayer,我有一个简单的mp3播放通过AVAudioPlayer,我想能够显示多少时间是剩下的 我知道答案包括从AVAudioPlayer.currentTime中减去AVAudioPlayer.duration,但我不知道如何实现一个函数,在播放时计算它(我想就像Actionscript中的一个内框)。目前,currentTime是静态的,即零。我会选择NSTimer。安排它在播放媒体时每秒运行一次,这样您就可以在剩余时间内保持UI的更新 // Place this where you start to
我知道答案包括从
AVAudioPlayer.currentTime
中减去AVAudioPlayer.duration
,但我不知道如何实现一个函数,在播放时计算它(我想就像Actionscript中的一个内框)。目前,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];
}
我在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 String
谢谢你,vfn,它看起来是完美的解决方案。我只发现了一个问题:计时器没有启动!啊,是的,只是需要使用
scheduledTimerWithTimeInterval
而不是timerWithTimeInterval
是的,您需要启动它或使用预定方法。现在编辑好了!