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

asked Oct 10 '12 at 14:59
 
 
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
1  
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

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

answered Oct 18 '12 at 9:06
 
 
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 question.stackoverflow.com/questions/12822420/… 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
1  
Thanks Zoltan!! – vnchopra Jul 16 at 23:30
1  
fileUrlWithPath FTW. Nice – KiwiBastard Jul 29 at 2:43

iOS play video的更多相关文章

  1. ios 安卓 video 取消播放自动全屏 属性

    x-webkit-airplay="true",x5-playsinline="true",webkit-playsinline="true" ...

  2. 10个优秀的Objective-C和iOS开发在线视频教程

    如果你自己开发iOS应用,你肯定会发现网上有很多资源.学习编程的一个最好的方法就是自己写代码,而开始写代码的最快的方式就是看其他人怎么写.我们从海量视频和学习网站中整理出了我 如果你自己开发iOS应用 ...

  3. 微信iOS WKWebview 网页开发适配指南

    微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配. 背景 WKWebView 是苹果在iOS 8中引入的新组件,目的是提供一个现代的 ...

  4. iOS苹果官方Demo合集

    Mirror of Apple’s iOS samples This repository mirrors Apple’s iOS samples. Name Topic Framework Desc ...

  5. video 标签在微信浏览器的问题解决方法

    最近做的些web页面,内嵌许多小视频,在ios和安卓手机上播放时,遇到不少问题: 在微信浏览器内播放时,视频会自动全屏 如果每个视频都有一张自定义的图片作为封面,在显示视频的同时,如果没有给这个视频设 ...

  6. iOS WKWebview 网页开发适配指南【转】

    微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们咨询. 背景 WKWebView 是苹果在iO ...

  7. 10个优秀Objective-C和iOS开发在线视频教程

    如果你自己开发iOS应用,你肯定会发现网上有很多资源.学习编程的一个最好的方法就是自己写代码,而开始写代码的最快的方式就是看其他人怎么写.我们从海量视频和学习网站中整理出了我们认为对你学习Object ...

  8. IOS 视频直播/智能家居(一行行敲代码,从零开始)lesson:1整体架构

    本文转载至 http://blog.csdn.net/u014011807/article/details/47144027 前段时间由于工作需要做了一个视频直播/智能家居类的应用.算是对iOS音视频 ...

  9. iOS WKWebview 网页开发适配指南

    iOS WKWebview 网页开发适配指南 微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们 ...

随机推荐

  1. weblogic控制台的启动与禁用

    在一些安全漏洞扫描中,经常会扫描发现,使用weblogic管理控制台,会有个中危的漏洞. http://192.168.10.46:7001/console/login/LoginForm.jsp W ...

  2. centos安装PHP-pdo支持pdo

    #yum install php-pdo phpinfo()查看PDO

  3. laravel 中config的使用

    在laravel的config中添加配置文件(比如:alipay.php)文件内容为return数组的形式 在方法中使用config()函数获取数据 $config = config('alipay. ...

  4. Oracle 数据库备份还原(Expdp/impdp)记录

    最近公司将原数据库服务器切换.之前没整过这块,也是一堆的度娘.经过不停的摸索,终于成功了.现在将这份艰辛记录下来,方便自己以后查阅的同时,方便有类似需求的同学参考. 我们此次切换共分:ERP.LOS. ...

  5. 浅谈FPGA的选型

    工欲善其事必先利其器,开发FPGA的第一步,当然是选择一片符合设计需求的芯片. 器件特色 选片第一个关注的应该是FPGA器件的专用资源. 例如是否需要高速接口,如果需要的话,需要多少个通道,各个通道需 ...

  6. Vue.js:起步

    ylbtech-Vue.js:起步 1.返回顶部 1. Vue.js 起步 每个 Vue 应用都需要通过实例化 Vue 来实现. 语法格式如下: var vm = new Vue({ // 选项 }) ...

  7. PHP定时任务Crontab结合CLI模式详解

    从版本 4.3.0 开始,PHP 提供了一种新类型的 CLI SAPI(Server Application Programming Interface,服务端应用编程端口)支持,名为 CLI,意为 ...

  8. Box2D学习blog

    http://www.ladeng6666.com/blog/category/box2d/

  9. jQuery中获取相对文档的坐标的方法是什么?

    offset(),获取匹配元素在当前视口的相对偏移.返回的对象包含两个整型属性:top 和 left,以像素计.此方法只对可见元素有效. position()获取匹配元素相对定位父级的偏移.没有定位父 ...

  10. 网站漏洞扫描并自动化工具-XAttacker

    注:该脚本根据网站的cms类型来扫描,所以推荐用来扫外国的站 运行脚本 ┌─[root@sch01ar]─[/sch01ar/XAttacker] └──╼ #perl XAttacker.pl 询问 ...