Rajesh Narkedimilli
Rajesh Narkedimilli

Reputation: 23

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

Upvotes: 2

Views: 3352

Answers (3)

Pooja Patel
Pooja Patel

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

Svein Halvor Halvorsen
Svein Halvor Halvorsen

Reputation: 2494

NSTimeInterval remaining = audioPlayer.duration - audioPlayer.currentTime;

Upvotes: 2

Muhammed Ayaz
Muhammed Ayaz

Reputation: 1398

Try This

CGFloat remainingTime =   audioPlayer.duration - audioPlayer.currentTime

Upvotes: 2

Related Questions