MPMoviePlayerController用于管理视频的播放,视频可以来自于本地文件或者是网络数据流。视频可以全屏或者内置在moviePlayer拥有的view中播放。你可以在你的app中在一个view包含视频播放的view,或者使用MPMoviePlayerViewController来管理。
默认支持AirPlay
当我们将MPMoviePlayerController加入到视图中时,需要合理设置frame
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: myURL]; [player prepareToPlay] [player.view setFrame: myView.bounds]; // player's frame must match parent's [myView addSubview: player.view]; // ... [player play];
(1)添加自定义子视图
将moviePlayer认为是一个封闭的结构,你可以在movie的Layer content上添加自定义的子试图但是绝不要修改它已经存在的子试图。 (view)
除了在movie上添加子视图,可以在backgroundView上添加子控件。在内置和全屏模式下,自定义的控件都被支持。但是必须调整两种情况下的位置。可以使用通知来判断和调整。(backgroundView)
Use the MPMoviePlayerWillEnterFullscreenNotification and MPMoviePlayerWillExitFullscreenNotification notifications to detect changes to and from fullscreen mode
MPMoviePlayerController支持代码控制,也提供了供用户使用的按钮。你可以使用MPMoviePlayerController实现的协议
MPMediaPlayback 中的方法用代码来控制视频播放的大部分操作。该协议中的方法能让你开始,停止,根据当前播放的内容向前或者向后,甚至改变播放的速率。controlStyle属性能够展示一些系统提供的标准的控制按钮来控制视频的播放。同时你也可以设置shouldAutoplay属性来决定是否自动播放网络视频文件。
当你创建了一个MPMoviePlayerController的时候,你指定了要播放的视频文件。但是,你可以通过设置contentUrl属性来改变正在播放的视频。改变这个属性使你重新利用了同一个movie Player
NOTE
Although you can create multiple MPMoviePlayerController objects and present their views in your interface, only one movie player at a time can play its movie.
为长视频创建video书签和章节链接提供便利。MPMoviePlayerController提供了在特定时间生成缩略图的方法。可以使用
thumbnailImageAtTime:timeOption:方法来创建单张缩略图或者用requestThumbnailImagesAtTimes:timeOption:
来创建多张缩略图。
To play a network stream whose URL requires access credentials, first create an appropriate NSURLCredential object. Do this by calling, for example, the initWithUser:password:persistence: method, as shown here:
NSURLCredential *credential = [[NSURLCredential alloc]
initWithUser: @"userName"
password: @"password"
persistence: NSURLCredentialPersistenceForSession];
self.credential = credential;
[credential release];
In addition, create an appropriate NSURLProtectionSpace object, as shown here. Make appropriate modifications for the realm you are accessing:
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost: "@streams.mydomain.com"
port: 80
protocol: @"http"
realm: @"mydomain.com"
authenticationMethod: NSURLAuthenticationMethodDefault];
self.protectionSpace = protectionSpace;
[protectionSpace release];
Add the URL credential and the protection space to the Singleton NSURLCredentialStorage object. Do this by calling, for example, the setCredential:forProtectionSpace: method, as shown here:
[[NSURLCredentialStorage sharedCredentialStorage]
setDefaultCredential: credential
forProtectionSpace: protectionSpace];
With the credential and protection space information in place, you can then play the protected stream.
一 Movie Player Notifications
MPMoviePlayerController是通过通知而非我们常见的代理来通知视频播放的状态的。以下情况会发送通知
视频开始播放,暂停,快进,快退(四个)
AirPlay播放开始,结束(两个)
视屏的scaling mode改变了
视频进入全屏或者退出全屏
网络视频的load state改变了
网络视频的meta-information可以获得了
二 支持的格式
.mov, .mp4, .mpv, and .3g
H.264 Baseline Profile Level 3.0 video, up to 640 x 480 at 30 fps. (The Baseline profile does not support B frames.)
MPEG-4 Part 2 video (Simple Profile)
If you use this class to play audio files, it displays a white screen with a QuickTime logo while the audio plays. For audio files, this class supports AAC-LC audio at up to 48 kHz, and MP3 (MPEG-1 Audio Layer 3) up to 48 kHz, stereo audio.