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

查看:78
本文介绍了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....

推荐答案

原来我们要做的就是这样:

Turns out all we have to do is this:

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的URLWithString方法;如果您在本地有文件,请使用NSURL的fileUrlWithPath

      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.moviePlayer 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

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

          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

          重要:如上所述,如果要通过网络访问文件 并使用NSURL的URLWithString方法. >如果您具有本地文件

          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天全站免登陆