video.gif
在實際應(yīng)用中,除了聽音樂
紧帕、照相之外
见秽,視頻服務(wù)
可以說是必不可少的愉烙!
MPMoviePlayerController
類是iOS提供的一個封裝性強(qiáng)
、功能性強(qiáng)大
的視頻播放類解取。
自帶播放視頻的視圖
步责,還有一個播放控制欄
,可以播放
禀苦、暫停
勺择、拖動播放進(jìn)度
、結(jié)束播放
伦忠、全屏播放
等省核。
一、MPMoviePlayerController常用屬性
MPMoviePlayerController支持全屏播放
昆码、嵌入視圖播放
和無視圖播放
气忠,這些播放樣式是通過它的controlStyle屬性
設(shè)置的。
- MPMoviePlayerController常用屬性表:
屬性聲明 | 功能描述 |
---|---|
@property (nonatomic) MPMovieSourceType movieSourceType; | 設(shè)置赋咽、獲取文件類型旧噪,是本地文件還是流媒體 |
@property (nonatomic, copy) NSURL *contentURL; | 設(shè)置、獲取播放內(nèi)容的地址[如果在播放過程中改變了內(nèi)容地址脓匿,則暫停當(dāng)前內(nèi)容淘钟,從頭開始播放新內(nèi)容]
|
@property (nonatomic) MPMovieControlStyle controlStyle; | 設(shè)置、獲取播放器的樣式陪毡,全屏米母、嵌入視圖、無視圖 |
@property (nonatomic) MPMovieScalingMode scalingMode; | 設(shè)置毡琉、獲取視頻播放的縮放模式 |
- controlStyle:
typedef NS_ENUM(NSInteger, MPMovieControlStyle) {
MPMovieControlStyleNone, // 不顯示視圖
MPMovieControlStyleEmbedded, // 播放器顯示在一個嵌入的視圖中
MPMovieControlStyleFullscreen, // 播放器全屏顯示
MPMovieControlStyleDefault = MPMovieControlStyleEmbedded
} MP_DEPRECATED("Use AVPlayerViewController in AVKit.", ios(3.2, 9.0)) MP_PROHIBITED(tvos);
- scalingMode
typedef NS_ENUM(NSInteger, MPMovieScalingMode) {
MPMovieScalingModeNone, // 不做縮放的處理
MPMovieScalingModeAspectFit, // 保持寬高比铁瞒,適應(yīng)屏幕大小
MPMovieScalingModeAspectFill, // 保持寬高比,適應(yīng)屏幕大小桅滋,讓畫面充滿整個屏幕
MPMovieScalingModeFill // 畫面充滿整個屏幕慧耍,不保持寬高比
} MP_DEPRECATED("Use AVPlayerViewController in AVKit.", ios(2.0, 9.0)) MP_PROHIBITED(tvos);
二身辨、筆者的簡單示例
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
/** 播放器*/
@property (nonatomic, strong) MPMoviePlayerController *MPPlayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupToPlay];
}
- (void)setupToPlay{
//根據(jù)URL創(chuàng)建播放器
// NSURL *url = [[NSBundle mainBundle] URLForResource:@"testMovie.mp4" withExtension:nil];
NSURL *url = [NSURL URLWithString:@"http://localhost/Videos/testMovie.mp4"];//127.0.0.1
MPMoviePlayerController *MPPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
//設(shè)置播放器的視圖
[self.view addSubview:MPPlayer.view];
MPPlayer.view.frame = self.view.bounds;
self.MPPlayer = MPPlayer;
//適配播放器View,實現(xiàn)旋轉(zhuǎn)
[self.MPPlayer.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view]-0-|" options:0 metrics:nil views:@{@"view":self.MPPlayer.view}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[view]-0-|" options:0 metrics:nil views:@{@"view":self.MPPlayer.view}]];
//準(zhǔn)備播放
[self.MPPlayer prepareToPlay];
[self.MPPlayer play];
}
補(bǔ)充:
這里我的url創(chuàng)建方式有兩種芍碧,
本地資源:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"testMovie.mp4" withExtension:nil];
網(wǎng)絡(luò)資源:
NSURL *url = [NSURL URLWithString:@"http://localhost/Videos/testMovie.mp4"];//127.0.0.1
這里我的視頻資源放在了我電腦搭建的服務(wù)器上面煌珊,Mac上如何搭建Apache服務(wù)器,這里有傳送門哦泌豆!