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. Java知识点汇总

    Java中泛型的本质 Java中静态变量的适用场景 Java类加载原理及类加载器 Java中对Clone的理解 Java中HashMap的实现 Java中Collection和Collections的 ...

  2. 使用nagios插件 check_mysql_health 过程中遇到的error

    使用nagios插件 check_mysql_health 过程中遇到的error 1.如果在运行监控mysql插件的时候遇到了error安装以下依赖包就可以解决: yum install perl- ...

  3. c#实现QQ群成员列表导出及邮件群发之模拟QQ登陆

    主题已迁移至:http://atiblogs.com/ ITO-神奇的程序员

  4. eclipse配置项目部署到到本地tomcat

    一.发现问题 在eclipse中新建Dynamic Web Project,配置好本地的tomcat并写好代码后选择Run on Server,但运行后发现在tomcat的安装目录下的webapps并 ...

  5. fragment用法

    简单用法: 1.新建布局.新建fragment类 2.在activity_main.xml中添加fragment <LinearLayout...... <fragment android ...

  6. 事务之四:Spring事务--原理

    一.Spring事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的.对于纯JDBC操作数据库,想要用到事务,可以按照以下步骤进行: ...

  7. 1074 Reversing Linked List

    题意: 每k个元素反转链表,不足k个就不反转.如原链表为1→2→3→4→5→6,k=3,则反转后的链表为3→2→1→6→5→4:若k=4,则反转后的链表为4→3→2→1→5→6. 思路: 这题会比较烦 ...

  8. 文件操作方法大全以及文件打开的其他一些模式sys.stdout.write()就是标准输出到你当前的屏幕 sys.stdout.flush()把内存立即显示到您当前的屏幕

    read()会让你读取的光标变成最后.tell()把你现在文件的句柄的指针打印出来.文件的开头指针位置是0f.read(5)只读取5个字符串个数如果你想把光标移回去,移动到首位f.seek(0)f.d ...

  9. USB设备驱动总结

    现象:把USB设备接到PC        (韦老师总结) 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱动程序 问1. 既然还没有" ...

  10. day1--心得

    info = ''' --------------------info of %s---------------- name: %s age: %s job: %s ----------------- ...