#import <AVFoundation/AVFoundation.h> /** 播放器 */ @property (nonatomic, strong) AVPlayer *player; #pragma mark - 懒加载代码 - (AVPlayer *)player { if (_player == nil) { NSURL *url = [NSURL URLWithString:@"http://cc.stream.qqmusic.qq.com/C100003j8IiV1X8Oaw.m4a?fromtag=52"]; // _player = [[AVPlayer alloc] initWithURL:url]; AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url]; _player = [[AVPlayer alloc] initWithPlayerItem:item]; } return _player; } // 播放音频 [self.player play]; // 更换音频 // self.player replaceCurrentItemWithPlayerItem:<#(nullable AVPlayerItem *)#>
#pragma mark - 懒加载代码 - (AVPlayer *)player { if (_player == nil) { // 1.创建URL NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"]; // 本地视频 // NSURL *url = [[NSBundle mainBundle] URLForResource:@"xxx.mp4" withExtension:nil]; // 2.创建AVPlayer对象 AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url]; _player = [[AVPlayer alloc] initWithPlayerItem:item]; // 3.创建播放layer AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_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; }
iOS提供了MPMoviePlayerController、MPMoviePlayerViewController两个类,可以用来轻松播放视频和网络流媒体/网络音频
提示:网络音频同样使用此控制器播放
YouTobe就是用MPMoviePlayerController实现的
MPMoviePlayerViewController只能全屏播放视频
上述两个类都定义在了MediaPlayer框架中
1、MPMoviePlayerController
#import <MediaPlayer/MediaPlayer.h> #import <AVKit/AVKit.h> /** 播放器 */ @property (nonatomic, strong) MPMoviePlayerController *player; #pragma mark - 懒加载代码 - (MPMoviePlayerController *)player { if (_player == nil) { // 1.获取视频的URL NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/3e35cbb0-c8e5-4827-9614-b5a355259010/L.mp4"]; // 2.创建播放器 _player = [[MPMoviePlayerController alloc] initWithContentURL:url]; // 3.给播放器的View设置frame,并且添加到view中 _player.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16); [self.view addSubview:_player.view]; } return _player; }
2、MPMoviePlayerViewController
/** 播放器 */ @property (nonatomic, strong) MPMoviePlayerViewController *playerVc; - (MPMoviePlayerViewController *)playerVc { if (_playerVc == nil) { NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"]; _playerVc = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; } return _playerVc; } // 只能全屏播放 // 只能modal出视频 [self presentMoviePlayerViewControllerAnimated:self.playerVc];
MPMoviePlayerController
继承自NSObject
内部有个view可以展示视频内容
将该视图添加其他控制器的view上,即可显示视频内容
MPMoviePlayerController可以播放的视频格式包括:
H.264、MPEG-4等
支持的文件扩展名包括:avi,mkv,mov,m4v,mp4等
可以从苹果官网:http://support.apple.com/kb/HT1425下载一些用来测试的视频文件,文件都比较小
提示:MPMoviePlayerController并不支持所有的视频格式,如果要播放不支持的视频格式,需要借助第三方框架进行解码,如VLC
https://github.com/videolan/vlc
加载视频资源(注意,如果url为nil同样可以加载)
NSAssert(self.url, @"URL不能为空");
[[MPMoviePlayerController alloc] initWithContentURL:self.url];
显示
[self.view addSubview:self.moviePlayer.view];
通过设置AutoresizingMask属性可以在横竖屏转换时自动调整视图大小
播放
[self.moviePlayer play];
全屏
[self.moviePlayer setFullscreen:YES animated:YES];
MPMoviePlayerController的播放状态是通过通知中心监听的
状态变化
MPMoviePlayerPlaybackStateDidChangeNotification
播放结束
MPMoviePlayerPlaybackDidFinishNotification
退出全屏
MPMoviePlayerDidExitFullscreenNotification
截屏完成
MPMoviePlayerThumbnailImageRequestDidFinishNotification
截屏方法
-requestThumbnailImagesAtTimes:timeOption:
原文:http://www.cnblogs.com/chglog/p/4870509.html