MPMoviePlayerController 给了我一个黑色的空视图,没有视频播放

Posted

技术标签:

【中文标题】MPMoviePlayerController 给了我一个黑色的空视图,没有视频播放【英文标题】:MPMoviePlayerController gives me a black empty view,no video playing 【发布时间】:2012-05-03 07:37:43 【问题描述】:

ios 5.1,代码如下:

 MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];  // player's frame must match parent's
    [self.view addSubview: player.view];
    // ...
    [player play];

其实这和苹果参考说的一样,但是当我点击按钮使用这个功能时,只有一个黑色的矩形停留在那里,没有视频播放,没有声音发生,也没有粉碎,所以我想知道如何完成这项工作

【问题讨论】:

【参考方案1】:

很抱歉回复太晚了。解决方案有点奇怪。但它帮助我在面对同样的问题时继续前进。

MPMovieSourceTypeStreaming 是你错过的东西。

MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];  // player's frame must match parent's
player.movieSourceType    = MPMovieSourceTypeStreaming;
[self.view addSubview: player.view];
// ...
[player play];

【讨论】:

这不是原海报的问题,但绝对是我的问题! 所以@shulmey,有帮助吗? 我的问题是我没有设置 movieSourceType 属性。所以,如果它帮助任何人。确保设置该属性。默认情况下,它必须设置为 MPMovieSourceTypeUnknown。当我把它放到 MPMovieSourceTypeFile 中时。它对我有用。 @SureshVarma,谢谢你的回答兄弟。和 OP 有同样的问题,但不知道这一行,即player.movieSourceType = MPMovieSourceTypeStreaming; 可以变魔术! @SHA 我很高兴它有帮助。万事如意【参考方案2】:

您可能正在发送您的

[player play] 

消息太早了。

使用以下通知观察

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];

监听 loadState 变为可播放。要检查该更新,请执行以下操作:

- (void)playMovie:(NSNotification *)notification 
    MPMoviePlayerController *player = notification.object;
    if (player.loadState & MPMovieLoadStatePlayable)
    
        NSLog(@"Movie is Ready to Play");
        [player play];
    

电影准备好后应该开始播放。

【讨论】:

【参考方案3】:

试试下面的代码来播放你项目资源中的视频:

  NSBundle *bundle = [NSBundle mainBundle];
  NSString *moviePath = [bundle pathForResource:@"map" ofType:@"mp4"];
  NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

  MPMoviePlayerController *player =
  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
  [player prepareToPlay];
  [player.view setFrame: self.view.bounds];  // player's frame must match parent's
  [self.view addSubview: player.view];
    // ...
  [player play];

谢谢..!

【讨论】:

由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'*** -[NSURL initFileURLWithPath:]: nil string parameter' 并感谢您的帮助,但我之前尝试过这种代码,这是控制台信息 将您的视频添加到copy bundle resources【参考方案4】:

基于@Dinesh 的回答,您的 URL 创建不正确。的价值

[NSURL URLWithString:@"map.mp4"]

将为零,因为 @"map.mp4" 不是有效的 URL。要从应用程序包中创建一个有效的 url,请按照 Dinesh 的说明进行操作。要创建远程 URL,您可以执行以下操作

[NSURL URLWithString:@"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"]

URL 应该包含它的所有部分:协议、主机、路径、文件。

干杯,费利佩

【讨论】:

谢谢,但实际上我想使用本地 mp4 文件,而不是流,[NSBundle mainBundle] 似乎可行。 我很高兴它对你有用。请考虑将答案授予 Dinesh。这对您也是最好的,因为不授予它会损害您的声誉。【参考方案5】:

在主窗口上添加您的电影播放器​​控制器视图,例如:

 MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview: player.view];

[player play];

希望它会起作用!

【讨论】:

【参考方案6】:

你的播放器应该是你的视图控制器的一个属性,像这样:

.h:

@property(nonatomic,weak)MPMoviePlayerController *moviePlayer;

.m:

    MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];  // player's frame must match parent's
    //set the player to your property
    self.moviePlayer=player;

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

【讨论】:

【参考方案7】:

请使用 MPMoviePlayerViewController 因为 MP4 文件。当您使用 MOV 时,工作完美!

MPMoviePlayerViewController *moviePlayerViewController;

-(void)PlayVideoController:(NSString*)videoUrl1  
    NSURL *fileURL = [NSURL URLWithString:videoUrl1]; 
    moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];

    // Register for the playback finished notification.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];

    //Present
    [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];

    // Play the movie!
    moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [moviePlayerViewController.moviePlayer play];


-(void)myMovieFinishedCallback:(NSNotification*)aNotification 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
    [moviePlayerViewController release], moviePlayerViewController = nil;

【讨论】:

【参考方案8】:

您可能需要交叉检查视频网址是否有空格。以下代码对我有用。

- (IBAction)btnPlayVideo:(id)sender 

    self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL    *fileURL    =   [NSURL URLWithString:[NSString stringWithFormat:@"%@", self.strVideoUrl]];
    NSLog(@"Url to play = %@", self.strVideoUrl);

        self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlaybackComplete:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:self.moviePlayerController];
        [self.moviePlayerController.view setFrame:self.view.bounds];
        [self.view addSubview:self.moviePlayerController.view];
        self.moviePlayerController.shouldAutoplay = YES;
        self.moviePlayerController.fullscreen = YES;
        self.moviePlayerController.controlStyle=NO;
        [self.moviePlayerController prepareToPlay];
        self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;

        [self.moviePlayerController play];



- (void)moviePlaybackComplete:(NSNotification *)notification

    self.moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.moviePlayerController];

    [self.moviePlayerController.view removeFromSuperview];
    self.closeVideAdProp.hidden = NO;
    btnCloseAd.hidden=FALSE;

【讨论】:

以上是关于MPMoviePlayerController 给了我一个黑色的空视图,没有视频播放的主要内容,如果未能解决你的问题,请参考以下文章

MPMoviePlayercontroller 在 iphone SDK 4 中不起作用? - 需要帮助

MPMoviePlayerViewController和MPMoviePlayerController的使用

MPMoviePlayerController 不播放 .wmv

如何显示 MPMoviePlayerController 控件?

MPMoviePlayerController 隐藏 AirPlay 按钮

MPMoviePlayerController 上的专辑封面