iOS 使用 MPMoviePlayerController 播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17578965/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:18:53  来源:igfitidea点击:

iOS play video with MPMoviePlayerController

iphoneiosios5ios6ios6.1

提问by krackmoe

I got this piece of code:

我得到了这段代码:

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:@"/Resources/disc.mp4"]];
    theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
    theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
    UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
    [theMoviPlayer.view setFrame:backgroundWindow.frame];
    [backgroundWindow addSubview:theMoviPlayer.view];
    [theMoviPlayer play];

But i really dont know how to add the video to my project. In which folder do i have to put the video file!? Or do i have to do something else to add it to my project?

但我真的不知道如何将视频添加到我的项目中。我必须将视频文件放在哪个文件夹中!?还是我必须做其他事情才能将其添加到我的项目中?

EDIT:

编辑:

It looks like this in xcode, is it correct? Because i do get a playback error right now. Previously i used an url to play this video and this worked quite well, but with this file locally not :(

在 xcode 中看起来像这样,对吗?因为我现在确实遇到播放错误。以前我使用一个 url 来播放这个视频,效果很好,但是这个文件在本地不是 :(

enter image description here

在此处输入图片说明

回答by Apollo SOFTWARE

Ok your bundle path looks Hymaned, below should work.

好的,你的包路径看起来被劫持了,下面应该可以工作。

NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];

回答by Mohit

Add MediaPlayer Framework

添加媒体播放器框架

import it to your file

将其导入您的文件

#import <MediaPlayer/MediaPlayer.h>

Create an object of MPMoviePlayerController

创建一个 MPMoviePlayerController 对象

MPMoviePlayerController * moviePlayer;

write this code where you wants to play video

在您想播放视频的地方编写此代码

NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"spacetest.mp4" ofType:nil];
NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
[moviePlayer play];

回答by Apollo SOFTWARE

Using HTML5 as I promised above:

正如我上面承诺的那样使用 HTML5:

    NSString *videoTitle = @"disc.mp4";
    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSString *playPath = [NSString stringWithFormat:@"<center><video  width=\"640\" height=\"480\" controls><source src=\"%@\" media=\"all and (max-width:1024px)\"></video></center>",videoTitle];


    [webView loadHTMLString:playPath baseURL:baseURL];

This will play in 640x480, but if you are familiar with HTML5 video tags, you can customize pretty heavily.

这将以 640x480 播放,但如果您熟悉 HTML5 视频标签,则可以进行大量自定义。

回答by Apollo SOFTWARE

Since you are using MPMoviePlayerController and not UIWebView you can place your mp4 or file in your Resources and XCode/iOS will find it. Be sure that the directory/group that the file is under is yellow not blue. You don't want it to be a relative path.

由于您使用的是 MPMoviePlayerController 而不是 UIWebView,您可以将您的 mp4 或文件放在您的资源中,XCode/iOS 会找到它。确保文件所在的目录/组是黄色而不是蓝色。您不希望它成为相对路径。

Just drag the resource into your project. Copy items into destination is selected, First option of Folders is selected, and most importantly, add to target!

只需将资源拖到您的项目中即可。选择复制项目到目标,选择文件夹的第一个选项,最重要的是,添加到目标!

Ok try the code below:

好的,试试下面的代码:

NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];