- #import <UIKit/UIKit.h>
- #import <CoreGraphics/CGGeometry.h>
- #import <Foundation/Foundation.h>
- #import <MediaPlayer/MediaPlayer.h>
- #import "MobileStudio/MSAppLauncher.h"
- @interface AVPMainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- {
- MPMoviePlayerController *movieController;
- NSString *launchArg;
- UITableView *recentTable;
- }
- - (void) playMovieAtPath:(NSString *)moviePath;
- - (void) movieDidFinish:(NSNotification *)notification;
- @end
- @implementation AVPMainViewController
- - (id) init
- {
- if (self = [super init]) self.title = @"AVPlayer 1.0";
- return self;
- }
- - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return YES;
- }
- - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
- CGRect newFrame;
- newFrame.origin = CGPointMake(0.0f, 0.0f);
- if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
- newFrame.size = CGSizeMake(480.0f, 300.0f);
- else
- newFrame.size = CGSizeMake(320.0f, 460.0f);
- for (UIView *subview in [self.view subviews])
- {
- [subview setFrame:newFrame];
- }
- }
- - (void) loadView
- {
- recentTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];
- [recentTable setDataSource:self];
- [recentTable setDelegate:self];
- self.view = recentTable;
- launchArg = [MSAppLauncher
- readLaunchInfoArgumentForAppID:@"com.optimo.avplayer"
- withApplication:[UIApplication sharedApplication]
- deletingLaunchPList: TRUE
- ];
- if([launchArg length] > 0)
- {
- NSMutableArray *recent = [[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] mutableCopy];
- if(recent == nil)
- {
- recent = [NSMutableArray new];
- }
- [recent insertObject:[NSDictionary dictionaryWithObjectsAndKeys:launchArg, @"filePath",nil] atIndex:0];
- [[NSUserDefaults standardUserDefaults] setObject:recent forKey:@"RecentFiles"];
- [recent release];
- [recentTable reloadData];
- }
- }
- - (NSInteger) numberOfSectionsInTableView:(UITableView *)table
- {
- return 2;
- }
- - (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
- {
- switch(section)
- {
- case 0 : return 1;
- case 1 : return [[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] count];
- default : return 0;
- }
- }
- - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- switch(section)
- {
- case 0 : return nil;
- case 1 :
- return [NSString stringWithFormat:@"Recent Files: %i", [[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] count]];
- default : return nil;
- }
- }
- - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
- if([indexPath section] == 0)
- {
- [cell setText:@"Open File..."];
- }
- if([indexPath section] == 1)
- {
- [cell setText:[[[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] objectAtIndex:indexPath.row] objectForKey:@"filePath"]];
- }
- return cell;
- }
- - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- switch(indexPath.section)
- {
- case 0:
- NSLog(@"Open File...");
- break;
- case 1:
- [self playMovieAtPath:[[[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] objectAtIndex:indexPath.row] objectForKey:@"filePath"]];
- break;
- }
- [recentTable deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
- }
- - (void) dealloc
- {
- [movieController dealloc];
- [launchArg release];
- [recentTable release];
- [super dealloc];
- }
- - (void) playMovieAtPath:(NSString *)moviePath
- {
- movieController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
- [movieController setScalingMode:MPMovieScalingModeAspectFit];
- [[NSNotificationCenter defaultCenter]
- addObserver:self
- selector:@selector(movieDidFinish:)
- name:MPMoviePlayerPlaybackDidFinishNotification
- object:movieController
- ];
- [movieController play];
- }
- - (void) movieDidFinish:(NSNotification*)notification
- {
- movieController = [notification object];
- [[NSNotificationCenter defaultCenter]
- removeObserver:self
- name:MPMoviePlayerPlaybackDidFinishNotification
- object:movieController
- ];
- [movieController stop];
- [movieController release];
- }
- @end //endimp AVPMainViewController
- @interface AVPAppDelegate : NSObject <UIApplicationDelegate>
- {
- UINavigationController *navController;
- }
- @property (nonatomic, retain) UINavigationController *navController;
- @end
- @implementation AVPAppDelegate
- @synthesize navController;
- - (void) applicationDidFinishLaunching:(UIApplication *)application
- {
- UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- self.navController = [[UINavigationController alloc] initWithRootViewController:[[AVPMainViewController alloc] init]];
- [window addSubview:self.navController.view];
- [window makeKeyAndVisible];
- }
- - (void) dealloc
- {
- [self.navController release];
- [super dealloc];
- }
- @end //endimp AVPAppDelegate
- int main(int argc, char *argv[])
- {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- int ret = UIApplicationMain(argc, argv, nil, @"AVPAppDelegate");
- [pool release];
- return ret;
- }