In this example we will see how to play video in iPhone OS4.
Step 1: Create a View base application using template. Give the application name “VideoPlayiPhoneOS4”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: Xpand classes and notice Interface Builder created the VideoPlayiPhoneOS4ViewController class for you. Expand Resources and notice the template generated a separate nib,VideoPlayiPhoneOS4ViewController.xib, for the “VideoPlayiPhoneOS4”.
Step 4: We need to add MediaPlayer.framework in the Frameworks folder. Select-> Frameworks folder -> Add ->Existing Frameworks -> then select MediaPlayer.framework.
Step 5: In the VideoPlayiPhoneOS4ViewController.h file , we have created instance of MPMoviePlayerController class, that manage the playback of a movie from a file or from the network, and create a instance of NSURL class . So make the following changes in the file.
#import <MediaPlayer/MediaPlayer.h>
@interface VideoPlayiPhoneOS4ViewController : UIViewController {
MPMoviePlayerController *moviePlayer;
NSURL *movieURL;
}
Step 6: Open the VideoPlayiPhoneOS4ViewController.m file and make the following changes:
{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"3idiots.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 300, 400);
[moviePlayer play];
}
Step 7: Now compile and run the application in the Simulator.
You can Download SourceCode from here VideoPlayiPhoneOS4












Where’s the volume controller?
Thanks, this helped me a lot
I have one more noob question: How can I make it fullscreen? Tryed the fullscreen and setFullscreen property, but noting worked.
Maybe you can help me with this?
Bye,
Robert
You need to change the value of moviePlayer.view.frame = CGRectMake(0, 0, 300, 400); , according to your device screen height width.
This I already found out, but this is not the fullscreen mode. It’s when you click on the button with the two arrows pointing in the opposite direction. This ist what setFullscreen should do, or did I misunderstood something?
@Allen: The Volume Bar is only visible on the device.
Yes, you can set fullscreen when you click the arrow. But if you want to change the frame height, width then you change the
moviePlayer.view.frame = CGRectMake(0, 0, 300, 400);
My app crash when I tray to play large videos
Any one has a solution to play large videos file (size 300 MB)
MPMoviePlayerController on large videos causes massive memory spike, and a level 1 memory warning
No sound???
No landscape orientation.
Help please!!!!!!
Eduardo: Is there any issue in the Source code? Please let us know.
Your source code dont change the orientation automatically, and always launch in portrait mode, and the status bar never disappear.
I force the landscape orientation
[ [moviePlayer view] setTransform:CGAffineTransformMakeRotation(M_PI/2)];
but the problem of the status bar is not resolved….
Thank you
Hello Sushant, I have built some of your examples keying in the code and able to run on Simulator 3.2. But the Video app will not run as there are two errors in the code. I copied the code and checked from your master copy and eventually I cut and paste this section and it is still showing errors even though it is identical to your code. This is what I keyed but an error stating “Request for member ‘view’ is not a structure or union” and the second error states the same for both self.view and moviePlayer.view.
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 300, 400);
[moviePlayer play];
I see that some of your students were successful so it appears I am doing something to cause these errors. Any help please. Thank you.
Thank you Sushant really enjoying the courses
@Brian, i think you need to update your Xcode version. Actually its fine in my mac, please try once again. If you have still problem then please let us know.
Hi nice tutorial. I used it and play video. so can I get information how to play video striming file in ipad.