From d125bd8b9fcc3cadd86f8c18727b31820644c83b Mon Sep 17 00:00:00 2001 From: Leon Radley Date: Thu, 1 Oct 2015 10:29:59 +0200 Subject: [PATCH] browser version --- plugin.xml | 15 +++++-- src/browser/MNowPlaying.js | 33 +++++++++++++++ src/ios/MNowPlaying.m | 87 ++++++++++++++++++++++---------------- 3 files changed, 94 insertions(+), 41 deletions(-) create mode 100644 src/browser/MNowPlaying.js diff --git a/plugin.xml b/plugin.xml index 578ec0c..886efee 100644 --- a/plugin.xml +++ b/plugin.xml @@ -11,20 +11,27 @@ - + - + - + - + + + + + + + + diff --git a/src/browser/MNowPlaying.js b/src/browser/MNowPlaying.js new file mode 100644 index 0000000..7070d4e --- /dev/null +++ b/src/browser/MNowPlaying.js @@ -0,0 +1,33 @@ +/** + * All keys from https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPNowPlayingInfoCenter_Class/index.html kan be specified + * // Only the keys passed in will be updated, and only if they are not null + * { + * albumTitle: "The Album Title", + * trackCount: 10, + * trackNumber: 1, + * artist: "The Artist", + * composer: "The Composer", + * discCount: 1, + * discNumber: 1, + * genre: "The Genre", + * persistentID: 12345, + * playbackDuration: 500, + * title: "The Title", + * elapsedPlaybackTime: 30, + * playbackRate: 1, + * playbackQueueIndex: 1, + * playbackQueueCount: 5, + * chapterNumber: 1, + * chapterCount: 2, + * artwork: "http://www.domain.com/image.png" / "https://www.domain.com/image.png" / "/file.png (relative to NSDocumentDirectory)" + * } + * + */ + +var NowPlaying = { + set: function setNowPlaying(dict, success, fail) { + console.log('NowPlaying set: ', JSON.stringify(dict)); + } +}; + +module.exports = NowPlaying; diff --git a/src/ios/MNowPlaying.m b/src/ios/MNowPlaying.m index 0a922f8..2bd8df7 100644 --- a/src/ios/MNowPlaying.m +++ b/src/ios/MNowPlaying.m @@ -2,8 +2,6 @@ @implementation MNowPlaying -static MNowPlaying *remoteControls = nil; - - (void)pluginInitialize { NSLog(@"NowPlaying plugin init."); @@ -14,92 +12,105 @@ - (void)pluginInitialize */ - (void)setNowPlaying:(CDVInvokedUrlCommand*)command { - // Parse json data - NSString* jsonStr = [command.arguments objectAtIndex:0]; - NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; - NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]; - + MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter]; + + // If now arguments are passed clear out nowPlaying and return + if ([command.arguments count] == 0) { + center.nowPlayingInfo = nil; + return; + } + + // Parse json data and check that data is available + NSString *jsonStr = [command.arguments objectAtIndex:0]; + NSDictionary *jsonObject; + if (jsonStr != nil || ![jsonStr isEqual: @""]) { + NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; + jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]; + } + // If the json object could not be parsed we exit early if (jsonObject == nil) { NSLog(@"Could not parse now playing json object"); return; } - - // - NSMutableDictionary *mediaDict = [[NSMutableDictionary alloc] init]; - + + // Create media dictionary from existing keys or create a new one, this way we can update single attributes if we want to + NSMutableDictionary *mediaDict = (center.nowPlayingInfo != nil) ? [[NSMutableDictionary alloc] initWithDictionary: center.nowPlayingInfo] : [NSMutableDictionary dictionary]; + if ([jsonObject objectForKey: @"albumTitle"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"albumTitle"] forKey:MPMediaItemPropertyAlbumTitle]; } - + if ([jsonObject objectForKey: @"trackCount"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"trackCount"] forKey:MPMediaItemPropertyAlbumTrackCount]; } - + if ([jsonObject objectForKey: @"trackNumber"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"trackNumber"] forKey:MPMediaItemPropertyAlbumTrackNumber]; } - + if ([jsonObject objectForKey: @"artist"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"artist"] forKey:MPMediaItemPropertyArtist]; } - + if ([jsonObject objectForKey: @"composer"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"composer"] forKey:MPMediaItemPropertyComposer]; } - + if ([jsonObject objectForKey: @"discCount"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"discCount"] forKey:MPMediaItemPropertyDiscCount]; } - + if ([jsonObject objectForKey: @"discNumber"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"discNumber"] forKey:MPMediaItemPropertyDiscNumber]; } - + if ([jsonObject objectForKey: @"genre"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"genre"] forKey:MPMediaItemPropertyGenre]; } - + if ([jsonObject objectForKey: @"persistentID"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"persistentID"] forKey:MPMediaItemPropertyPersistentID]; } - + if ([jsonObject objectForKey: @"playbackDuration"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"playbackDuration"] forKey:MPMediaItemPropertyPlaybackDuration]; } - + if ([jsonObject objectForKey: @"title"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"title"] forKey:MPMediaItemPropertyTitle]; } - + if ([jsonObject objectForKey: @"elapsedPlaybackTime"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"elapsedPlaybackTime"] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; } - + if ([jsonObject objectForKey: @"playbackRate"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"playbackRate"] forKey:MPNowPlayingInfoPropertyPlaybackRate]; + } else { + // In iOS Simulator, always include the MPNowPlayingInfoPropertyPlaybackRate key in your nowPlayingInfo dictionary + [mediaDict setValue:[NSNumber numberWithDouble:1] forKey:MPNowPlayingInfoPropertyPlaybackRate]; } - + if ([jsonObject objectForKey: @"playbackQueueIndex"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"playbackQueueIndex"] forKey:MPNowPlayingInfoPropertyPlaybackQueueIndex]; } - + if ([jsonObject objectForKey: @"playbackQueueCount"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"playbackQueueCount"] forKey:MPNowPlayingInfoPropertyPlaybackQueueCount]; } - + if ([jsonObject objectForKey: @"chapterNumber"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"chapterNumber"] forKey:MPNowPlayingInfoPropertyChapterNumber]; } - + if ([jsonObject objectForKey: @"chapterCount"] != nil) { [mediaDict setValue:[jsonObject objectForKey: @"chapterCount"] forKey:MPNowPlayingInfoPropertyChapterCount]; } - MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter]; center.nowPlayingInfo = mediaDict; - - // Custom handling of artwork + + // Custom handling of artwork in another thread, will be loaded async if ([jsonObject objectForKey: @"artwork"] != nil) { [self setNowPlayingArtwork: [jsonObject objectForKey: @"artwork"]]; } @@ -112,16 +123,15 @@ - (void)setNowPlayingArtwork:(NSString*)url { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ UIImage *image = nil; - // check whether cover path is present + // check whether artwork path is present if (![url isEqual: @""]) { - // cover is remote file + // artwork is url download from the interwebs if ([url hasPrefix: @"http://"] || [url hasPrefix: @"https://"]) { NSURL *imageURL = [NSURL URLWithString:url]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; image = [UIImage imageWithData:imageData]; - } - // cover is local file - else { + } else { + // artwork is local. so create it from a UIImage NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *fullPath = [NSString stringWithFormat:@"%@%@", basePath, url]; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath]; @@ -140,13 +150,16 @@ - (void)setNowPlayingArtwork:(NSString*)url CGImageRef cgref = [image CGImage]; CIImage *cim = [image CIImage]; if (cim != nil || cgref != NULL) { + // Callback to main queue to set nowPlayingInfo dispatch_async(dispatch_get_main_queue(), ^{ MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter]; MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage: image]; - center.nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys: artwork, MPMediaItemPropertyArtwork, nil]; + NSMutableDictionary *mediaDict = (center.nowPlayingInfo != nil) ? [[NSMutableDictionary alloc] initWithDictionary: center.nowPlayingInfo] : [NSMutableDictionary dictionary]; + [mediaDict setValue:artwork forKey:MPMediaItemPropertyArtwork]; + center.nowPlayingInfo = mediaDict; }); } }); } -@end \ No newline at end of file +@end