AVPlayer 播放在线视频和本地文件(数据库)设置

首先使用AVPlayer 必须导入框架:
AVFoundation 以及 头文件:
#
两者的主要区别在路径的初始化:

1、播放本地视频(数据库数据)

     titleLabel.text = [NSString stringWithFormat:@"%@",currentMusicModel.audio_name];
        [img sd_setImageWithURL:[NSURL URLWithString:currentMusicModel.image] placeholderImage:[UIImage imageNamed:@"default_172"]];
        [bgImg sd_setImageWithURL:[NSURL URLWithString:currentMusicModel.image] placeholderImage:[UIImage imageNamed:@"default_172"]];

        //读取数据库音频资源
        NSString *sqlstr = [NSString stringWithFormat:@"where down_audioid = '%@'",musicid];
        // 这里执行创建数据库,以后的shareDatabase系列都属于获取当前的数据库引用
        JQFMDB *db = [JQFMDB shareDatabase:SQL_NAME path:SQL_PATH];
        NSArray *voicelist = [db jq_lookupTable:SQL_VOICE dicOrModel:[HGSQLModel class] whereFormat:sqlstr];
        NSLog(@"voicelist--%@",voicelist);

        [voicelist enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            sqlModel = (HGSQLModel *)obj;

        }];
        if (voicelist.count > 0) {

            //读取某个文件
            NSString *filePath = [SQL_PATH stringByAppendingString:sqlModel.down_filename];

            NSURL *sourceMovieUrl = [NSURL fileURLWithPath:filePath];

            AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieUrl options:nil];
            playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];

        }
        [self.player replaceCurrentItemWithPlayerItem:playerItem];
        [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];// 监听status属性
        [self monitoringPlayback:playerItem];// 监听播放状态

2、播放在线视频

 //使用playerItem获取视频的信息,当前播放时间,总时间等
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURLURLWithString:@"在线视频路径"]];
    //player是视频播放的控制器,可以用来快进播放,暂停等
    AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer = [AVPlayerLayerplayerLayerWithPlayer:_LGCustomMoviePlayerController.player];
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    //调用一下setter方法
    [_LGCustomMoviePlayerController setPlayer:player];
    [_LGCustomMoviePlayerController.player play];

你可能感兴趣的:(ios-进阶)