How to Play Video in iPhoneOS4

In this example we will see how to play video in iPhone OS4.

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

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

14 Responses to “How to Play Video in iPhoneOS4”

  1. Allen says:

    Where’s the volume controller?

  2. Robert says:

    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

  3. Sushant says:

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

  4. Robert says:

    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?

  5. Robert says:

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

  6. Sushant says:

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

  7. luis says:

    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

  8. Eduardo Ballesteros says:

    No sound???
    No landscape orientation.

    Help please!!!!!!

  9. Sushant says:

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

  10. Eduardo Ballesteros says:

    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

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

  12. Thank you Sushant really enjoying the courses

  13. Sushant says:

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

  14. emam says:

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

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG