How to set the music playback slider in the music player

I created a music player with previous and next functionality in my application. I want to implement a song playback slider. how to do it?

+4
source share
2 answers

Use the UISlider parameter UISlider that the value of maxValue matches the current playback time (in seconds) and minValue matches 0.

Assuming you are using MPMusicPlayerController , use currentPlaybackTime to get the current playing time of the track and use this value to update the slider every second

 slider.value = musicPlayerController.currentPlaybackTime; slider.minimumValue = 0; slider.maximumValue = [musicPlayerController.nowPlayingItem valueForProperty:@"MPMediaItemPropertyPlaybackDuration"]; 
+8
source
 - (IBAction)slide:(id)sender{ [musicPlayer setCurrentPlaybackTime: [slider value]]; } - (void)actualizaSlider{ slider.value = musicPlayer.currentPlaybackTime; slider.minimumValue = 0; NSNumber *duration = [self.musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration]; float totalTime = [duration floatValue]; slider.maximumValue = totalTime; 
+8
source

Source: https://habr.com/ru/post/1396169/