How to Play a Video With MPMoviePlayerController

Dec 20, 2009   //   by Derek van Vliet   //   Development  //  6 Comments

Splash screens, cutscenes, tutorials or just good old content. There are plenty of reasons to need to play a video in your iPhone app. Here’s how to do it in 3 simple steps:

1. Add the MediaPlayer framework to your target.

2. Import the MediaPlayer header file in the file you intend to begin playing the video in, like so:

#import <MediaPlayer/MediaPlayer.h>

3a. Now we can stream a video from the internet like so:

NSURL *url = [NSURL URLWithString:@"http://www.example.com/myvideo.m4v"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];

3b. Alternately, we can stream a video that you include in your app bundle. To stream a file called myvideo.m4v, you would run the following code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"m4v"];
NSURL *url = [NSURL fileURLWithPath:path];
 
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];

6 Comments

  • Nice simple instructions play a movie :-) Like.

    But … does anyone know how to play a movie next to other interface items like buttons?

  • Nice but it does not shows Video, I have used this code under a button pressed event but it only play audio not showing video

  • Great.!!
    I am working on developing an app which contains the use of lots of videos in it. I want to play two or three videos one after the other. When one video gets over, other video should start playing without pressing any button.
    I am trying to use NSArray for storing and retrieving videos one after the other but it is not working.
    I also tried putting all the videos in Bundle resources but i don’t know how to retrieve all the videos in sequence from bundle resources.
    Can anyone has any idea about this? How can i play videos one after the other?

    Thankx

  • Great.!!
    I am working on developing an app which contains the use of lots of videos in it. I want to play two or three videos one after the other. When one video gets over, other video should start playing without pressing any button.
    I am trying to use NSArray for storing and retrieving videos one after the other but it is not working.
    I also tried putting all the videos in Bundle resources but i don’t know how to retrieve all the videos in sequence from bundle resources.
    Can anyone has any idea about this? How can i play videos one after the other?

    Thankx

  • Great.!!
    I am working on developing an app which contains the use of lots of videos in it. I want to play two or three videos one after the other. When one video gets over, other video should start playing without pressing any button.
    I am trying to use NSArray for storing and retrieving videos one after the other but it is not working.
    I also tried putting all the videos in Bundle resources but i don’t know how to retrieve all the videos in sequence from bundle resources.
    Can anyone has any idea about this? How can i play videos one after the other?

    Thankx

  • Great.!!
    I am working on developing an app which contains the use of lots of videos in it. I want to play two or three videos one after the other. When one video gets over, other video should start playing without pressing any button.
    I am trying to use NSArray for storing and retrieving videos one after the other but it is not working.
    I also tried putting all the videos in Bundle resources but i don’t know how to retrieve all the videos in sequence from bundle resources.
    Can anyone has any idea about this? How can i play videos one after the other?

    Thankx

Leave a comment

Our Games

Latest Tweets