iOS 视频AVFoundation使用

来源:互联网 发布:java入门什么书 编辑:程序博客网 时间:2024/04/29 18:18

iOS9弃用了MPMoviePlayerViewController 使用了AVPlayerViewViewController 框架

其中:
1.AVPlayer 播放音视频
2.AVPlayerItem 音频的对象
3.AVPlayerLayer 播放显示视频的图层界面
4.AVPlayerViewController 调节控件
流程:
AVPlayer(视频播放器) ->去播放AVPlayerItem视频播放的元素 ->展示播放的视图AVPlayerLayer方法:

//监听播放状态[AVPlayerItemaddObserver:selfforKeyPath:@"status"options:NSKeyValueObservingOptionNewcontext:nil];

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context

{

    //    NSLog(@"%@",change[@"new"]);

    switch ([change[@"new"]integerValue]) {

        case0:{

            NSLog(@"未知状态");

            break;

        }

        case1:{

            NSLog(@"获得视频总时长  %f",CMTimeGetSeconds(player.currentItem.duration));

            break;

        }

        case2:{

            NSLog(@"加载失败");

            break;

        }

        default:

            break;

    }

}

记得

removeObserver

//监听结束[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(moviePlayDidEnd:)name:AVPlayerItemDidPlayToEndTimeNotificationobject:player.currentItem];

//开始暂停进度调整等都提供了API

视频截图

#pragma mark -截取视频-

- (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL {

    

    AVURLAsset *asset = [[AVURLAssetalloc] initWithURL:videoURLoptions:nil];

    

    AVAssetImageGenerator *gen = [[AVAssetImageGeneratoralloc] initWithAsset:asset];

    

    gen.appliesPreferredTrackTransform =YES;

    

    CMTime time =CMTimeMakeWithSeconds(2.0,600);

    

    NSError *error =nil;

    

    CMTime actualTime;

    

    CGImageRef image = [gencopyCGImageAtTime:time actualTime:&actualTimeerror:&error];

    

    UIImage *thumbImg = [[UIImagealloc] initWithCGImage:image];

    

    return thumbImg;

    

}


0 0
原创粉丝点击