ios 如何使用 MPMoviePlayerController 播放视频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10814308/
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
How to play video using MPMoviePlayerController?
提问by Perseus
I am using the following code to play the video using the MPMoviePlayerController , but the video is not played. Can anyone tell me why ?
我使用以下代码使用 MPMoviePlayerController 播放视频,但未播放视频。谁能告诉我为什么?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
[[moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [moviePlayer view]];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[moviePlayer play];
回答by Matt Long
It's pretty weird, but it seems to work okay if you make your MPMoviePlayerController a property instead of a local variable. Seems something is going on behind the scenes. I'm thinking it's related to ARC. Are you using ARC?
这很奇怪,但是如果您将 MPMoviePlayerController 设为属性而不是局部变量,它似乎可以正常工作。似乎在幕后发生了一些事情。我认为这与ARC有关。你在使用ARC吗?
It's also an issue that you've over-appended your path:
您过度附加路径也是一个问题:
// You've already got the full path to the documents directory here.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];
// Now you're appending the full path to the documents directory to your bundle path
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
When I run your code in the simulator, the path looks like this:
当我在模拟器中运行您的代码时,路径如下所示:
/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B9 -442C-A525-573FE343506D/Documents/one.mp4
It should just be this:
应该只是这样:
/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
So just delete this line:
所以只需删除这一行:
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
And then change your player instantiation to this:
然后将您的播放器实例化更改为:
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
[[_moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [_moviePlayer view]];
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_moviePlayer play];
So you should add the MPMoviePlayerController as a property of your containing view controller.
因此,您应该将 MPMoviePlayerController 添加为包含视图控制器的属性。
回答by Fran Sevillano
All right, there is a big difference between the app bundle and the documents directory. I suggest you take a look at that.
好吧,应用程序包和文档目录之间有很大的区别。我建议你看一看这一点。
First of all, Where is the video stored?
首先,视频存储在哪里?
If your video is in the documents directory, don't append the documents directory path to the bundle path.
如果您的视频在文档目录中,请不要将文档目录路径附加到包路径中。
Just try with the filePath
variable:
只需尝试使用filePath
变量:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL filePath]];
However, if the file is in the app bundle (you added it to your project in XCode), you should use what is in jinx
response.
但是,如果该文件在应用程序包中(您在 XCode 中将其添加到您的项目中),您应该使用jinx
响应中的内容。
回答by NANNAV
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer.view setFrame:CGRectMake(//set rect frame)];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay=YES;
moviePlayer.repeatMode = NO;
[moviePlayer setFullscreen:YES animated:NO];
[moviePlayer prepareToPlay];
[self.view addsubview:movieplayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {
if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
//add your code
}
}
回答by pbx
Try asking your bundle directly and not setting up the file path manually
尝试直接询问您的包,而不是手动设置文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"];
回答by masterbinoy
Player is initialized with the URL of the video which we want to be played (It can be either a path of local file of a device or a live URL.) after that player is added as a sub view of current view.
Player 被初始化为我们想要播放的视频的 URL(它可以是设备本地文件的路径或实时 URL。)在该播放器作为当前视图的子视图添加后。
Supported video formats by MPMoviePlayerController class are following
MPMoviePlayerController 类支持的视频格式如下
- .mov .mpv .3gp .mp4
- .mov .mpv .3gp .mp4
I am not sure how much you this article would help you out. I am new to this. I worked on the step by step instructions provided in this article
我不确定这篇文章对你有多大帮助。我是新来的。我按照本文提供的分步说明进行了工作
回答by Ayaz
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
hope Help it
希望帮助它
回答by Waleed Mahmood
It took me few mins to debug the problem but the answer is quite simple. Here is the deal:
我花了几分钟来调试问题,但答案很简单。这是交易:
If you want to MPMoviePlayerViewController to play from a web URL use this: NSURL *url = [NSURL URLWithString:@"https://www.test.com/anyMovie.mp4"];
And if you want MPMoviePlayerViewController to play it from App Bundle use this: NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"anyMovie" ofType:@"m4v"]; NSURL *url = [NSURL fileURLWithPath:moviePath];
如果您希望 MPMoviePlayerViewController 从 Web URL 播放,请使用: NSURL *url = [NSURL URLWithString:@" https://www.test.com/anyMovie.mp4"];
如果您希望 MPMoviePlayerViewController 从 App Bundle 播放它,请使用: NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"anyMovie" ofType:@"m4v"]; NSURL *url = [NSURL fileURLWithPath:moviePath];
The Rest of it goes the same, except you have to set this property "movieSourceType" as follows:
其余部分相同,除了您必须按如下方式设置此属性“movieSourceType”:
MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentViewController:moviePlayerView animated:YES completion:^{}];