1.介紹
播放視頻文件。
2.iOS9.0以前使用
2.1框架
#import <MediaPlayer/MediaPlayer.h>
控制器類 :MPMoviePlayerViewController
播放視圖類:MPMoviePlayerController
2.1 MPMoviePlayerViewController 視頻彈出控制器方式
//第一種彈出控制器方式
- (void)loadMovie{
//1.創(chuàng)建URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video1.mp4" withExtension:nil];
//2.創(chuàng)建視頻播放控制器
MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//3.彈出視頻播放控制器
[self presentMoviePlayerViewControllerAnimated:vc];
//4.視頻播放器代碼退出 -- 在視頻播放完之后退出
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//讓窗口消失
[self dismissMoviePlayerViewControllerAnimated];
});
}
//第二種彈出控制器方式
- (void)loadMovie{
//1.創(chuàng)建URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video1.mp4" withExtension:nil];
//2.創(chuàng)建視頻播放控制器
MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//3.彈出視頻播放控制器
[self presentViewController:vc animated:YES completion:nil];
//4.視頻播放器代碼退出 -- 在視頻播放完之后退出
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//讓窗口消失
[self dismissViewControllerAnimated:YES completion:nil];
});
}
3.2 MPMoviePlayerController 在某個View中播放
- (void)loadMovie{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video4.mp4" withExtension:nil];
self.mpc = [[MPMoviePlayerController alloc] initWithContentURL:url];
// 注意: 需要配置frame值, 否則有可能看不到
self.mpc.view.frame = self.tempView.bounds;
//將播放視頻View添加當前的某個View上
[self.tempView addSubview:self.mpc.view];
// 播放視頻
[self.mpc play];
}
設置MPMoviePlayerController顯示的一些屬性
- (void)loadMovie{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video4.mp4" withExtension:nil];
self.mpc = [[MPMoviePlayerController alloc] initWithContentURL:url];
// 注意: 需要配置frame值, 否則有可能看不到
self.mpc.view.frame = self.tempView.bounds;
[self.tempView addSubview:self.mpc.view];
//控制欄樣式
/**
MPMovieControlStyleNone, 沒有控制界面
MPMovieControlStyleEmbedded, 嵌入的控制界面
MPMovieControlStyleFullscreen, 全屏的控制界面
*/
self.mpc.controlStyle = MPMovieControlStyleEmbedded; // 默認
//視頻拉伸模式
/**
MPMovieScalingModeNone, 沒有拉伸
MPMovieScalingModeAspectFit, 保持比例拉伸
MPMovieScalingModeAspectFill, 保持比例填充
MPMovieScalingModeFill 拉伸填充
*/
self.mpc.scalingMode = MPMovieScalingModeAspectFit; // 默認
// 視頻準備并且播放
[self.mpc prepareToPlay];
// 視頻播放
// [self.mpc play];
// 視頻暫停
// [self.mpc pause];
// 視頻停止
// [self.mpc stop];
}
2.3視頻播放完成的通知
// 監(jiān)聽播放完成的通知
- (void)registerNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishAction:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
- (void)didFinishAction:(NSNotification *)notification{
NSLog(@"播放完成, %@", notification);
MPMovieFinishReason reason = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
/**
MPMovieFinishReasonPlaybackEnded, 播放結束
MPMovieFinishReasonPlaybackError, 播放完成
MPMovieFinishReasonUserExited 播放退出
*/
if (reason == MPMovieFinishReasonPlaybackEnded) {
//播放結束
}
}
// 移除通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:
MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
3.iOS9.0以后
3.1在View中播放
3.1.1框架
#import <AVFoundation/AVFoundation.h>
類: AVPlayer
3.1.2使用流程
- (void)avplayerLayerTest
{
//1.獲得視頻的URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video2.mp4" withExtension:nil];
//2.創(chuàng)建播放器
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
//3.創(chuàng)建播放顯示Layer
self.layer = [AVPlayerLayer playerLayerWithPlayer:player];
//4.將播放Layer添加到View中
[self.tempView.layer addSublayer:self.layer];
//5.設置顯示layer的frame
self.layer.frame = self.tempView.bounds;
//6.視頻的拉伸樣式
/**
AVLayerVideoGravityResize, more
AVLayerVideoGravityResizeAspect 保持比例
AVLayerVideoGravityResizeAspectFill 拉伸樣式
*/
self.layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
//7.視頻播放
[self.layer.player play];
}
3.1.3視頻播放其它
- (void)vedioPauseOrPlay{
//rate 播放比率
if (self.layer.player.rate == 0.0) {
// 沒有播放
[self.layer.player play];
} else if(self.layer.player.rate == 1.0) {
// 正常播放
[self.layer.player pause];
}
}
3.2控制器播放
3.2.1框架
#import <AVKit/AVKit.h>
類: AVPlayerViewController
3.2.2使用流程
- (void)videoController{
//1.創(chuàng)建控制器
AVPlayerViewController *pvc = [[AVPlayerViewController alloc] init];
//2.獲得視頻的URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video2.mp4" withExtension:nil];
//3.創(chuàng)建播放類
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
//4.對播放控制器賦值
pvc.player = player;
//5.彈出控制器
[self presentViewController:pvc animated:YES completion:nil];
//6.開始播放 注意:player必須手動調用, 才會播放視頻
[pvc.player play];
}