MPMoviePlayerViewController不播放视频或显示控件

时间:2013-05-10 01:18:15

标签: iphone ios objective-c video mpmovieplayercontroller

这是我的代码应该播放嵌入在子视图中的视频,但它只显示没有控件的静止图像。

- (void)displayVideo:(NSURL *)videoURL
{
    if (self.mediaPlayer) {
        [self.mediaPlayer.view removeFromSuperview];
        self.mediaPlayer = nil;
    }

    self.mediaPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
    [self.mediaPlayer.moviePlayer prepareToPlay];
    self.mediaPlayer.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    self.mediaPlayer.view.frame = CGRectMake(0, 0, self.mediaView.bounds.size.width, self.mediaView.bounds.size.height);
    [self.mediaView addSubview:self.mediaPlayer.view];
    [self.mediaPlayer.moviePlayer play];
}

我还尝试直接加载媒体播放器,其中mediaPlayer是MPMoviePlayerController而不是MPMoviePlayerViewController,但只有黑色视图我才能得到更少。

    self.mediaPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    [self.mediaPlayer prepareToPlay];
    self.mediaPlayer.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    self.mediaPlayer.view.frame = CGRectMake(0, 0, self.mediaView.bounds.size.width, self.mediaView.bounds.size.height);
    [self.mediaView addSubview:self.mediaPlayer.view];
    [self.mediaPlayer play];

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

第一个代码完全错误。使用MPMoviePlayerViewController的唯一方法是作为呈现的视图控制器(presentViewController:...);你不能抓住它的观点,并试图将它推入你自己的界面。

第二个有更好的机会。所以这里有一些事情要考虑:

  • videoURL有效吗?你怎么知道的?不,真的。并考虑格式,因为不是每种视频格式都可以在iOS下播放。

  • self.mediaPlayer是否保留了电影播放器​​控制器?再次,仔细看;这至关重要。它必须有strongretain政策。

  • 您的界面中是否还有其他媒体播放器控制器视图?我注意到在第二个代码中你忘了删除前一个代码。这至关重要!只有一个这样的观点。

(顺便说一句,没有必要要求MPMovieControlStyleEmbedded;这是此配置的默认设置。)

最后,与工作代码进行比较可能会有所帮助。我的书中的代码工作:

http://www.apeth.com/iOSBook/ch28.html#_mpmovieplayercontroller

NSURL* m = [[NSBundle mainBundle] URLForResource:@"ElMirage"
                                   withExtension:@"mp4"];
MPMoviePlayerController* mp =
    [[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // retain policy
self.mpc.shouldAutoplay = NO;
[self.mpc prepareToPlay];
self.mpc.view.frame = CGRectMake(10, 10, 300, 250);
self.mpc.backgroundView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.mpc.view];

您可以通过下载此示例来证明:

https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/ch28p786moviePlayer

答案 1 :(得分:0)

首先检查您的videoURL有效吗? iOS中的视频技术支持使用.mov,.mp4,.m4v和.3gp文件扩展名播放电影文件,并使用以下压缩标准:

1)H.264视频,高达1.5 Mbps,640 x 480像素,每秒30帧,H.264基线配置文件的低复杂度版本,AAC-LC音频高达160 Kbps,48 kHz,立体声.m4v,.mp4和.mov文件格式的音频

2)H.264视频,高达768 Kbps,320 x 240像素,每秒30帧,基线配置文件高达1.3级,AAC-LC音频高达160 Kbps,48 kHz,立体声音频,.m4v, .mp4和.mov文件格式

3)MPEG-4视频,高达2.5 Mbps,640 x 480像素,每秒30帧,简单配置文件,AAC-LC音频高达160 Kbps,48 kHz,立体声音频,.m4v,.mp4和.mov文件格式

 -(void) viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];

     player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:strSelectedVideoUrl]];
     player.scalingMode = MPMovieScalingModeAspectFit;
     player.movieSourceType = MPMovieSourceTypeFile;
       player.view.frame = CGRectMake(0, 45, 320, 400);
       player.shouldAutoplay = YES;
     [player prepareToPlay];
     [self.view addSubview:player.view];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];

     [player play];
 }
 - (void) movieFinishedCallback:(NSNotification*) aNotification {
     MPMoviePlayerController *player1 = [aNotification object];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player1];
     [player stop];
     [player1.view removeFromSuperview];
     //[player1 release];
     player1 = nil;
     [self.navigationController popViewControllerAnimated:YES];
 }

答案 2 :(得分:0)

试试这个,它对我来说很完美

NSURL *movieURL = [NSURL URLWithString:@"http://........"];

// Initialize a movie player object with the specified URL

 self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;


    [self.moviePlayer.view setFrame:self.view.bounds];

    [self.view addSubview:self.moviePlayer.view];

    [self.moviePlayer play];
相关问题