Jonny
Jonny

Reputation: 16338

Can't play video file using MPMoviePlayerController

The video file is http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4 I can't play it after copying it into the bundle of an app, then running this: What's wrong?

-(void)showvideo {
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];

    [center addObserver:self selector:@selector(mPMoviePlayerLoadStateDidChangeNotification) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
    [center addObserver:self selector:@selector(readytoplay) name:MPMoviePlayerReadyForDisplayDidChangeNotification object:nil];

    NSBundle* b = NSBundle.mainBundle;
    NSString* res = [b resourcePath];
    NSString* file;
    //  file = @"sr.m4v";
//  file = @"sample_iPod.m4v";
    file = @"big_buck_bunny.mp4";
//  file = @"xxx.mp4";
    NSString* path = [res stringByAppendingPathComponent:file];
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        NSLog(@"file exists!");
    }
    else {
        NSLog(@"file does not exist!");
    }

    NSURL* url = [NSURL URLWithString:path];
    NSLog(@"url: %@", url);

    self.movieplayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    //  self.movieplayer = [MPMoviePlayerController new];
    self.movieplayer.movieSourceType = MPMovieSourceTypeFile;

    NSLog(@"self.movieplayer: %@, loadState: %d", self.movieplayer, self.movieplayer.loadState);

    [self.movieplayer prepareToPlay];

    NSLog(@"self.movieplayer: %@, loadState: %d", self.movieplayer, self.movieplayer.loadState);
}

-(void)readytoplay {
    NSLog(@"readytoplay");

    [self.movieplayer.view setFrame:self.view.bounds];  // player's frame must match parent's
    [self.view addSubview:self.movieplayer.view];

    NSLog(@"self.movieplayer: %@", self.movieplayer);
    self.movieplayer.fullscreen = YES;
    NSLog(@"self.movieplayer: %@", self.movieplayer);
    [self.movieplayer play];
}

-(void)mPMoviePlayerLoadStateDidChangeNotification {
    NSLog(@"mPMoviePlayerLoadStateDidChangeNotification %@, loadState: %d", self.movieplayer, self.movieplayer.loadState);
}

Movieplayer property:

@property (nonatomic, strong) MPMoviePlayerController* movieplayer;

Upvotes: 0

Views: 1521

Answers (2)

Mani Kandan
Mani Kandan

Reputation: 629

your URL is wrong. Thats y it didnt work out. Use [NSURL fileURLWithPath:path] for url. It should work.

Upvotes: 0

Fahri Azimov
Fahri Azimov

Reputation: 11768

First of all, you can't modify (append, add, delete) application's bundle on runtime. Secondly, NSURL for local files has to be initialised with fileURLWithPath method. Try to correct this issues, and if it doesn't play, try to observe MPMoviePlayerController's loadState changes, and when its loadState is ready to play, then try to play the file. Hope this helps.

EDIT Sorry, my bad, you are not changing app's bundle on the runtime, just go for Second part. And, you can get the url for the file by calling just [[NSBundle mainBundle] URLForResource:@"file_name" withExtension:@"extension"];

Upvotes: 4

Related Questions