1. 带View的播放器
- (IBAction)moviePlayerViewController:(id)sender {
// 带View的播放器的控制器
//1. 获取URL地址
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Cupid_高清.mp4" withExtension:nil];
//2. 创建带View的播放器
MPMoviePlayerViewController *mpVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//3. 模态视图弹出 --> 模态视图的切换应该在View完全展示之后进行
[self presentViewController:mpVC animated:YES completion:nil];
}
2. 不带View的播放器
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
@interface ViewController ()
@property (nonatomic, strong) MPMoviePlayerController *mpC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//检测视频播放完毕 --> 可以连续播放视频
//注册通知监测视频播放完毕
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
#pragma mark 通知绑定的方法
- (void)moviePlayerPlaybackDidFinishNotification:(NSNotification *)notification
{
/**
MPMovieFinishReasonPlaybackEnded, 播放结束
MPMovieFinishReasonPlaybackError, 播放错误
MPMovieFinishReasonUserExited 退出播放
*/
//1. 获取通知结束的状态
NSInteger movieFinishKey = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
//2. 根据状态不同来自行填写逻辑代码
switch (movieFinishKey) {
case MPMovieFinishReasonPlaybackEnded:
NSLog(@"播放结束");
// 进行视频切换 需要两步
//1. 要想换视频, 就需要更换地址
self.mpC.contentURL = [[NSBundle mainBundle] URLForResource:@"Alizee_La_Isla_Bonita.mp4" withExtension:nil];
//
[self.mpC play];
break;
case MPMovieFinishReasonPlaybackError:
NSLog(@"播放错误");
break;
case MPMovieFinishReasonUserExited:
NSLog(@"退出播放");
// 如果是不带view的播放器, 那么播放完毕(退出/错误/结束)都应该退出
[self.mpC.view removeFromSuperview];
break;
default:
break;
}
}
- (void)dealloc
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (IBAction)moviePlayerController
{
// 不带View的播放器的控制器 --> 需要强引用, 设置frame, 添加到view上, 开始播放
//1. 获取URL地址
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Cupid_高清.mp4" withExtension:nil];
//2. 创建不带View的播放器
self.mpC = [[MPMoviePlayerController alloc] initWithContentURL:url];
//3. 设置view.frame
self.mpC.view.frame = CGRectMake(0, 0, 300, 400);
//4. 添加到view上
[self.view addSubview:self.mpC.view];
//5. 准备播放 --> 规范写法, 要写上. 调用play方法时, 会自动调用此方法
[self.mpC prepareToPlay];
//6. 开始播放
[self.mpC play];
//7. 控制模式
self.mpC.controlStyle = MPMovieControlStyleFullscreen;
/**
MPMovieControlStyleNone, // No controls
MPMovieControlStyleEmbedded, // 嵌入式的控制 -- 默认
MPMovieControlStyleFullscreen, // 全屏时的控制样式
*/
}
@end
3. iOS9 播放视频
#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@implementation ViewController
- (IBAction)playerViewController {
//1. 获取URL地址
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Cupid_高清.mp4" withExtension:nil];
//2. AV播放视图控制器
AVPlayerViewController *pVC = [AVPlayerViewController new];
//3. 创建player --> 设置时需要传入网址
pVC.player = [AVPlayer playerWithURL:url];
//4. 开始播放
[pVC.player play];
//5. 模态弹出
//[self presentViewController:pVC animated:YES completion:nil];
//5. 如果想要自定义播放器的大小,应该自定义 --> 设置frame / 添加到视图中
pVC.view.frame = CGRectMake(40, 200, 300, 400);
[self.view addSubview:pVC.view];
}
@end
4. 视频截图
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
#pragma mark 点击屏幕, 开始截图
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1. URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Cupid_高清.mp4" withExtension:nil];
//2. 获取资源
AVAsset *asset = [AVAsset assetWithURL:url];
//3. 创建 资源图像生成器
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
//4. 开始生成图像
//Times : 用来表示影片的时间的值
//value: 帧数
//timescale: 当前视频的每秒的帧数
CMTime time = CMTimeMake(60, 1);
NSValue *value = [NSValue valueWithCMTime:time];
[imageGenerator generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
//5. 主线程中更新UI
dispatch_sync(dispatch_get_main_queue(), ^{
self.imageView.image = [UIImage imageWithCGImage:image];
});
}];
}
@end
6. 视频录制
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, strong) MPMoviePlayerController *mpC;
@end
@implementation ViewController
- (IBAction)movieClick:(id)sender {
//1. 判断是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
return;
}
//2. 创建图像选择器
UIImagePickerController *picker = [UIImagePickerController new];
//3. 设置类型
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//4. 设置媒体类型
picker.mediaTypes = @[(NSString *)kUTTypeMovie];
//5. 设置相机检测模式
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
//6. 设置视频的质量
picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
//7. 设置代理
picker.delegate = self;
//8. 模态弹出
[self presentViewController:picker animated:YES completion:nil];
}
//UIImagePickerController 代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
NSLog(@"info: %@",info);
//1. 获取媒体类型
NSString *mediaTyep = info[UIImagePickerControllerMediaType];
//2. 判断是否是视频的媒体类型
id url = info[UIImagePickerControllerMediaURL];
if ([mediaTyep isEqualToString:(NSString *)kUTTypeMovie]) {
if (self.mpC == nil) {
self.mpC = [MPMoviePlayerController new];
self.mpC.view.frame = self.view.bounds;
[self.view addSubview:self.mpC.view];
}
self.mpC.contentURL = url;
[self.mpC play];
}
//3. 保存视频
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//3.1 创建ALAssetsLibrary对象
ALAssetsLibrary *assetsLibrary = [ALAssetsLibrary new];
//3.2 调用writeVideoAtPathToSavedPhotosAlbum即可
//前面的URL, 需要传入要保存的视频的URL.
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:url completionBlock:nil];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end
7. 视频压缩
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1. 判断是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
return;
}
//2. 创建图像选择器
UIImagePickerController *picker = [UIImagePickerController new];
//3. 设置类型
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//4. 设置媒体类型
picker.mediaTypes = @[(NSString *)kUTTypeMovie];
//5. 设置代理
picker.delegate = self;
//6. 模态弹出
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark 选中视频的时候, 进行压缩处理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//1. 获取媒体类型
NSString *mediaTyep = info[UIImagePickerControllerMediaType];
//2. 获取视频的地址
id url = info[UIImagePickerControllerMediaURL];
//3. 开始导出--> 压缩
[self exportWithURL:url];
}
- (void)exportWithURL:(NSURL *)url
{
//1. 获取资源
AVAsset *asset = [AVAsset assetWithURL:url];
//2. 根据资源, 创建资源导出会话对象
//presetName : 压缩的大小
AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
//3. 设置导出路径
session.outputURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"12345.mov"]];
//4. 设置导出类型
session.outputFileType = AVFileTypeQuickTimeMovie;
//5. 开始导出
[session exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"当你看到这句话的时候, 恭喜你已经导出成功");
}];
}
@end