How To Play Video In iPhoneOS4

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 <uikit/UIKit.h>
#import <mediaPlayer/MediaPlayer.h>

@interface VideoPlayiPhoneOS4ViewController : UIViewController {

MPMoviePlayerController *moviePlayer;
NSURL *movieURL;

}

 

 

Step 6: Open the VideoPlayiPhoneOS4ViewController.m file and make the following changes:

 

-(void)viewWillAppear:(BOOL)animated
{
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

Leave a Comment:

22 comments
Allen says June 30, 2010

Where’s the volume controller?

Reply
Robert says June 30, 2010

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

Reply
    Bram says November 4, 2010

    theMovie.scalingMode = MPMovieScalingModeFill;

    i used this line of code.. theMovie is my pointer so you have to fill in your own pointer

    Reply
    Macheo says January 19, 2011

    Deze code is voor fulscreen movie!

    Stap 1:
    Maak een mediaplayer.framework aan

    Stap 2:
    Zet deze code in je .h view:

    UIWindow *window;

    -(IBAction)playMovie;

    @property (nonatomic, retain) IBOutlet UIWindow *window;

    Stap 3:
    Zet deze code in je .m view:

    #import

    -(IBAction)playMovie {
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@”Jou movie” ofType:@”Type movie”];
    NSURL *fileURL = [NSURL fileURLWithPath:filepath];
    MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
    [moviePlayerController.moviePlayer play];
    [window addSubview:moviePlayerController.view];

    }

    Ik hoop dat je hier iets aan hebt!

    Groetjes Macheo

    Reply
Sushant says June 30, 2010

You need to change the value of moviePlayer.view.frame = CGRectMake(0, 0, 300, 400); , according to your device screen height width.

Reply
Robert says June 30, 2010

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?

Reply
Robert says June 30, 2010

@Allen: The Volume Bar is only visible on the device.

Reply
Sushant says July 1, 2010

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);

Reply
luis says July 8, 2010

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

Reply
Tyler says July 18, 2010

Robert: I was having the same issue with fullscreen, you have to set the controlStyle to Fullscreen.

theMovie.controlStyle = MPMovieControlStyleFullscreen;

Reply
Eduardo Ballesteros says August 13, 2010

No sound???
No landscape orientation.

Help please!!!!!!

Reply
Sushant says August 17, 2010

Eduardo: Is there any issue in the Source code? Please let us know.

Reply
Eduardo Ballesteros says August 17, 2010

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

Reply
Brian McCarthy says September 22, 2010

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.

Reply
Brian McCarthy says September 22, 2010

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.

Reply
Brian McCarthy says September 22, 2010

Thank you Sushant really enjoying the courses

Reply
Sushant says September 25, 2010

@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.

Reply
Carter Kristek says October 25, 2010

Hello I was wondering how I would make the video start playing after I push a UIButton. I thought about setting up a UIButton in the .h view controller but when I connect it in interface builder and go back to .m view controller i’m lost on what to do. Could you help me out please.

Reply
emam says December 14, 2010

Hi nice tutorial. I used it and play video. so can I get information how to play video striming file in ipad.

Reply
BAPPS says January 16, 2011

thank you very much. but how to release the moviePlayer after the movie has finished?

Reply
Macheo says January 19, 2011

Deze code is voor fulscreen movie!

Stap 1:
Maak een mediaplayer.framework aan

Stap 2:
Zet deze code in je .h view:

UIWindow *window;

-(IBAction)playMovie;

@property (nonatomic, retain) IBOutlet UIWindow *window;

Stap 3:
Zet deze code in je .m view:

#import

-(IBAction)playMovie {
NSString *filepath = [[NSBundle mainBundle] pathForResource:@”Jou movie” ofType:@”Type movie”];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[moviePlayerController.moviePlayer play];
[window addSubview:moviePlayerController.view];

}

Ik hoop dat je hier iets aan hebt!

Reply
Kurt says June 4, 2011

Is there a way to have the video play over a background image in both Portrait and Landscape orientation instead of having the black bars top and bottom?

Thanks

Kurt

Reply
Add Your Reply