diff --git a/GVMusicPlayerController/GVMusicPlayerController.m b/GVMusicPlayerController/GVMusicPlayerController.m index 0bc073a..ceec7f1 100755 --- a/GVMusicPlayerController/GVMusicPlayerController.m +++ b/GVMusicPlayerController/GVMusicPlayerController.m @@ -245,6 +245,7 @@ - (NSTimeInterval)currentPlaybackTime { - (void)setCurrentPlaybackTime:(NSTimeInterval)currentPlaybackTime { CMTime t = CMTimeMake(currentPlaybackTime, 1); [self.player seekToTime:t]; + [self performSelector:@selector(doUpdateNowPlayingCenter) withObject:nil afterDelay:0.5]; } - (float)currentPlaybackRate { @@ -384,13 +385,31 @@ - (void)doUpdateNowPlayingCenter { } MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter]; + NSTimeInterval elapsedInSeconds = self.currentPlaybackTime; + NSTimeInterval totalInSeconds = [[self.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration] doubleValue]; + MPMusicPlaybackState state = self.playbackState; + float playbackProgress = (totalInSeconds > 0) ? elapsedInSeconds/totalInSeconds : 0; + float rate = (state == MPMusicPlaybackStatePlaying) ? self.currentPlaybackRate : 0; + NSMutableDictionary *songInfo = [NSMutableDictionary dictionaryWithDictionary:@{ MPMediaItemPropertyArtist: [self.nowPlayingItem valueForProperty:MPMediaItemPropertyArtist] ?: @"", MPMediaItemPropertyTitle: [self.nowPlayingItem valueForProperty:MPMediaItemPropertyTitle] ?: @"", MPMediaItemPropertyAlbumTitle: [self.nowPlayingItem valueForProperty:MPMediaItemPropertyAlbumTitle] ?: @"", - MPMediaItemPropertyPlaybackDuration: [self.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration] ?: @0 + MPMediaItemPropertyPlaybackDuration: @(totalInSeconds), + MPNowPlayingInfoPropertyElapsedPlaybackTime: @(elapsedInSeconds), + MPNowPlayingInfoPropertyPlaybackRate: @(rate), }]; + if (&MPNowPlayingInfoPropertyPlaybackProgress != NULL) + songInfo[MPNowPlayingInfoPropertyPlaybackProgress] = @(playbackProgress); + if (&MPNowPlayingInfoPropertyMediaType != NULL) + songInfo[MPNowPlayingInfoPropertyMediaType] = @(MPNowPlayingInfoMediaTypeAudio); + + if (_shuffleMode == MPMusicShuffleModeOff) { + songInfo[MPNowPlayingInfoPropertyPlaybackQueueIndex] = @(self.indexOfNowPlayingItem); + songInfo[MPNowPlayingInfoPropertyPlaybackQueueCount] = @(_queue.count); + } + // Add the artwork if it exists MPMediaItemArtwork *artwork = [self.nowPlayingItem valueForProperty:MPMediaItemPropertyArtwork]; if (artwork) { @@ -408,6 +427,8 @@ - (void)setPlaybackState:(MPMusicPlaybackState)playbackState { MPMusicPlaybackState oldState = _playbackState; _playbackState = playbackState; + [self doUpdateNowPlayingCenter]; + for (id delegate in self.delegates) { if ([delegate respondsToSelector:@selector(musicPlayer:playbackStateChanged:previousPlaybackState:)]) { [delegate musicPlayer:self playbackStateChanged:_playbackState previousPlaybackState:oldState];