【问题标题】:Can't play video file using MPMoviePlayerController无法使用 MPMoviePlayerController 播放视频文件
【发布时间】:2013-08-27 13:56:32
【问题描述】:

视频文件为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);
}

Movieplayer 属性:

@property (nonatomic, strong) MPMoviePlayerController* movieplayer;

【问题讨论】:

  • 你用 MPMoviePlayerViewController 试过了吗?

标签: ios mpmovieplayercontroller


【解决方案1】:

首先,您不能在运行时修改(追加、添加、删除)应用程序的包。其次,本地文件的NSURL 必须使用fileURLWithPath 方法初始化。尝试纠正这个问题,如果不能播放,尝试观察MPMoviePlayerControllerloadState变化,当它的loadState准备好播放时,再尝试播放文件。希望这会有所帮助。

编辑 对不起,我的错,你没有在运行时更改应用程序的包,只是去第二部分。而且,你可以通过调用[[NSBundle mainBundle] URLForResource:@"file_name" withExtension:@"extension"];来获取文件的url

【讨论】:

    【解决方案2】:

    您的网址错误。就是这样,它没有成功。使用 [NSURL fileURLWithPath:path] 作为 url。它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 2013-07-08
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多