【问题标题】:MPMoviePlayerController gives me a black empty view,no video playingMPMoviePlayerController 给了我一个黑色的空视图,没有视频播放
【发布时间】: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];

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

【问题讨论】:

    标签: ios mpmovieplayercontroller


    【解决方案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;
            }
            

            【讨论】:

              猜你喜欢
              • 2012-11-29
              • 2013-10-10
              • 2012-05-11
              • 1970-01-01
              • 1970-01-01
              • 2023-03-23
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多