iOS play video-阿里云开发者社区

开发者社区> 桃子红了呐> 正文

iOS play video


iOS: How to use MPMoviePlayerController

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 0x0Warning:Attempt to present <MPMoviePlayerViewController:0x821e3b0> on <ViewController:0x863aa40> whose view is not in the window hierarchy!

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

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.

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

share|improve this question
  Try putting it in viewDidAppear. – H2CO3 Oct 10 '12 at 15:03
  Putting it in viewDidAppear solved the Context errors, thanks, but I still do not get the player, just a white screen – Zoltán Matók Oct 10 '12 at 15:15
  Matok funny how two people with the same mother tongue discuss stuff using a foreign language. :P – H2CO3 Oct 10 '12 at 15:18
Yeah, since I have no knowledge of any quality hun forums where these kind of questions could be asked I just use SO. And I don't want to use hungarian even now, so as not to confuse the others reading this thread. :D – Zoltán Matók Oct 10 '12 at 15:24

2 Answers

up vote19down voteaccepted

Turns out all we have to do is this:

NSURL *movieURL =[NSURL URLWithString:@""];
movieController =[[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];[self presentMoviePlayerViewControllerAnimated:movieController];[movieController.moviePlayer play];
  • movieController is an instance of MPMoviePlayerViewController declared in the .h file.

  • 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] 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.

  • 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 forMPMoviePlayerDidExitFullScreen notification, but that didn't work for me.

--------- EDIT: OR, if this is not working you can just do that following ----------

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

  • 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 filelocally!

share|improve this answer
  I have tried out this code and I only see the video when i add it to subview like this: [self.view addSubview:_movieController.view]; But for some reason, the top scrubber is a few pixels further below than usual from the status bar. Is there any way to rectify this? And i get these errors as shown in this… What am I doing wrong? – vnchopra Jul 16 at 1:17
  hey @vnchopra, I updated my answer with a solution that works best for me nowadays! Hope it helps – Zoltán Matók Jul 16 at 14:23
Thanks Zoltan!! – vnchopra Jul 16 at 23:30
fileUrlWithPath FTW. Nice – KiwiBastard Jul 29 at 2:43
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!



在应用中,有时会遇到用户询问如何使单台云服务器具备多个公网IP的问题。 具体如何操作呢,有了NAT网关这个也不是难题。
25948 0
2646 0
10239 0
11696 0
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
11723 0
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
11251 0
windows server 2008阿里云ECS服务器安全设置
最近我们Sinesafe安全公司在为客户使用阿里云ecs服务器做安全的过程中,发现服务器基础安全性都没有做。为了为站长们提供更加有效的安全基础解决方案,我们Sinesafe将对阿里云服务器win2008 系统进行基础安全部署实战过程! 比较重要的几部分 1.
8174 0
阿里云安全组设置详细图文教程(收藏起来) 阿里云服务器安全组设置规则分享,阿里云服务器安全组如何放行端口设置教程。阿里云会要求客户设置安全组,如果不设置,阿里云会指定默认的安全组。那么,这个安全组是什么呢?顾名思义,就是为了服务器安全设置的。安全组其实就是一个虚拟的防火墙,可以让用户从端口、IP的维度来筛选对应服务器的访问者,从而形成一个云上的安全域。
6667 0