iPhone SDK:如何在视图中播放视频? 而不是全屏
我试图在 UIView
中播放视频,所以我的第一步是为该视图添加一个类,并使用以下代码开始在其中播放电影:
- (IBAction)movie:(id)sender{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
}
但这只会在使用此方法时使应用程序崩溃在它自己的班级内,但在其他地方很好。 有谁知道如何在视图中播放视频? 并避免全屏显示?
I am trying to play video inside a UIView
, so my first step was to add a class for that view and start playing a movie in it using this code:
- (IBAction)movie:(id)sender{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
}
But this just crashes the app when using this method inside it's own class, but is fine elsewhere. Does anyone know how to play video inside a view? and avoid it being full screen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
从 3.2 SDK 开始,您可以访问 MPMoviePlayerController 的 view 属性,修改其框架并将其添加到视图层次结构中。
这里有一个例子: http://www.devx.com/wireless/Article/44642/ 1954年
As of the 3.2 SDK you can access the view property of
MPMoviePlayerController
, modify its frame and add it to your view hierarchy.There's an example here: http://www.devx.com/wireless/Article/44642/1954
最好的方法是使用视图中的层:
不要忘记添加框架:
The best way is to use layers insted of views:
Don't forget to add frameworks:
Swift
这是一个独立的项目,因此您可以在上下文中查看所有内容。
布局
使用
UIView
和UIButton
创建如下所示的布局。UIView
将是我们将在其中播放视频的容器。将视频添加到项目中
如果您需要示例视频来练习,可以从 sample-videos.com。 我在本例中使用 mp4 格式的视频。 将视频文件拖放到您的项目中。 我还必须将其明确添加到捆绑资源中(转到构建阶段>复制捆绑资源,请参阅这个答案了解更多)。
代码
这是该项目的完整代码。
注释
AVPlayerItem
。AVFoundation
和AVPlayer
,那么你必须构建自己的所有控件。 如果你想全屏播放视频,可以使用AVPlayerViewController
。 您需要为此导入 AVKit。 它配有一整套用于暂停、快进、快退、停止等的控件。此处< /a> 和此处是一些视频教程。MPMoviePlayerController
已被弃用。结果
该项目现在应该如下所示。
Swift
This is a self contained project so that you can see everything in context.
Layout
Create a layout like the following with a
UIView
and aUIButton
. TheUIView
will be the container in which we will play our video.Add a video to the project
If you need a sample video to practice with, you can get one from sample-videos.com. I'm using an mp4 format video in this example. Drag and drop the video file into your project. I also had to add it explicitly into the bundle resources (go to Build Phases > Copy Bundle Resources, see this answer for more).
Code
Here is the complete code for the project.
Notes
AVPlayerItem
.AVFoundation
andAVPlayer
, then you have to build all of your own controls. If you want full screen video playback, you can useAVPlayerViewController
. You will need to importAVKit
for that. It comes with a full set of controls for pause, fast forward, rewind, stop, etc. Here and here are some video tutorials.MPMoviePlayerController
that you may have seen in other answers is deprecated.Result
The project should look like this now.
查看您的代码,您需要设置电影播放器控制器视图的框架,并将电影播放器控制器的视图添加到您的视图中。 另外,不要忘记将 MediaPlayer.framework 添加到您的目标中。
这是一些示例代码:
Looking at your code, you need to set the frame of the movie player controller's view, and also add the movie player controller's view to your view. Also, don't forget to add MediaPlayer.framework to your target.
Here's some sample code:
斯威夫特版本:
Swift version:
使用以下方法。
self.imageView_VedioContainer
是AVPlayer
的容器视图。Use the following method.
self.imageView_VedioContainer
is the container view of yourAVPlayer
.您无法在视图内播放视频。 必须全屏播放。
You cannot play a video inside a view. It has to be played fullscreen.