From 5a8b3d314fd7b46b12556f299337f5c6d38a9c8c Mon Sep 17 00:00:00 2001 From: wangzhongjie Date: Mon, 24 Mar 2025 15:29:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0VoiceTag=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=EF=BC=8C=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA=EF=BC=8C=E5=A2=9E=E5=BC=BA=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/config/LogEnum.ets | 5 ++- entry/src/main/ets/utils/Voice.ets | 57 ++++++++++++++++----------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/entry/src/main/ets/config/LogEnum.ets b/entry/src/main/ets/config/LogEnum.ets index 9513281f..3c71f195 100644 --- a/entry/src/main/ets/config/LogEnum.ets +++ b/entry/src/main/ets/config/LogEnum.ets @@ -3,4 +3,7 @@ export const DbTag = '[DBTag]'; export const SerialPortTag = '[SerialPortTag]'; // usb tag -export const UsbTag = '[UsbTag]'; \ No newline at end of file +export const UsbTag = '[UsbTag]'; + +//voice tag +export const VoiceTag = '[VoiceTag]'; \ No newline at end of file diff --git a/entry/src/main/ets/utils/Voice.ets b/entry/src/main/ets/utils/Voice.ets index 18496c2a..d8d24055 100644 --- a/entry/src/main/ets/utils/Voice.ets +++ b/entry/src/main/ets/utils/Voice.ets @@ -1,5 +1,7 @@ import media from '@ohos.multimedia.media'; import { BusinessError } from '@ohos.base'; +import common from '@ohos.app.ability.common'; +import { VoiceTag } from '../config'; type AVPlayerCallback = (status: string, val?: string) => void; @@ -10,7 +12,8 @@ export class voiceService { private playerName: string = ''; private type: number = 1; private endFlag: Boolean = false; - private mediaArray: Array = []; + private mediaArray: Array = []; + private callBack: AVPlayerCallback = null; constructor(callBack: AVPlayerCallback) { // 创建avPlayer实例对象 @@ -24,11 +27,11 @@ export class voiceService { // 注册avplayer回调函数 setAVPlayerCallback(callBack: AVPlayerCallback) { - console.log('jiangsong avPlayerFdSrc setAVPlayerCallback begin') + console.log(VoiceTag, ' avPlayerFdSrc setAVPlayerCallback begin') // error回调监听函数,当avPlayer在操作过程中出现错误时调用reset接口触发重置流程 this.avPlayer.on('error', (err: BusinessError) => { - console.error(`Invoke avPlayer failed, code is ${err.code}, message is ${err.message}`); + console.error(VoiceTag, `Invoke avPlayer failed, code is ${err.code}, message is ${err.message}`); this.avPlayer.reset(); // 调用reset重置资源,触发idle状态 }) @@ -38,7 +41,7 @@ export class voiceService { switch (state) { case 'idle': // 成功调用reset接口后触发该状态机上报 // callBack('idle'); - console.log('jiangsong AVPlayer idle') + console.log(VoiceTag, ' AVPlayer idle') if (this.type == 3) { if (this.mediaArray.length && !this.endFlag) { this.mediaArray.splice(0, 1) @@ -51,11 +54,11 @@ export class voiceService { callBack('idle', this.playerName); break; case 'initialized': // avplayer 设置播放源后触发该状态上报 - console.info('jiangsong AVPlayerstate initialized called.'); + console.info(VoiceTag, ' AVPlayerstate initialized called.'); this.avPlayer.prepare().then(() => { - console.info('jiangsong AVPlayer prepare succeeded.'); + console.info(VoiceTag, ' AVPlayer prepare succeeded.'); }, (err: BusinessError) => { - console.error(`jiangsong Invoke prepare failed, code is ${err.code}, message is ${err.message}`); + console.error(VoiceTag, ` Invoke prepare failed, code is ${err.code}, message is ${err.message}`); }); callBack('initialized'); break; @@ -70,21 +73,21 @@ export class voiceService { callBack('paused'); break; case 'completed': // 播放结束后触发该状态机上报 - console.info('jiangsong AVPlayer state completed called.'); + console.info(VoiceTag, ' AVPlayer state completed called.'); this.avPlayer.stop(); //调用播放结束接口 break; case 'stopped': // stop接口成功调用后触发该状态机上报 - console.info('jiangsong AVPlayer state stopped called.'); + console.info(VoiceTag, ' AVPlayer state stopped called.'); this.avPlayer.reset(); // 调用reset接口初始化avplayer状态 // callBack('stopped'); break; case 'released': - console.info('jiangsong AVPlayer state released called.'); + console.info(VoiceTag, ' AVPlayer state released called.'); callBack('released'); break; default: - console.info('jiangsong AVPlayer state unknown called.'); + console.info(VoiceTag, ' AVPlayer state unknown called.'); callBack('unknown'); break; } @@ -92,13 +95,13 @@ export class voiceService { } // 以下为使用资源管理接口获取打包在HAP内的媒体资源文件并通过fdSrc属性进行播放示例 - avPlayerFdSrc(name) { - const context = AppStorage.get('context') + avPlayerFdSrc(name: string) { + const context: common.UIAbilityContext = AppStorage.get('context') context.resourceManager.getRawFd(name, async (error, value) => { if (error != null) { - console.log(`jiangsong callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); + console.log(VoiceTag, ` callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); } else { - console.log('jiangsongjiangsong', this.avPlayer) + console.log(VoiceTag, this.avPlayer) if (this.avPlayer) { await this.avPlayer.reset() this.avPlayer.fdSrc = value; @@ -106,13 +109,13 @@ export class voiceService { // 为fdSrc赋值触发initialized状态机上报 // this.avPlayer.play() - console.info("jiangsong click me after success value.fd " + JSON.stringify(value)); + console.info(VoiceTag, " click me after success value.fd " + JSON.stringify(value)); } }); } // 以下为通过url设置网络地址来实现播放直播码流的 - avPlayerLive(url) { + avPlayerLive(url: string) { this.avPlayer.url = url } @@ -121,18 +124,17 @@ export class voiceService { } avPlayerStop() { - this.avPlayer && this.avPlayer.stop((err) => { + this.avPlayer && this.avPlayer.stop((err: BusinessError) => { if (err == null) { this.endFlag = true - console.info('stop success'); + console.info(VoiceTag, 'stop success'); } else { - console.error('stop filed,error message is :' + err.message) + console.error(VoiceTag, 'stop filed,error message is :' + err.message) } }) } - playAudio(param) { - console.log('jiangsong') + playAudio(param: playParams) { this.endFlag = false this.mediaArray = [] this.type = param.type @@ -150,6 +152,13 @@ export class voiceService { } } - private callBack = function (value) { - } + // private callBack = function (value) { + // } +} + +interface playParams { + type: number, + name?: string, + url?: string, + value?: string[] } \ No newline at end of file