简体   繁体   中英

how to get the remaining time of the song which is played in AVAudioPlayer

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...

NSTimeInterval remaining = audioPlayer.duration - audioPlayer.currentTime;

尝试这个

CGFloat remainingTime =   audioPlayer.duration - audioPlayer.currentTime

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM