Nosrettap
Nosrettap

Reputation: 11320

Why won't my movie play using MPMoviePlayerController?

I'm trying to get a basic movie to play in an iPhone app; however, I can't seem to get it to work. Here's my entire code:

#import <MediaPlayer/MediaPlayer.h>
#import "ViewController.h"

@implementation ViewController

- (IBAction)playMoviePressed:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];

    MPMoviePlayerController *moviePlayer =
    [[MPMoviePlayerController alloc] initWithContentURL:url];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayer];

    moviePlayer.controlStyle = MPMovieControlStyleDefault;
    moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:moviePlayer.view];
    [moviePlayer setFullscreen:YES animated:YES];
    [moviePlayer prepareToPlay];
    [moviePlayer play];
}


@end

I have one button on screen that calls playMoviePressed when it is tapped. I know that this method is getting called. I have also tried this with .mov and .mp4 local files that I have dragged into xcode.

Upvotes: 0

Views: 1466

Answers (3)

Nosrettap
Nosrettap

Reputation: 11320

I figured it out. My problem was really dumb. Because I am not maintaining a strong pointer to my MPMoviePlayerViewController, it gets deallocated as soon as the method ends. A simple @property in the interface solves this.

Upvotes: 0

K S
K S

Reputation: 212

If you want a full screen mode, you can simply use MPMoviePlayerViewController and do something like:

MPMoviePlayerViewController* viewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:viewController];

Upvotes: 1

ShowMe Xcode
ShowMe Xcode

Reputation: 136

I got your code to play the video by adding a frame for it:

[moviePlayer.view setFrame:CGRectMake(0, 0, 320, 200)];

However the video was choppy but that could be my internet connection though. Seeing that you're using an IBAction, you're probably using Interface Builder or Storyboard so the only thing that I can think that's preventing it from playing is that you're missing the connection to the IBOutlet.

Upvotes: 3

Related Questions