iOS - AVPlayer播放视频,获取视频尺寸(宽高)

版权声明:本文为博主原创文章,欢迎转载,但是请注明原地址 https://blog.csdn.net/icefishlily/article/details/74544435

公司启动页要求播放视频,其实很简单,AVPlayer可以胜任此任务。

- (void)playVideoWithType:(NSString *)type {
    //获取视频size
    NSURL *mediaFileUrl;
    if ([type isEqualToString:@"default"]) {
        NSString *defaultVideoPath = [[NSBundle mainBundle] pathForResource:@"defaultVideo.mp4" ofType:nil];
        mediaFileUrl = [NSURL fileURLWithPath:defaultVideoPath];
    } else {
        NSString *filePath = [[ResourceManager sharedManager] resourceFilePathWithCategory:ResourceCategoryStartup];
        //通过移动该文件对文件重命名
        NSString *filePath2= [filePath stringByAppendingString:@"video.mp4"];
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        //判断是否移动
        if (![fileMgr fileExistsAtPath:filePath2]) {
            NSError *error;
            if ([fileMgr copyItemAtPath:filePath toPath:filePath2 error:&error] != YES) {
                NSLog(@"reName faild!");
                [self.timer invalidate];
                self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(clickSkipButton) userInfo:nil repeats:NO];
                return;
            }
        }
        mediaFileUrl = [NSURL fileURLWithPath:filePath2];
    }

    //获取视频尺寸
    AVURLAsset *asset = [AVURLAsset assetWithURL:mediaFileUrl];
    NSArray *array = asset.tracks;
    CGSize videoSize = CGSizeZero;
    
    for (AVAssetTrack *track in array) {
        if ([track.mediaType isEqualToString:AVMediaTypeVideo]) {
            videoSize = track.naturalSize;
        }
    }
    
    if (videoSize.width == 0) {
        [self.timer invalidate];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(clickSkipButton) userInfo:nil repeats:NO];
        return;
    }
    
    self.videoView.frame = [self caculateRealRectWithResourceRect:videoSize];

    //创建视频并将视频放入videoView的layer中
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset: asset];
    self.player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    playerLayer.frame = self.videoView.frame;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.videoView.layer addSublayer:playerLayer];
    
    //为视频设置 播放结束、播放未到达结尾、播放抛锚 的消息处理
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemFailedToPlayToEndTimeNotification object:self.player.currentItem];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemPlaybackStalledNotification object:self.player.currentItem];
    
    //播放视频
    [self.player play];
}

//视频结束处理
-(void)playbackFinished:(NSNotification *)notification{
    if (self.player) {
        [self.player pause];
    }
}


阅读更多
换一批

没有更多推荐了,返回首页