diff options
| author | Brian Jordan | 2012-03-27 20:54:10 -0400 |
|---|---|---|
| committer | Brian Jordan | 2012-03-27 20:54:10 -0400 |
| commit | 9ac7c867baf16a6cf1992956891e7a61503edb89 (patch) | |
| tree | 4106f42d62aebee25042a6df49bb89117a55059f /Video Tuneup/ViewController.m | |
| parent | f1d5f1118a540bfc8ffe71be788fc355e75ad1a9 (diff) | |
| download | Video-Tuneup-9ac7c867baf16a6cf1992956891e7a61503edb89.tar.bz2 | |
video loading
Diffstat (limited to 'Video Tuneup/ViewController.m')
| -rw-r--r-- | Video Tuneup/ViewController.m | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/Video Tuneup/ViewController.m b/Video Tuneup/ViewController.m index 5625a9b..200553f 100644 --- a/Video Tuneup/ViewController.m +++ b/Video Tuneup/ViewController.m @@ -7,9 +7,99 @@ // #import "ViewController.h" +#import "PlayerView.h" + +// Define this constant for the key-value observation context. +static const NSString *ItemStatusContext; @implementation ViewController +@synthesize player, playerItem, playerView, playButton; + +#pragma mark - Video playback + +- (void)syncUI { + if ((player.currentItem != nil) && + ([player.currentItem status] == AVPlayerItemStatusReadyToPlay)) { + playButton.enabled = YES; + } + else { + playButton.enabled = NO; + } +} + +- (IBAction)loadAssetFromFile:sender { + + NSLog(@"Loading asset."); + + NSURL *fileURL = [[NSBundle mainBundle] + URLForResource:@"airplane" withExtension:@"m4v"]; + + AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil]; + + NSLog(@"Asset duration is %f", CMTimeGetSeconds([asset duration])); + + NSString *tracksKey = @"tracks"; + + [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler: + ^{ + NSLog(@"Handler block reached"); + // Completion handler block. + dispatch_async(dispatch_get_main_queue(), + ^{ + NSError *error = nil; + AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error]; + + if (status == AVKeyValueStatusLoaded) { + self.playerItem = [AVPlayerItem playerItemWithAsset:asset]; + [playerItem addObserver:self forKeyPath:@"status" + options:0 context:&ItemStatusContext]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(playerItemDidReachEnd:) + name:AVPlayerItemDidPlayToEndTimeNotification + object:playerItem]; + self.player = [AVPlayer playerWithPlayerItem:playerItem]; + [playerView setPlayer:player]; + + // File has loaded into player + NSLog(@"File loaded!"); + NSLog(@"Asset duration is %f", CMTimeGetSeconds([asset duration])); + } + else { + // You should deal with the error appropriately. + NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]); + } + }); + }]; +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object + change:(NSDictionary *)change context:(void *)context { + + if (context == &ItemStatusContext) { + // Have to dispatch to main thread queue for UI operations + dispatch_async(dispatch_get_main_queue(), + ^{ + [self syncUI]; + }); + return; + } + [super observeValueForKeyPath:keyPath ofObject:object + change:change context:context]; + return; +} + +- (IBAction)play:sender { + [player play]; +} + +- (void)playerItemDidReachEnd:(NSNotification *)notification { + [player seekToTime:kCMTimeZero]; +} + + +#pragma mark - View controller boilerplate + - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; @@ -21,7 +111,32 @@ - (void)viewDidLoad { [super viewDidLoad]; + [self syncUI]; + + // Register with the notification center after creating the player item. + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(playerItemDidReachEnd:) + name:AVPlayerItemDidPlayToEndTimeNotification + object:[player currentItem]]; + + // Do any additional setup after loading the view, typically from a nib. + // http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/ + +// NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/airplane.m4v", [[NSBundle mainBundle] resourcePath]]]; + // http://developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html#//apple_ref/doc/uid/TP40010188-CH3-SW2 + + // Create an asset using AVURLAsset and load its tracks using loadValuesAsynchronouslyForKeys:completionHandler:. + // When the asset has loaded its tracks, create an instance of AVPlayerItem using the asset. + // Associate the item with an instance of AVPlayer. + // Wait until the item’s status indicates that it’s ready to play (typically you use key-value observing to receive a notification when the status changes). + + // Put video in supporting files + + // Get URL of video + + // Load video into player? } - (void)viewDidUnload |
