Reputation: 23
I am new to iphone. I am working on audio player. I have to show the current time and remaining time of the song in audioplayer. In video player it will gets as default but in audioplayer it is not getting so that i write a logic for getting current time of the song. The code below is for that
int minutes = (int)audioPlayer.currentTime / 60;
int seconds = (int)audioPlayer.currentTime % 60;
startDurationLabel.text = [NSString stringWithFormat:@"%d:%02d",minutes,seconds];
here audioPlayer is instance of AVAudioPlayer and startDurationLabel is the label for display the current time of the song.
But I am struggling to get this logic to work and show the remaining time of the song
If any body know this please help me...
Upvotes: 2
Views: 3352
Reputation: 636
Try this -
NSString *strTimeLeft = [self getTimeFromTimeInterval:CMTimeGetSeconds(_player.currentItem.duration) - CMTimeGetSeconds(_player.currentTime)];
Add this method in your class
- (NSString*)getTimeFromTimeInterval:(NSTimeInterval)timeInterval
{
NSInteger interval = (NSInteger)timeInterval;
NSInteger seconds = interval%60;
NSInteger minutes = (interval/ 60)%60;
//NSInteger hr = (interval/3600)%60;
NSString *strTime = [NSString stringWithFormat:@"%02d:%02d",minutes,seconds];
return strTime;
}
Upvotes: 0
Reputation: 2494
NSTimeInterval remaining = audioPlayer.duration - audioPlayer.currentTime;
Upvotes: 2
Reputation: 1398
Try This
CGFloat remainingTime = audioPlayer.duration - audioPlayer.currentTime
Upvotes: 2