当前位置: 首页 > 工具软件 > PVS.AVPlayer > 使用案例 >

视频播放器 AVPlayer

姬安志
2023-12-01
{
    // 设置音频播放
    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);
    }
}
 类似资料: