Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 5.48 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #import <UIKit/UIKit.h>
  2. #import <CoreGraphics/CGGeometry.h>
  3. #import <Foundation/Foundation.h>
  4. #import <MediaPlayer/MediaPlayer.h>
  5. #import "MobileStudio/MSAppLauncher.h"
  6.  
  7. @interface AVPMainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
  8. {
  9.         MPMoviePlayerController         *movieController;      
  10.         NSString                                        *launchArg;
  11.         UITableView                                     *recentTable;
  12. }
  13. - (void) playMovieAtPath:(NSString *)moviePath;
  14. - (void) movieDidFinish:(NSNotification *)notification;
  15. @end
  16.  
  17. @implementation AVPMainViewController
  18. - (id) init
  19. {
  20.         if (self = [super init]) self.title = @"AVPlayer 1.0";
  21.         return self;
  22. }
  23.  
  24. - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  25. {
  26.         return YES;
  27. }
  28.  
  29. - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration  {
  30.         CGRect newFrame;
  31.         newFrame.origin = CGPointMake(0.0f, 0.0f);
  32.        
  33.         if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
  34.                 newFrame.size = CGSizeMake(480.0f, 300.0f);
  35.         else
  36.                 newFrame.size = CGSizeMake(320.0f, 460.0f);
  37.  
  38.         for (UIView *subview in [self.view subviews])  
  39.         {
  40.                 [subview setFrame:newFrame];
  41.         }
  42. }
  43.  
  44. - (void) loadView
  45. {
  46.     recentTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];
  47.     [recentTable setDataSource:self];
  48.     [recentTable setDelegate:self];
  49.  
  50.     self.view = recentTable;
  51.  
  52.         launchArg = [MSAppLauncher
  53.                                         readLaunchInfoArgumentForAppID:@"com.optimo.avplayer"
  54.                                         withApplication:[UIApplication sharedApplication]
  55.                                         deletingLaunchPList: TRUE
  56.                                 ];
  57.  
  58.         if([launchArg length] > 0)
  59.         {
  60.                 NSMutableArray *recent = [[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] mutableCopy];
  61.                 if(recent == nil)
  62.                 {
  63.                         recent = [NSMutableArray new];
  64.                 }
  65.                
  66.                 [recent insertObject:[NSDictionary dictionaryWithObjectsAndKeys:launchArg, @"filePath",nil] atIndex:0];
  67.                
  68.                 [[NSUserDefaults standardUserDefaults] setObject:recent forKey:@"RecentFiles"];
  69.                
  70.                 [recent release];
  71.        
  72.                 [recentTable reloadData];
  73.                
  74.         }
  75. }
  76.  
  77. - (NSInteger) numberOfSectionsInTableView:(UITableView *)table
  78. {
  79.         return 2;
  80. }
  81.  
  82. - (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
  83. {
  84.         switch(section)
  85.         {
  86.                 case 0 : return 1;
  87.                 case 1 : return [[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] count];
  88.                 default : return 0;
  89.         }
  90.        
  91. }
  92.  
  93. - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  94. {
  95.         switch(section)
  96.         {
  97.                 case 0 : return nil;
  98.                 case 1 :
  99.                         return [NSString stringWithFormat:@"Recent Files: %i", [[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] count]];
  100.                 default : return nil;
  101.         }
  102. }
  103.  
  104. - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106.     UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
  107.  
  108.         if([indexPath section] == 0)
  109.         {
  110.                 [cell setText:@"Open File..."];
  111.         }
  112.        
  113.         if([indexPath section] == 1)
  114.         {
  115.                 [cell setText:[[[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] objectAtIndex:indexPath.row] objectForKey:@"filePath"]];
  116.         }
  117.     return cell;
  118. }
  119.  
  120. - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122.         switch(indexPath.section)
  123.         {
  124.                 case 0:
  125.                         NSLog(@"Open File...");
  126.                         break;
  127.                 case 1:
  128.                         [self playMovieAtPath:[[[[NSUserDefaults standardUserDefaults] objectForKey:@"RecentFiles"] objectAtIndex:indexPath.row] objectForKey:@"filePath"]];
  129.                         break;
  130.         }
  131.        
  132.         [recentTable deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
  133. }
  134.  
  135. - (void) dealloc
  136. {
  137.         [movieController dealloc];     
  138.         [launchArg release];
  139.        
  140.         [recentTable release]; 
  141.         [super dealloc];
  142. }
  143.  
  144. - (void) playMovieAtPath:(NSString *)moviePath
  145. {
  146.         movieController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
  147.         [movieController setScalingMode:MPMovieScalingModeAspectFit];
  148.        
  149.         [[NSNotificationCenter defaultCenter]
  150.                 addObserver:self
  151.                 selector:@selector(movieDidFinish:)
  152.                 name:MPMoviePlayerPlaybackDidFinishNotification
  153.                 object:movieController
  154.         ];
  155.         [movieController play];
  156. }
  157.  
  158. - (void) movieDidFinish:(NSNotification*)notification
  159. {
  160.         movieController = [notification object];
  161.         [[NSNotificationCenter defaultCenter]
  162.                 removeObserver:self
  163.                 name:MPMoviePlayerPlaybackDidFinishNotification
  164.                 object:movieController
  165.         ];
  166.         [movieController stop];
  167.         [movieController release];
  168. }
  169. @end    //endimp AVPMainViewController
  170.  
  171. @interface AVPAppDelegate : NSObject <UIApplicationDelegate>
  172. {
  173.         UINavigationController *navController;
  174. }
  175. @property (nonatomic, retain)           UINavigationController *navController;
  176. @end
  177.  
  178. @implementation AVPAppDelegate
  179.         @synthesize navController;
  180. - (void) applicationDidFinishLaunching:(UIApplication *)application
  181. {      
  182.         UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  183.         self.navController = [[UINavigationController alloc] initWithRootViewController:[[AVPMainViewController alloc] init]];
  184.  
  185.         [window addSubview:self.navController.view];
  186.         [window makeKeyAndVisible];
  187. }
  188.  
  189. - (void) dealloc
  190. {
  191.         [self.navController release];  
  192.         [super dealloc];
  193. }
  194. @end    //endimp AVPAppDelegate
  195.                                
  196. int main(int argc, char *argv[])
  197. {
  198.         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  199.         int ret = UIApplicationMain(argc, argv, nil, @"AVPAppDelegate");
  200.         [pool release];
  201.         return ret;
  202. }