iOS play video

简介:

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

up vote 19 down vote accepted

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!

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 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
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!
相关文章
|
27天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
83 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
38 0
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
90 2
|
3月前
|
API 开发工具 iOS开发
iOS 开发高效率工具包:10 大必备工具
iOS 开发高效率工具包:10 大必备工具
42 1
|
3月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
52 1
|
2天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
8 0
|
17天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
181 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4