iOS开发视频播放器 AVPlayer

释放双眼,带上耳机,听听看~!
{
// 设置音频播放
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
//创建播放器
CGRect playerFrame = CGRectMake(0, 0, _backView.layer.bounds.size.width, _backView.layer.bounds.size.height);
AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:_patch]];
_playerItem = [AVPlayerItem playerItemWithAsset:asset];
_player = [[AVPlayer alloc]initWithPlayerItem:_playerItem];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
_playerLayer.frame = playerFrame;
_playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[_backView.layer addSublayer:_playerLayer];
}

[_player pause];//暂停
[_player play];//播放

/**
图片抓帧

@param videoURL 网络地址
@return 返回图片
*/
– (UIImage )pk_previewImageWithVideoURL:(NSURL )videoURL {
AVURLAsset *asset = [AVURLAsset assetWithURL:videoURL];
// AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode =AVAssetImageGeneratorApertureModeEncodedPixels;

CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = 1;
NSError *thumbnailImageGenerationError = nil;
thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];
if(!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage:thumbnailImageRef] : nil;
return thumbnailImage;

关于图片抓帧

观察者模式监听

//增加观察者
-(void)addObserverToPlayerItem:(AVPlayerItem *)playerItem{
//监控状态属性,注意AVPlayer也有一个status属性,通过监控它的status也可以获得播放状态
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
//监控网络加载情况属性
[playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
}
//移除观察这
-(void)removeObserverFromPlayerItem:(AVPlayerItem *)playerItem{
[playerItem removeObserver:self forKeyPath:@"status"];
[playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
}
/** * 通过KVO监控播放器状态 *
* @param keyPath 监控属性
* @param object 监视器
* @param change 状态改变
* @param context 上下文 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
AVPlayerItem *playerItem=object;
if ([keyPath isEqualToString:@"status"]) {
AVPlayerStatus status= [[change objectForKey:@"new"] intValue];
if(status==AVPlayerStatusReadyToPlay){
[self hideHud];
NSLog(@"正在播放...,视频总长度:%.2f",CMTimeGetSeconds(playerItem.duration));
}
}
else if([keyPath isEqualToString:@"loadedTimeRanges"])
{
[self hideHud];
NSArray *array=playerItem.loadedTimeRanges;
CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];//本次缓冲时间范围
float startSeconds = CMTimeGetSeconds(timeRange.start);
float durationSeconds = CMTimeGetSeconds(timeRange.duration);
NSTimeInterval totalBuffer = startSeconds + durationSeconds;//缓冲总长度
NSLog(@"共缓冲:%.2f",totalBuffer);
}
}

人已赞赏
iOS文章

IOS 时间定时器

2020-4-29 8:48:37

iOS文章

iOS开发tableView

2020-4-29 10:03:03

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索