Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Fixed & enhanced Now playing info #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion GVMusicPlayerController/GVMusicPlayerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -408,6 +427,8 @@ - (void)setPlaybackState:(MPMusicPlaybackState)playbackState {
MPMusicPlaybackState oldState = _playbackState;
_playbackState = playbackState;

[self doUpdateNowPlayingCenter];

for (id <GVMusicPlayerControllerDelegate> delegate in self.delegates) {
if ([delegate respondsToSelector:@selector(musicPlayer:playbackStateChanged:previousPlaybackState:)]) {
[delegate musicPlayer:self playbackStateChanged:_playbackState previousPlaybackState:oldState];
Expand Down