Created
February 20, 2013 08:34
-
-
Save thomasbabuj/4993987 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Playlist.h | |
// | |
// Created by James Thompson on 5/31/12. | |
// Copyright (c) 2012 Ligonier Ministries, Inc. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <AVFoundation/AVFoundation.h> | |
#ifdef CORDOVA_FRAMEWORK | |
#import <Cordova/CDVPlugin.h> | |
#else | |
#import "CDVPlugin.h" | |
#endif | |
@interface Playlist : CDVPlugin { | |
NSMutableArray* playlistSlots; | |
NSMutableArray* playlist; | |
AVQueuePlayer* player; | |
} | |
- (void)play; | |
- (void)stop; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Playlist.js | |
* | |
* Created by James Thompson on 5/31/12. | |
* Copyright (c) 2012 Ligonier Ministries, Inc. All rights reserved. | |
* | |
*/ | |
var Playlist = { | |
play: function(success, fail) { | |
return Cordova.exec(success, fail, "Playlist", "play"); | |
}, | |
stop: function(success, fail) { | |
return Cordova.exec(success, fail, "Playlist", "stop"); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Playlist.m | |
// | |
// Created by James Thompson on 5/31/12. | |
// Copyright (c) 2012 Ligonier Ministries, Inc. All rights reserved. | |
// | |
#import "Playlist.h" | |
#define PLAYLIST_API_ENDPOINT @"http://admin.refnet.fm/api/playlist.json" | |
@implementation Playlist | |
- (void)play { | |
if (!playlistSlots || [playlistSlots count] == 0) { | |
[self retrievePlaylistSlots]; | |
} | |
[self cleanupPlaylistSlots]; | |
[self buildPlaylistFromSlots]; | |
[self seekInFirstPlaylistItem]; | |
if (!player) { | |
AVQueuePlayer* player = [[AVQueuePlayer alloc] initWithItems:playlist]; | |
} | |
[player play]; | |
[super writeJavascript:[[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1] toSuccessCallbackString:callbackId]]; | |
} | |
- (void)stop { | |
[player pause]; | |
[super writeJavascript:[[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1] toSuccessCallbackString:callbackId]]; | |
} | |
- (void)retrievePlaylistSlots { | |
NSURL* playlistURL = [NSURL URLWithString:PLAYLIST_API_ENDPOINT]; | |
NSString* playlistJSON = [NSString stringWithContentsOfURL:playlistURL]; | |
NSData* playlistData = [playlistJSON dataUsingEncoding:NSUTF8StringEncoding]; | |
NSError* jsonError = nil; | |
NSDictionary *playlistDict = [NSJSONSerialization JSONObjectWithData:playlistData options:nil error:&jsonError]; | |
if (!playlistDict) { | |
NSLog(@"Error parsing JSON: %@", jsonError); | |
} else { | |
NSLog(@"Playlist successfully retrieved."); | |
[playlistSlots addObjectsFromArray:[playlistDict valueForKey:@"collection"]]; | |
} | |
} | |
- (void)cleanupPlaylistSlots { | |
for (NSDictionary* slot in playlistSlots) { | |
NSDate* endTime = [self parseDateFromString:[slot valueForKey:@"ends"]]; | |
NSDate* now = [NSDate date]; | |
NSComparisonResult dateComparison = [now compare:endTime]; | |
if (dateComparison == NSOrderedDescending || dateComparison == NSOrderedSame) { | |
NSLog(@"Excluding slot with id: %@", [slot valueForKey:@"id"]); | |
[playlistSlots removeObjectIdenticalTo:slot]; | |
} | |
} | |
} | |
- (void)buildPlaylistFromSlots { | |
if (!playlist) { | |
NSMutableArray* playlist = [[NSMutableArray alloc] init]; | |
} | |
for (NSDictionary* slot in playlistSlots) { | |
[playlist addObject:[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[slot valueForKey:@"url"]]]]; | |
} | |
} | |
- (void)seekInFirstPlaylistItem { | |
NSDictionary* firstSlot = [playlistSlots objectAtIndex:0]; | |
NSString* startTimeStr = [firstSlot valueForKey:@"starts"]; | |
NSDate* startTime = [self parseDateFromString:startTimeStr]; | |
NSDate* now = [NSDate date]; | |
NSTimeInterval since = [now timeIntervalSinceDate:slotStartTime]; | |
CMTime seekTo = CMTimeMake(since, 1); | |
[[playlist objectAtIndex:0] seekToTime:seekTo]; | |
} | |
- (NSDate)parseDateFromString:(NSString*)date { | |
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; | |
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; | |
return [dateFormatter dateFromString:date]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment