feat: 添加VoiceTag常量,优化日志输出,增强可读性

This commit is contained in:
wangzhongjie 2025-03-24 15:29:08 +08:00
parent a9cdc3c552
commit 5a8b3d314f
2 changed files with 37 additions and 25 deletions

View File

@ -3,4 +3,7 @@ export const DbTag = '[DBTag]';
export const SerialPortTag = '[SerialPortTag]';
// usb tag
export const UsbTag = '[UsbTag]';
export const UsbTag = '[UsbTag]';
//voice tag
export const VoiceTag = '[VoiceTag]';

View File

@ -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<any> = [];
private mediaArray: Array<string> = [];
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[]
}