From 333cce81c26dbbeb347098042439596fda401298 Mon Sep 17 00:00:00 2001 From: Michal Zelinka Date: Tue, 21 Feb 2017 10:39:40 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Fixed=20&=20enhanced=20Now=20playing=20info?= =?UTF-8?q?=20=E2=80=93=20added=20a=20few=20other=20properties=20so=20the?= =?UTF-8?q?=20information=20is=20more=20complete=20and=20trackable=20by=20?= =?UTF-8?q?remote=20clients=20=E2=80=93=E2=80=93=20this=20fixes=20f.e.=20P?= =?UTF-8?q?ebble=20watch=20Music=20app=20so=20it=20can=20properly=20indica?= =?UTF-8?q?te=20Play/Pause=20and=20playback=20position=20=E2=80=93=C2=A0se?= =?UTF-8?q?t=20to=20update=20the=20Now=20playing=20info=20a=20bit=20more?= =?UTF-8?q?=20often?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GVMusicPlayerController.m | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/GVMusicPlayerController/GVMusicPlayerController.m b/GVMusicPlayerController/GVMusicPlayerController.m index 0bc073a..76621ac 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,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), + 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) { @@ -408,6 +423,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]; From f97d4942328abdae77087268c42e3075714b7f32 Mon Sep 17 00:00:00 2001 From: Michal Zelinka Date: Tue, 21 Feb 2017 21:45:42 +0100 Subject: [PATCH 2/2] Safety-check around possible zero division line & added symbols check for newly added properties --- GVMusicPlayerController/GVMusicPlayerController.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/GVMusicPlayerController/GVMusicPlayerController.m b/GVMusicPlayerController/GVMusicPlayerController.m index 76621ac..ceec7f1 100755 --- a/GVMusicPlayerController/GVMusicPlayerController.m +++ b/GVMusicPlayerController/GVMusicPlayerController.m @@ -388,6 +388,7 @@ - (void)doUpdateNowPlayingCenter { 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:@{ @@ -396,11 +397,14 @@ - (void)doUpdateNowPlayingCenter { MPMediaItemPropertyAlbumTitle: [self.nowPlayingItem valueForProperty:MPMediaItemPropertyAlbumTitle] ?: @"", MPMediaItemPropertyPlaybackDuration: @(totalInSeconds), MPNowPlayingInfoPropertyElapsedPlaybackTime: @(elapsedInSeconds), - MPNowPlayingInfoPropertyPlaybackProgress: @(elapsedInSeconds/totalInSeconds), - MPNowPlayingInfoPropertyMediaType: @(MPNowPlayingInfoMediaTypeAudio), 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);