Play iOS videos using MPMoviePlayerController

I got this piece of code:

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:@"/Resources/disc.mp4"]]; theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen; theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; [theMoviPlayer.view setFrame:backgroundWindow.frame]; [backgroundWindow addSubview:theMoviPlayer.view]; [theMoviPlayer play]; 

But I really don't know how to add video to my project. In which folder should I put the video file !? Or do I need to do something else to add it to my project?

EDIT:

Sounds like this in xcode, right? Because now I get a playback error. I used to use url to play this video, and it worked pretty well, but locally not with this file :(

enter image description here

+7
ios iphone ios5 ios6
source share
4 answers

It's good that your bundle of packages looks tight, the below should work.

 NSBundle *bundle = [NSBundle mainBundle]; NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"]; NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen; theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; [theMoviPlayer.view setFrame:backgroundWindow.frame]; [backgroundWindow addSubview:theMoviPlayer.view]; [theMoviPlayer play]; 
+13
source share

Add MediaPlayer Framework

import it into your file

 #import <MediaPlayer/MediaPlayer.h> 

Create an MPMoviePlayerController Object

 MPMoviePlayerController * moviePlayer; 

write this code where you want to play the video

 NSString *filepath = [[NSBundle mainBundle] pathForResource:@"spacetest.mp4" ofType:nil]; NSURL *fileURL = [NSURL fileURLWithPath:filepath]; moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [self.view addSubview:moviePlayer.view]; moviePlayer.fullscreen = YES; [moviePlayer play]; 
+8
source share

Using HTML5 as I said above:

  NSString *videoTitle = @"disc.mp4"; NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; NSString *playPath = [NSString stringWithFormat:@"<center><video width=\"640\" height=\"480\" controls><source src=\"%@\" media=\"all and (max-width:1024px)\"></video></center>",videoTitle]; [webView loadHTMLString:playPath baseURL:baseURL]; 

This will play in 640x480 format, but if you are familiar with the HTML video tags, you can configure it quite strongly.

+1
source share

Since you are using MPMoviePlayerController and not UIWebView, you can place your mp4 or file in your resources and Xcode / iOS will find it. Make sure the directory / group the file is in is not yellow. You do not want this to be a relative path.

Just drag and drop the resource into your project. A copy of the items to the destination is selected, the first option of folders is selected and, most importantly, added to the target!

Ok try the code below:

 NSBundle *bundle = [NSBundle mainBundle]; NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"]; NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen; theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; [theMoviPlayer.view setFrame:backgroundWindow.frame]; [backgroundWindow addSubview:theMoviPlayer.view]; [theMoviPlayer play]; 
0
source share

All Articles