iOS:如何使用 MPMoviePlayerController [英] iOS: How to use MPMoviePlayerController

查看:17
本文介绍了iOS:如何使用 MPMoviePlayerController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个空白项目 (iOS) 并将其放入我的 viewDidLoad:

I've created a blank project (iOS) and put this in my viewDidLoad:

NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer play];

当应用程序启动时,我得到的只是一个白屏,日志中有错误消息:

When the app starts all I get is a white screen with error messages in the log:

 <Error>: CGContextSaveGState: invalid context 0x0
 <Error>: CGContextClipToRect: invalid context 0x0
 <Error>: CGContextTranslateCTM: invalid context 0x0
 <Error>: CGContextDrawShading: invalid context 0x0
 <Error>: CGContextRestoreGState: invalid context 0x0
Warning: Attempt to present <MPMoviePlayerViewController: 0x821e3b0> on <ViewController: 0x863aa40> whose view is not in the window hierarchy!

...还有一堆关于禁用自动播放的行.我特别不明白关于视图不属于层次结构的那一行,因为它是一个空白的单一视图应用程序"iOS 项目,代码在 ViewController.m 中.它位于视图层次结构中.

...and a bunch of lines regarding disabling autoplay. I especially don't understand the line about the view not being part of the hierarchy since it's a blank "Single View Application" iOS project and the code is in ViewController.m. It IS in the view hierarchy.

我知道电影文件本身不是问题,因为我是从 Apple 在 MPMoviePlayer 上的示例代码中得到的.尽管我(似乎)尝试了示例中编写的所有内容,但我还是无法让播放器正常工作.

I know for a fact that the movie file itself is not the problem because I got it from Apple's sample code on MPMoviePlayer. And although I (seemingly) tried everything written in the sample, I just couldn't get the player to work.

这是另一个尝试,这次使用 MPMoviePlayerController(不是 MPMoviePlayerViewController):

Here is another try, this time with MPMoviePlayerController (not MPMoviePlayerViewController):

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player setContentURL:url];
[player setMovieSourceType:MPMovieSourceTypeFile];

[[player view] setFrame:self.view.bounds];
[player view].backgroundColor = [UIColor greenColor];

player.scalingMode = MPMovieScalingModeNone;
player.controlStyle = MPMovieControlModeDefault;
player.backgroundView.backgroundColor = [UIColor whiteColor];
player.repeatMode = MPMovieRepeatModeNone;

[self.view addSubview: [player view]];
[player play];

类似的结果,有白屏和错误.请帮忙....

Similar result, with white screen and errors. Please help....

推荐答案

原来我们要做的就是:

NSURL *movieURL = [NSURL URLWithString:@"http://example.com/somefile.mp4"];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer play];

  • movieController 是在 .h 文件中声明的 MPMoviePlayerViewController 的一个实例.

    • movieController is an instance of MPMoviePlayerViewController declared in the .h file.

      重要:在定义 URL 时,如果您想通过网络访问文件并使用 NSURL 的 fileUrlWithPath 方法,请使用 NSURL 的 URLWithString 方法> 如果您在本地拥有该文件!

      Important: when defining the URL use NSURL's URLWithString method if you want to access the file through a network and use NSURL's fileUrlWithPath if you have the file locally!

      [movieController.movi​​ePlayer play] 不是必需的,无论您没有将自动播放设置为否,播放器都会启动,但我观察到如果你把 play 放在里面,它开始得更快一点.这可能只是巧合.

      [movieController.moviePlayer play] is not required and the player will start regardless if you didn't set autoplay to NO, but I observed that if you put play in it it starts a bit quicker. This could be just a coincidence.

      如果您想知道用户何时点击完成按钮(播放器将自动关闭),您应该知道 -viewDidAppear 在当玩家被解散时出现的视图控制器.您可以在播放器启动时设置 BOOL 变量并检查 -viewDidAppear 中的 BOOL 以便您知道 -viewDidAppear 被调用是因为玩家被解雇了.或者,您可以注册 MPMoviePlayerDidExitFullScreen 通知,但这对我不起作用.

      If you want to know when the user tapped the done button (the player will be dismissed automatically) you should know that -viewDidAppear is called on the view controller that appears when the player is dismissed. You could set a BOOL variable when the player starts and check for the BOOL in your -viewDidAppear so that you know that -viewDidAppear was called becaouse the player was dismissed. Alternatively you can register for MPMoviePlayerDidExitFullScreen notification, but that didn't work for me.

      或者,如果这不起作用,您可以执行以下操作

      self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"something" ofType:@"mp4"]]];
      [self.moviePlayer.view setFrame:CGRectMake(0, 0, 320, 320)];
      [self.moviePlayer play];
      [self.view addSubview:self.moviePlayer.view];
      

      • self.movi​​eplayer 是 MPMoviePlayerController(不是 MPMoviePlayerViewController)的一个实例.根据我的经验,将它声明为一个属性很重要(像这样:@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;)而不是一个简单的 ivar,因为有时它只是不工作,如果它是一个 ivar

        • self.movieplayer is an instance of MPMoviePlayerController (not MPMoviePlayerViewController). In my experience it's important to declare it as a property (like so: @property (strong, nonatomic) MPMoviePlayerController *moviePlayer;) rather than a simple ivar, because sometimes it just doesn't work if it's an ivar

          设置frame也很重要,因为如果我们不设置,视频根本就不会出现.框架可以是任何东西,只要您定义的内容在您的视图范围内

          setting the frame is also important, because if we don't set it, the video will not appear at all. The frame can be anything as long as what you define is within the bounds of your view

          重要:如上所述,如果您想通过网络访问文件,请在定义 URL 时使用 NSURL 的 URLWithString 方法并使用 NSURL 的 fileUrlWithPath 如果你有文件本地

          Important: As above, when defining the URL use NSURL's URLWithString method if you want to access the file through a network and use NSURL's fileUrlWithPath if you have the file locally!

          这篇关于iOS:如何使用 MPMoviePlayerController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆