//
//  SimpleVideoPlayerViewController.m
//  SimpleVideoPlayer
//
//  Created by Chris Adamson on 12/29/08.
//  Copyright Subsequently and Furthermore, Inc. 2008. All rights reserved.
//
//
//  Licensed with the Apache 2.0 License
//  http://apache.org/licenses/LICENSE-2.0
//


#import "SimpleVideoPlayerViewController.h"

@implementation SimpleVideoPlayerViewController

@synthesize logView;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 */
- (void)viewDidLoad {
    [super viewDidLoad];
//START:code.SimpleVideoPlayer.SimpleVideoPlayerViewController.initcontroller
	NSString *videoPath = [[NSBundle mainBundle]
		pathForResource:@"movie" ofType:@"m4v"];
	if (videoPath == NULL)
		return;
	NSURL *videoURL = [NSURL fileURLWithPath: videoPath];
	moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
//END:code.SimpleVideoPlayer.SimpleVideoPlayerViewController.initcontroller

	// register for notifications
//START:code.SimpleVideoPlayer.SimpleVideoPlayerViewController.registerfornotifications
	[[NSNotificationCenter defaultCenter] addObserver:self
		selector: @selector (playbackFinished:)
		name:@"MPMoviePlayerPlaybackDidFinishNotification"
		object:nil];
//END:code.SimpleVideoPlayer.SimpleVideoPlayerViewController.registerfornotifications
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

//START:code.SimpleVideoPlayer.SimpleVideoPlayerViewController.appendtexttologview
- (void) appendTextToLogView: (NSString*) text {
	logView.text = [NSString stringWithFormat: @"%@%@: %@\n",
		logView.text, [NSDate date], text];
}
//END:code.SimpleVideoPlayer.SimpleVideoPlayerViewController.appendtexttologview

 
- (void) playVideo {
	[moviePlayer play];
	[self appendTextToLogView:@"Playing"];
}


//START:code.SimpleVideoPlayer.SimpleVideoPlayerViewController.playbackfinished
- (void) playbackFinished: (NSNotification*) notification {
	[self appendTextToLogView:@"Finished"];
}
//END:code.SimpleVideoPlayer.SimpleVideoPlayerViewController.playbackfinished


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
	[moviePlayer release];
	[logView release];
}

@end
