您的位置:首页 > 其它

视频播放–AVPlayer

2016-08-18 18:09 239 查看

视频播放–AVPlayer

能播放本地、远程的音频、视频文件

基于Layer显示,需要自己编写控制面板

#import <AVFoundation/AVFoundation.h>

-(AVPlayer *)player
{
if(_palyer == nil){
// 1.获取本地URL方式一
NSString *filePath = [NSBundle mainBundle] pathForResource:@"welcome" ofType:@"mp4"];
NSString *fileUrl = [NSURL fileURLWithPath:thePath];
// 2.获取本地URL方式二
NSURL *url = [[NSBundle mainBundle] URLForResource:@"welcome.mp4" wihtExtension:nil];
// 3.获取远程URL方式
NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];

// 4.创建AVPlayerItem
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];

// 5.创建AVPlayer
_player = [AVPlayer playerWithPlayerItem:item];

// 6.添加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;
}


监听播放状态结束

[[NSNotificationCenter defaultCenter] addObserver: selector:@selector(回调方法) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  AVPlayer