Skip to content

Commit

Permalink
browser version
Browse files Browse the repository at this point in the history
  • Loading branch information
leon committed Oct 1, 2015
1 parent 0328b66 commit d125bd8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 41 deletions.
15 changes: 11 additions & 4 deletions plugin.xml
Expand Up @@ -11,20 +11,27 @@
<engines>
<engine name="cordova" version=">=3.4.0"/>
</engines>

<js-module src="www/NowPlaying.js" name="NowPlaying">
<clobbers target="window.NowPlaying" />
</js-module>

<!-- ios -->
<platform name="ios">
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="NowPlaying">
<param name="ios-package" value="MNowPlaying" />
<param name="ios-package" value="MNowPlaying" />
</feature>
</config-file>
<header-file src="src/ios/MNowPlaying.h" />
<source-file src="src/ios/MNowPlaying.m" />
</platform>

<!-- browser -->
<platform name="browser">
<js-module src="src/browser/MNowPlaying.js" name="MNowPlaying">
<clobbers target="window.NowPlaying" />
</js-module>
</platform>

</plugin>
33 changes: 33 additions & 0 deletions 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;
87 changes: 50 additions & 37 deletions src/ios/MNowPlaying.m
Expand Up @@ -2,8 +2,6 @@

@implementation MNowPlaying

static MNowPlaying *remoteControls = nil;

- (void)pluginInitialize
{
NSLog(@"NowPlaying plugin init.");
Expand All @@ -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"]];
}
Expand All @@ -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];
Expand All @@ -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
@end

0 comments on commit d125bd8

Please sign in to comment.