【问题标题】:Playing a video in iOS app: audio but no picture在 iOS 应用中播放视频:有音频但没有图片
【发布时间】:2011-07-17 07:28:18
【问题描述】:

我想在我的 iphone 应用程序中播放一段短视频。当我使用下面的代码时,我只听到音频并看到 应用程序的常规视图。我希望视频在此视图之上播放。 我该怎么办?

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"LEADER" ofType:@"mov"];
    NSURL  *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    theMovie.scalingMode = MPMovieScalingModeAspectFill;
    [theMovie play];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];

【问题讨论】:

    标签: iphone video audio


    【解决方案1】:

    不要混淆MPMoviePlayerControllerMPMoviePlayerViewController。当您使用 MPMoviePlayerController 时,请像这样使用它(通常用于 iPad 上的嵌入式视频):

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

    当您使用 MPMoviePlayerViewController 时,然后使用 presentMoviePlayerViewControllerAnimated: 呈现视频(通常用于全屏视频)。

    【讨论】:

    • 感谢您的回答。尝试用您的建议替换 MPMoviePlayerController 部分,但到处都出现错误...
    【解决方案2】:
    MPMoviePlayerController *player =
            [[MPMoviePlayerController alloc] initWithContentURL: myURL];
    [player.view.frame = self.view.frame];
    [self.view addSubview: player.view];
    // ...
    [player play];
    

    【讨论】:

      【解决方案3】:

      唯一对我有用的魔法是

       - (void) playMovie {
          NSURL *url = [NSURL URLWithString: 
              @"http://www.example.com/video.mp4"];
          MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] 
              initWithContentURL:url];
      
          self.mc = controller; //Super important
          controller.view.frame = self.view.bounds; //Set the size
      
          [self.view addSubview:controller.view]; //Show the view
          [controller play]; //Start playing
      }
      

      在头文件中

      @property (nonatomic,strong) MPMoviePlayerController* mc;
      

      More Details

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多