iOS:如何使用 MPMoviePlayerController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12822420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: How to use MPMoviePlayerController
提问by Zoltán Matók
I've created a blank project (iOS) and put this in my viewDidLoad:
我创建了一个空白项目(iOS)并将其放在我的 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!
...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.
...还有一堆关于禁用自动播放的行。我特别不明白关于视图不属于层次结构的那一行,因为它是一个空白的“单视图应用程序”iOS 项目,代码在 ViewController.m 中。它在视图层次结构中。
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.
我知道电影文件本身不是问题,因为我是从 Apple 在 MPMoviePlayer 上的示例代码中得到的。尽管我(似乎)尝试了示例中编写的所有内容,但我还是无法让播放器正常工作。
Here is another try, this time with MPMoviePlayerController (not MPMoviePlayerViewController):
这是另一个尝试,这次使用 MPMoviePlayerController(不是 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....
类似的结果,白屏和错误。请帮忙....
回答by Zoltán Matók
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 ofMPMoviePlayerViewController
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'sfileUrlWithPath
if you have the file locally![movieController.moviePlayer play]
is not requiredand the player will start regardless if you didn't set autoplay to NO, but I observed that if you putplay
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 aBOOL
variable when the player starts and check for theBOOL
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.
movieController
是MPMoviePlayerViewController
在 .h 文件中声明的实例。重要提示:在定义 URL 时,
URLWithString
如果您想通过网络访问文件,请使用 NSURL 的方法,fileUrlWithPath
如果您在本地拥有该文件,请使用 NSURL 的方法![movieController.moviePlayer play]
不是必需的,无论您没有将自动播放设置为否,播放器都会启动,但我观察到,如果您将play
其放入,它会启动得更快一些。这可能只是巧合。如果您想知道用户何时点击完成按钮(播放器将自动关闭),您应该知道
-viewDidAppear
在播放器关闭时出现的视图控制器上调用。您可以BOOL
在玩家开始时设置一个变量并检查BOOL
您的 ,-viewDidAppear
以便您知道因为-viewDidAppear
玩家被解雇而被调用。或者,您可以注册MPMoviePlayerDidExitFullScreen
通知,但这对我不起作用。
OR, if this is not working you can just do the 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 ivarsetting 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 networkand use NSURL'sfileUrlWithPath
if you have the file locally!
self.movieplayer 是MPMoviePlayerController(不是 MPMoviePlayerViewController)的一个实例。根据我的经验,将其声明为属性(如
@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
:)而不是简单的 ivar 很重要,因为有时如果它是 ivar,它就不起作用设置frame也很重要,因为如果我们不设置它,视频根本就不会出现。框架可以是任何东西,只要您定义的内容在您的视图范围内
重要提示:如上,定义 URL 时,
URLWithString
如果您想通过网络访问文件,请使用 NSURL 的方法,fileUrlWithPath
如果您在本地拥有该文件,请使用 NSURL 的方法!
回答by Parameswar Vellachu
You have to try this......
你必须试试这个......
NSString *path = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"mp4"];
player = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
[player.view setFrame:CGRectMake(0, 0, 320, 320)];
[self presentMoviePlayerViewControllerAnimated:player];
[player.moviePlayer play];
[self.view addSubview:player.view];
回答by Rakesh Kumar
Try this:
尝试这个:
//Hello guise :- complete process here.
//1. add framework in .h
MediaPlayer/MediaPlayer.h
//2.add delegate in @interface, MPMediaPickerControllerDelegate
//3.@property (retain, nonatomic) MPMoviePlayerViewController *moviePlayerController;
//4.and synthesize in .m - @synthesize moviePlayerController;
//5.IN - tableViewdelegateMethod
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{
//given path for song
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
NSString *strPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory1,[movieArray objectAtIndex:indexPath.row]];
NSLog(@"strPath %@",strPath);
//path for song.
//convert path in NSURL.
NSURL *videosURL = [NSURL fileURLWithPath:strPath];
moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:videosURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer play];
}
//play song on UITableViewCell click.
回答by Mikhail Viceman
Try add
尝试添加
[player prepareToPlay];
after
后
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
You can read more about MPMoviePlayerController in Apple documentation: http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html#//apple_ref/doc/c_ref/MPMoviePlayerController
您可以在 Apple 文档中阅读有关 MPMoviePlayerController 的更多信息:http: //developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html#//apple_ref/doc/c_ref/MPMoviePlayerController