电影播放错误

时间:2012-05-31 10:18:14

标签: ios objective-c mpmovieplayercontroller

我正在尝试在设备上播放视频。视频是这样的 video

和我的代码:

  NSString *urlStr = @"http://easyhtml5video.com/images/happyfit2.mp4";
NSURL *url = [NSURL fileURLWithPath:urlStr];
  moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 320, 400);  
[moviePlayer play];

当我启动应用程序时,会看到电影屏幕,但电影无法播放。你能帮帮我哪里出错吗?

2 个答案:

答案 0 :(得分:1)

尝试使用     NSURL *url = [NSURL URLWithPath:urlStr];

而不是     NSURL *url = [NSURL fileURLWithPath:urlStr]; 这是一个网址,而不是文件网址。

答案 1 :(得分:0)

查看此视频以供参考。这对我有用 -

- (IBAction)playMovie:(id)sender
{
    NSString *filepath   =   @"http://easyhtml5video.com/images/happyfit2.mp4";
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:)
                                                               name:MPMoviePlayerPlaybackDidFinishNotification
                                                             object:moviePlayerController];

    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    [moviePlayerController play];
}

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

    [moviePlayerController.view removeFromSuperview];
    [moviePlayerController release];
}