Created
June 19, 2012 21:41
-
-
Save kellabyte/2956706 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
#import "Video_DemoViewController.h" | |
#import "AWSServiceController.h" | |
NSString *M3PlaybackTime = @"Duration"; | |
NSString *M3PlayMessage = | |
@"http://kellabyte.com/samples/VideoDemo/Messages/PlayMessage"; | |
NSString *M3PauseMessage = | |
@"http://kellabyte.com/samples/VideoDemo/Messages/PauseMessage"; | |
@interface Video_DemoViewController () | |
- (MPMoviePlayerController *)_moviePlayer; | |
@end | |
@implementation Video_DemoViewController | |
@synthesize serviceController; | |
@synthesize contentView; | |
/*************************** | |
Set up | |
**************************/ | |
- (id)initWithCoder:(NSCoder *)aDecoder { | |
if ((self = [super initWithCoder:aDecoder])) { | |
[self addObserver:self | |
forKeyPath:@"serviceController" | |
options:NSKeyValueObservingOptionOld | |
context:NULL]; | |
} | |
return self; | |
} | |
/*************************** | |
Update notifications when the service controller changes | |
**************************/ | |
- (void)observeValueForKeyPath:(NSString *)keyPath | |
ofObject:(id)object | |
change:(NSDictionary *)change | |
context:(void *)context { | |
if ([keyPath isEqualToString:@"serviceController"]) { | |
[[SAFE_NULL([change objectForKey:NSKeyValueChangeOldKey]) | |
notificationCenter] removeObserver:self]; | |
id centre = [[self serviceController] notificationCenter]; | |
[centre addObserver:self | |
selector:@selector(playMovie:) | |
name:M3PlayMessage | |
object:nil]; | |
[centre addObserver:self | |
selector:@selector(pauseMovie:) | |
name:M3PauseMessage | |
object:nil]; | |
} | |
} | |
/*************************** | |
Set up the movie player | |
**************************/ | |
- (void)viewDidLoad { | |
MPMoviePlayerController *player = [self _moviePlayer]; | |
[[player view] setFrame:[[self contentView] bounds]]; | |
[[self contentView] addSubview:[player view]]; | |
[super viewDidLoad]; | |
} | |
/*************************** | |
Set up and return the movie player | |
**************************/ | |
- (MPMoviePlayerController *)_moviePlayer { | |
if (!moviePlayer) { | |
NSURL *url = [NSURL URLWithString: | |
@"http://someurl/tron.m4v"]; | |
moviePlayer = [[MPMoviePlayerController alloc] | |
initWithContentURL:url]; | |
[moviePlayer setShouldAutoplay:NO]; | |
[moviePlayer setControlStyle:MPMovieControlStyleNone]; | |
} | |
return moviePlayer; | |
} | |
/*************************** | |
Handle a play notification | |
**************************/ | |
- (void)playMovie:(NSNotification *)aNote { | |
NSNumber *time = [[aNote userInfo] objectForKey:M3PlaybackTime] | |
CGFloat playbackTime = [time floatValue]; | |
[[self _moviePlayer] setCurrentPlaybackTime:playbackTime]; | |
[[self _moviePlayer] play]; | |
} | |
/*************************** | |
Handle a pause notification | |
**************************/ | |
- (void)pauseMovie:(NSNotification *)aNote { | |
NSNumber *time = [[aNote userInfo] objectForKey:M3PlaybackTime] | |
CGFloat playbackTime = [time floatValue]; | |
[[self _moviePlayer] setCurrentPlaybackTime:playbackTime]; | |
[[self _moviePlayer] pause]; | |
} | |
/*************************** | |
Post a play notification when the user presses the play button | |
**************************/ | |
- (IBAction)play:(id)sender { | |
CGFloat time = [[self _moviePlayer] currentPlaybackTime]; | |
NSNumber *timeObj = [NSNumber numberWithFloat:time]; | |
id info = [NSDictionary dictionaryWithObject:timeObj | |
forKey:M3PlaybackTime] | |
id centre = [[self serviceController] notificationCenter]; | |
[centre postNotificationName:M3PlayMessage | |
object:nil | |
userInfo:info]; | |
} | |
/*************************** | |
Post a pause notification when the user presses the pause button | |
**************************/ | |
- (IBAction)pause:(id)sender { | |
CGFloat time = [[self _moviePlayer] currentPlaybackTime]; | |
NSNumber *timeObj = [NSNumber numberWithFloat:time]; | |
id info = [NSDictionary dictionaryWithObject:timeObj | |
forKey:M3PlaybackTime] | |
id centre = [[self serviceController] notificationCenter]; | |
[centre postNotificationName:M3PauseMessage | |
object:nil | |
userInfo:info]; | |
} | |
/*************************** | |
Clean up | |
**************************/ | |
- (void)dealloc { | |
[contentView release]; | |
[super dealloc]; | |
} | |
- (void)viewDidUnload { | |
[self setContentView:nil]; | |
[super viewDidUnload]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment