本文主要是介绍AVPlayer 播放在线视频和本地文件(数据库)设置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先使用AVPlayer 必须导入框架:
AVFoundation 以及 头文件:
#<MediaPlayer/MediaPlayer.h>
两者的主要区别在路径的初始化:
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];
这篇关于AVPlayer 播放在线视频和本地文件(数据库)设置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!