Nikhil Nikhil - 1 year ago 74
iOS Question

How to play a video from either a local or a server URL in iOS

How can I play a .mp4 or .mov video from either an Internet URL or a local file in iOS?

Answer Source

1.First of all add MediaPlayer.Framework in XCode

2.Then add #import < MediaPlayer/MediaPlayer.h > in your viewController's .h file

3.Now implement this code in your viewDidLoad Method

     //NSString *filepath = [[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"mp4"];  
     //NSURL    *fileURL = [NSURL fileURLWithPath:filepath];  

     NSURL *fileURL = [NSURL URLWithString:@""];

     moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
     [moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; 
     [self.view addSubview:moviePlayerController.view];  
     moviePlayerController.fullscreen = YES;  
     [moviePlayerController play];  

For Orientation Please add this code

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     // Return YES for supported orientations
          if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
         [moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; 
     } else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
         [moviePlayerController.view setFrame:CGRectMake(0, 0, 480, 320)]; 
     return YES;

In this code moviePlayerController is MPMoviePlayerController declared in .h file

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download