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 1 commit
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
19 changes: 18 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,27 @@ - (void)doUpdateNowPlayingCenter {
}

MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSTimeInterval elapsedInSeconds = self.currentPlaybackTime;
NSTimeInterval totalInSeconds = [[self.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration] doubleValue];
MPMusicPlaybackState state = self.playbackState;
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),
MPNowPlayingInfoPropertyPlaybackProgress: @(elapsedInSeconds/totalInSeconds),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be a divide by zero risk there, no?

Copy link
Author

@username0x0a username0x0a Feb 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in fact there is none because of how the system behaves, further details here:
http://stackoverflow.com/questions/1449109/why-doesnt-this-crash-arent-i-dividing-by-zero-here

But it definitely is a suspiciously looking operation. I've patched this in the following commit along with a symbol checking for the other properties I've added as these are officially available since SDK 10.0.

There are potentially other places that could cause troubles because of the SDK availability stuff since iOS 7.0 or 9.x so I wouldn't guarantee that this library still works correctly on iOS 5+6 (and maybe even 7+8+9.0) like the Example deployment setting declares.

MPNowPlayingInfoPropertyMediaType: @(MPNowPlayingInfoMediaTypeAudio),
MPNowPlayingInfoPropertyPlaybackRate: @(rate),
}];

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 +423,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