1.介绍
播放视频文件。
2.iOS9.0以前使用
2.1框架
#import <MediaPlayer/MediaPlayer.h>
控制器类 :MPMoviePlayerViewController
播放视图类:MPMoviePlayerController
2.1 MPMoviePlayerViewController 视频弹出控制器方式
//第一种弹出控制器方式
- (void)loadMovie{
//1.创建URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video1.mp4" withExtension:nil];
//2.创建视频播放控制器
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.创建URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video1.mp4" withExtension:nil];
//2.创建视频播放控制器
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视频播放完成的通知
// 监听播放完成的通知
- (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.创建播放器
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
//3.创建播放显示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.创建控制器
AVPlayerViewController *pvc = [[AVPlayerViewController alloc] init];
//2.获得视频的URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video2.mp4" withExtension:nil];
//3.创建播放类
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
//4.对播放控制器赋值
pvc.player = player;
//5.弹出控制器
[self presentViewController:pvc animated:YES completion:nil];
//6.开始播放 注意:player必须手动调用, 才会播放视频
[pvc.player play];
}
网友评论