视频播放器

AVPlayerLayer

  #import 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"喵星人" ofType:@"mp4"];
    //创建播放本地视频的播放器对象
    AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
    
    //使用播放器对象创建用于显示的AVPlayerLayer
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = self.view.frame;
    [self.view.layer addSublayer:playerLayer];
    [player play];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        //当前在第60秒,每秒24帧
        CMTime t = CMTimeMakeWithSeconds(60, 24);
        [player seekToTime:t];
        //获取当前播放时间
        NSLog(@"------>%f", CMTimeGetSeconds(player.currentTime));
        //音量,但是一般用MPVolumeView来控制
        player.volume = 1.0;
        //静音
        player.muted = YES;
    });
    
    CMTime time = CMTimeMake(2, 2);//每隔2/2秒调用一次
    //周期性调用
    [player addPeriodicTimeObserverForInterval:time queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        NSLog(@"------>%f", CMTimeGetSeconds(player.currentTime));
    }];
    //观察播放状态
    [player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionInitial context:NULL];

AVPlayerViewController

#import 

    AVPlayerViewController *playerViewController = [AVPlayerViewController new];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"喵星人" ofType:@"mp4"];
    AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
    playerViewController.player = player;
    [self presentViewController:playerViewController animated:YES completion:nil];

第三方框架

#import 
    //需导入AudioToolbox、lib包文件
    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
    [options setPlayerOptionIntValue:1  forKey:@"videotoolbox"];
    // 帧速率(fps) (可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97)
    [options setPlayerOptionIntValue:29.97 forKey:@"r"];
    // -vol——设置音量大小,256为标准音量。(要设置成两倍音量时则输入512,依此类推
    [options setPlayerOptionIntValue:512 forKey:@"vol"];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"喵星人" ofType:@"mp4"];
    
    IJKFFMoviePlayerController *playerController = [[IJKFFMoviePlayerController alloc] initWithContentURLString:path withOptions:options];
    
    playerController.scalingMode = IJKMPMovieScalingModeFill;
    playerController.shouldAutoplay = YES;
    playerController.shouldShowHudView = YES;
    playerController.view.frame = self.view.frame;
    [self.view addSubview:playerController.view];
    [playerController prepareToPlay];
    [playerController play];

你可能感兴趣的:(视频播放器)