无法使用MPMoviePlayerController播放视频文件

时间:2013-08-27 04:52:25

标签: ios mpmovieplayercontroller

视频文件为http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4 我将它复制到应用程序包中后无法播放,然后运行: 怎么了?

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

电影播放器​​属性:

@property (nonatomic, strong) MPMoviePlayerController* movieplayer;

2 个答案:

答案 0 :(得分:4)

首先,您无法在运行时修改(追加,添加,删除)应用程序的包。其次,必须使用NSURL方法初始化本地文件的fileURLWithPath。尝试更正此问题,如果无法解决,请尝试观察MPMoviePlayerController的{​​{1}}更改,并在其loadState准备好播放时,尝试播放该文件。希望这会有所帮助。

修改 对不起,我的不好,你没有在运行时更改应用程序包,只是去第二部分。而且,您只需调用loadState

即可获取该文件的网址

答案 1 :(得分:0)

您的网址错误。多数民众赞成并没有成功。使用[NSURL fileURLWithPath:path]作为url。它应该工作。