Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

status ios11 no reload same image #118

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
73 changes: 51 additions & 22 deletions src/ios/MusicControls.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,52 @@ - (void) create: (CDVInvokedUrlCommand *) command {
if (!NSClassFromString(@"MPNowPlayingInfoCenter")) {
return;
}
MPNowPlayingInfoCenter * nowPlayingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
NSDictionary * nowPlayingInfo = nowPlayingInfoCenter.nowPlayingInfo;
NSMutableDictionary * updatedNowPlayingInfo = [NSMutableDictionary dictionaryWithDictionary:nowPlayingInfo];

[self.commandDelegate runInBackground:^{
MPNowPlayingInfoCenter * nowPlayingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
NSDictionary * nowPlayingInfo = nowPlayingInfoCenter.nowPlayingInfo;
NSMutableDictionary * updatedNowPlayingInfo = [NSMutableDictionary dictionaryWithDictionary:nowPlayingInfo];

MPMediaItemArtwork * mediaItemArtwork = [self createCoverArtwork:[musicControlsInfo cover]];
NSNumber * duration = [NSNumber numberWithInt:[musicControlsInfo duration]];
NSNumber * elapsed = [NSNumber numberWithInt:[musicControlsInfo elapsed]];
NSNumber * playbackRate = [NSNumber numberWithBool:[musicControlsInfo isPlaying]];

if (mediaItemArtwork != nil) {
[updatedNowPlayingInfo setObject:mediaItemArtwork forKey:MPMediaItemPropertyArtwork];
NSNumber * duration = [NSNumber numberWithInt:[musicControlsInfo duration]];
NSNumber * elapsed = [NSNumber numberWithInt:[musicControlsInfo elapsed]];
NSNumber * playbackRate = [NSNumber numberWithBool:[musicControlsInfo isPlaying]];


[updatedNowPlayingInfo setObject:[musicControlsInfo artist] forKey:MPMediaItemPropertyArtist];
[updatedNowPlayingInfo setObject:[musicControlsInfo track] forKey:MPMediaItemPropertyTitle];
[updatedNowPlayingInfo setObject:[musicControlsInfo album] forKey:MPMediaItemPropertyAlbumTitle];
[updatedNowPlayingInfo setObject:duration forKey:MPMediaItemPropertyPlaybackDuration];
[updatedNowPlayingInfo setObject:elapsed forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[updatedNowPlayingInfo setObject:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];

if (@available(iOS 11.0, *)) {
if ([musicControlsInfo isPlaying]){
[nowPlayingInfoCenter setPlaybackState:MPNowPlayingPlaybackStatePlaying];

}else{
[nowPlayingInfoCenter setPlaybackState:MPNowPlayingPlaybackStatePaused];

}
}

[nowPlayingInfoCenter setNowPlayingInfo:updatedNowPlayingInfo];

[self.commandDelegate runInBackground:^{

[updatedNowPlayingInfo setObject:[musicControlsInfo artist] forKey:MPMediaItemPropertyArtist];
[updatedNowPlayingInfo setObject:[musicControlsInfo track] forKey:MPMediaItemPropertyTitle];
[updatedNowPlayingInfo setObject:[musicControlsInfo album] forKey:MPMediaItemPropertyAlbumTitle];
[updatedNowPlayingInfo setObject:duration forKey:MPMediaItemPropertyPlaybackDuration];
[updatedNowPlayingInfo setObject:elapsed forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[updatedNowPlayingInfo setObject:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];
MPMediaItemArtwork * mediaItemArtwork = [self createCoverArtwork:[musicControlsInfo cover]];

if (mediaItemArtwork != nil ) {
UIImage *newImage = [[updatedNowPlayingInfo objectForKey:@"artwork"] imageWithSize:CGSizeMake(1, 1)];
UIImage *oldImage = [mediaItemArtwork imageWithSize:CGSizeMake(1, 1)];
NSData *data1 = UIImagePNGRepresentation(newImage);
NSData *data2 = UIImagePNGRepresentation(oldImage);
if ([data1 isEqual:data2] == NO){
[updatedNowPlayingInfo setObject:mediaItemArtwork forKey:MPMediaItemPropertyArtwork];
[nowPlayingInfoCenter setNowPlayingInfo:updatedNowPlayingInfo];
}
}

nowPlayingInfoCenter.nowPlayingInfo = updatedNowPlayingInfo;


}];

[self registerMusicControlsEventListener];
}

Expand All @@ -63,7 +84,15 @@ - (void) updateIsPlaying: (CDVInvokedUrlCommand *) command {

MPNowPlayingInfoCenter * nowPlayingCenter = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary * updatedNowPlayingInfo = [NSMutableDictionary dictionaryWithDictionary:nowPlayingCenter.nowPlayingInfo];

if (@available(iOS 11.0, *)) {
if ([musicControlsInfo isPlaying]){
[nowPlayingCenter setPlaybackState:MPNowPlayingPlaybackStatePlaying];

}else{
[nowPlayingCenter setPlaybackState:MPNowPlayingPlaybackStatePaused];

}
}
[updatedNowPlayingInfo setObject:elapsed forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[updatedNowPlayingInfo setObject:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];
nowPlayingCenter.nowPlayingInfo = updatedNowPlayingInfo;
Expand Down Expand Up @@ -193,7 +222,7 @@ - (void) handleMusicControlsNotification: (NSNotification *) notification {

if (receivedEvent.type == UIEventTypeRemoteControl) {
NSString * action;

switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
action = @"music-controls-toggle-play-pause";
Expand Down