9

iOS 5.1 ,here is the code:

 MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];  // player's frame must match parent's
    [self.view addSubview: player.view];
    // ...
    [player play];

in fact ,this is just the same as the apple reference say,but when I click the button to use this function,there is just a black Rectangle stay there,no vedio playing ,no sound happening,and no crush,so I want to know how to make this work

fsaint
  • 8,759
  • 3
  • 36
  • 48
Pengxuan Li
  • 91
  • 1
  • 1
  • 5

8 Answers8

17

Sorry for very late reply. The Solution is kind of wierd. But it helped me keep going while facing the same issue.

MPMovieSourceTypeStreaming is the thing you have missed.

MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];  // player's frame must match parent's
player.movieSourceType    = MPMovieSourceTypeStreaming;
[self.view addSubview: player.view];
// ...
[player play];
Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
  • This wasn't the original poster's problem, but it was definitely mine! – shulmey Jun 19 '13 at 22:26
  • 4
    My issue was I didn't set the movieSourceType property. So if it helps anyone. make sure you set that property. By default it must be set to MPMovieSourceTypeUnknown. When I've put it to MPMovieSourceTypeFile. It worked for me. – James Laurenstin Feb 02 '14 at 02:18
  • @SureshVarma , Thanks for your answer bro. Had same problem as OP, and didn't know this one line i.e. `player.movieSourceType = MPMovieSourceTypeStreaming;` could do magic! – Hyder Jun 14 '15 at 14:54
  • @SHA I am glad It helped. All the best – Suresh Varma Jun 14 '15 at 16:31
4

You may be sending your

[player play] 

message too soon.

Use the following notification observation

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];

To listen for the loadState to change to playable. To check for that update do this :

- (void)playMovie:(NSNotification *)notification {
    MPMoviePlayerController *player = notification.object;
    if (player.loadState & MPMovieLoadStatePlayable)
    {
        NSLog(@"Movie is Ready to Play");
        [player play];
    }
}

The movie should begin playing once it's ready.

David Hegner
  • 101
  • 1
  • 6
1

Try this below code for play video from your project Resource :

  NSBundle *bundle = [NSBundle mainBundle];
  NSString *moviePath = [bundle pathForResource:@"map" ofType:@"mp4"];
  NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

  MPMoviePlayerController *player =
  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
  [player prepareToPlay];
  [player.view setFrame: self.view.bounds];  // player's frame must match parent's
  [self.view addSubview: player.view];
    // ...
  [player play];

Thanks..!

Dinesh
  • 6,500
  • 10
  • 42
  • 77
  • Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' and Thanks for your help,but I had tried this kind of code before,this is the console information – Pengxuan Li May 09 '12 at 02:23
  • add your video to `copy bundle resources`. – Muruganandham K Oct 26 '13 at 07:30
1

Building on @Dinesh's answer, your URL creation is incorrect. The value of

[NSURL URLWithString:@"map.mp4"]

will be nil since @"map.mp4" is not a valid URL. To create a valid url from the app's bundle do what Dinesh says. To create a remote URL you would do something like

[NSURL URLWithString:@"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"]

The URL should contain all it's parts: protocol, host, path, file.

Cheers, Felipe

fsaint
  • 8,759
  • 3
  • 36
  • 48
  • Thanks,but actually I want to use a local mp4 file,not a stream,[NSBundle mainBundle] seems work. – Pengxuan Li May 09 '12 at 02:20
  • 1
    I'm glad it worked for you. Please consider awarding the answer to Dinesh. It is best for you too since not awarding it will hurt your reputation. – fsaint May 09 '12 at 07:18
1

Add your movie player controller view on main windows like:

 MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview: player.view];

[player play];

Hope so it will work!

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Amol
  • 85
  • 5
0

Your player should be a property of your view controller, something like this:

.h:

@property(nonatomic,weak)MPMoviePlayerController *moviePlayer;

.m:

    MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];  // player's frame must match parent's
    //set the player to your property
    self.moviePlayer=player;

    [self.view addSubview: self.moviePlayer.view];
    // ...
    [self.moviePlayer play];
Malloc
  • 15,434
  • 34
  • 105
  • 192
0

Please use MPMoviePlayerViewController Because of MP4 file. When you use MOV then working perfect!!

MPMoviePlayerViewController *moviePlayerViewController;

-(void)PlayVideoController:(NSString*)videoUrl1 { 
    NSURL *fileURL = [NSURL URLWithString:videoUrl1]; 
    moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];

    // Register for the playback finished notification.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];

    //Present
    [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];

    // Play the movie!
    moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [moviePlayerViewController.moviePlayer play];
}

-(void)myMovieFinishedCallback:(NSNotification*)aNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
    [moviePlayerViewController release], moviePlayerViewController = nil;
}
Sandeep Agrawal
  • 425
  • 3
  • 10
0

You might want to cross check video url for blank spaces. Following code works for me.

- (IBAction)btnPlayVideo:(id)sender {

    self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL    *fileURL    =   [NSURL URLWithString:[NSString stringWithFormat:@"%@", self.strVideoUrl]];
    NSLog(@"Url to play = %@", self.strVideoUrl);

        self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlaybackComplete:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:self.moviePlayerController];
        [self.moviePlayerController.view setFrame:self.view.bounds];
        [self.view addSubview:self.moviePlayerController.view];
        self.moviePlayerController.shouldAutoplay = YES;
        self.moviePlayerController.fullscreen = YES;
        self.moviePlayerController.controlStyle=NO;
        [self.moviePlayerController prepareToPlay];
        self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;

        [self.moviePlayerController play];

}

- (void)moviePlaybackComplete:(NSNotification *)notification
{
    self.moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.moviePlayerController];

    [self.moviePlayerController.view removeFromSuperview];
    self.closeVideAdProp.hidden = NO;
    btnCloseAd.hidden=FALSE;
}
Deepak Thakur
  • 3,453
  • 2
  • 37
  • 65