视频播放
1> AVPlayer
能播放本地、远程的音频、视频文件
基于Layer显示,得自己去编写控制面板2> MPMoviePlayerController
能播放本地、远程的音频、视频文件
自带播放控制面板(暂停、播放、播放进度、是否要全屏)3> MPMoviePlayerViewController
能播放本地、远程的音频、视频文件
内部是封装了MPMoviePlayerController
播放界面默认就是全屏的
如果播放功能比较简单,仅仅是简单地播放远程、本地的视频文件,建议用这个
区别
1.AVPlayer(远程⾳音乐/播放视频)—>添加layer
2.MPMoviePlayerController—>给.view设置frame,将这个view 添加到某⼀一个View
3.MPMoviePlayerViewController(modal出来,⾃自动播放,⼀一定全 屏播放)
1:AVPlayer
- 可以播放远程视频(需要通过添加layer来显示视频)
#pragma mark - 懒加载代码
- (AVPlayer *)player
{
if (_player == nil) {
// 1.获取URL(远程/本地)
// NSURL *url = [[NSBundle mainBundle] URLForResource:@"01-知识回顾.mp4" withExtension:nil];
NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
// 2.创建AVPlayerItem
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
// 3.创建AVPlayer
_player = [AVPlayer playerWithPlayerItem:item];
// 4.添加AVPlayerLayer
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
[self.view.layer addSublayer:layer];
}
return _player;
}
2:MPMoviePlayerController
#import <MediaPlayer/MediaPlayer.h>
#pragma mark - 懒加载代码
- (MPMoviePlayerController *)playerController
{
if (_playerController == nil) {
// 1.获取视频的URL
NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];
// 2.创建控制器
_playerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
// 3.设置控制器的View的位置
_playerController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
// 4.将View添加到控制器上
[self.view addSubview:_playerController.view];
// 5.设置属性
_playerController.controlStyle = MPMovieControlStyleNone;
}
return _playerController;
}
3:MPMoviePlayerViewController
#import <MediaPlayer/MediaPlayer.h>
#pragma mark - 懒加载
- (MPMoviePlayerViewController *)playerVc
{
if (_playerVc == nil) {
NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
_playerVc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];;
}
return _playerVc;
}
- (IBAction)play {
[self presentMoviePlayerViewControllerAnimated:self.playerVc];
}