How to set music playback slider in music player

2019-06-23 18:28发布

i created a music player with previous and next functionality in my app.i want to impliment song playback slider. how to do this?

2条回答
够拽才男人
2楼-- · 2019-06-23 19:00
- (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;
查看更多
Anthone
3楼-- · 2019-06-23 19:13

Use a UISlider setting the maxValue to the current playing song duration (in seconds) and the minValue to 0.

Assuming that you're using an MPMusicPlayerController use the currentPlaybackTime to get the current time of the playing track and use that value to update the slider each second

slider.value = musicPlayerController.currentPlaybackTime;
slider.minimumValue = 0;
slider.maximumValue = [musicPlayerController.nowPlayingItem valueForProperty:@"MPMediaItemPropertyPlaybackDuration"];
查看更多
登录 后发表回答