private avplayer: media.AVPlayer = Object()
// 核心1 avplayer 实例 this.avplayer = await media.createAVPlayer()
this.avplayer.on('durationUpdate', (val) => { this.total = val }) this.avplayer.on('stateChange', (state) => { if (state == 'initialized') { //监听播放器状态,当监听都处于初始化状态时,播放器调用prepare()变成就绪态 avplay.prepare() } })
// 核心2 播放地址 if (name.includes('http')) { console.log('qf http') this.avplayer.url = 互联网地址 } else { console.log('qf local') //这里的音频资源是放在rawfile文件夹中的.mp3文件,读者自己设置其他文件资源。 //给播放器设置播放资源,上图有参考资料,使用的是fsSrc资源,不是网络资源->。 const fd = getContext().resourceManager.getRawFdSync(本地rawfile下的名字) this.avplayer.fdSrc = { fd: fd.fd, offset: fd.offset, length: fd.length } }
// 核心3 播放
this.avplayer.play() this.avplayer.pause()