AVAudioPlayer Time Display

I have a simple mp3 game through AVAudioPlayer, and I want to be able to display how much time is left.

I know that the answer involves subtracting AVAudioPlayer.duration from AVAudioPlayer.currentTime , but I don’t know how to implement a function that evaluates it during its playback (e.g. onEnterFrame in Actionscript, I think). Currently, currentTime is static, i.e. Zero.

+4
source share
2 answers

I would go for NSTimer. Schedule it to run every second while the media is playing, and you can update your interface with the remaining time.

 // Place this where you start to play NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimeLeft) userInfo:nil repeats:YES]; 

And create a way to update the interface:

 - (void)updateTimeLeft { NSTimeInterval timeLeft = self.player.duration - self.player.currentTime; // update your UI with timeLeft self.timeLeftLabel.text = [NSString stringWithFormat:@"%f seconds left", timeLeft]; } 
+12
source

I use this in quick and it works:

 // 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 
0
source

All Articles