介绍
OhosVideoCache是一个支持边播放边缓存的库,只需要将音视频的url传递给OhosVideoCache处理之后再设置给播放器, OhosVideoCache就可以一边下载音视频数据并保存在本地,一边读取本地缓存返回给播放器,使用者无需进行其他操作。
效果图预览

使用说明:
- 进入页面,自动播放视频,点击暂停,视频停止播放,点击播放,视频继续播放。
- 视频播放完成之后,点击播放按钮,视频重新播放。
实现思路
边缓存播放功能主要是通过Xcomponent组件、@ohos.multimedia.media以及OhosVideoCache三方库实现,XComponent组件主要用于绘制 视频播放的窗口,页面进来初始化服务器然后利用XComponent组件的onLoad函数来调用VideoPlayerManager 中的initPlayer方法创建一个音视频管理实例,并通过setAVPlayerCallback函数和cacheAndPlayVideo函数来实现视频状态的监听以及边缓存边播放功能。
1.XComponent组件绘制视频播放窗口。
XComponent({
id: 'xcomponent',
type: 'surface',
controller: this.componentController
})
.height(`${
this.xComponentHeight}px`)
.width(`${
this.xComponentWidth}px`)
.onLoad(() => {
// 设置XComponent持有Surface的宽度和高度
this.componentController.setXComponentSurfaceSize({
surfaceWidth: SURFACE_WIDTH,
surfaceHeight: SURFACE_HEIGHT
});
this.surfaceId = this.componentController.getXComponentSurfaceId();
// 创建音视频播放实例
AvPlayManager.getInstance()
.initPlayer(getContext(this) as common.UIAbilityContext, this.surfaceId, (avPlayer: media.AVPlayer) => {
avPlayer.on('timeUpdate', (time: number) => {
this.currentTime = time;
});
this.videoDuration = handleTime(avPlayer.duration

最低0.47元/天 解锁文章
649

被折叠的 条评论
为什么被折叠?



