166 lines
5.6 KiB
Plaintext
Raw Normal View History

2025-04-10 10:28:07 +08:00
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;
export class voiceService {
private avPlayer: media.AVPlayer | null = null;
private fileSize: number = -1;
private fd: number = 0;
private playerName: string = '';
private type: number = 1;
private endFlag: Boolean = false;
private mediaArray: Array<string> = [];
private callBack: AVPlayerCallback | null = null;
private context: common.UIAbilityContext
constructor(callBack: AVPlayerCallback, context: common.UIAbilityContext) {
this.context = context
// 创建avPlayer实例对象
media.createAVPlayer().then(video => {
this.avPlayer = video
// 创建状态机变化回调函数
this.callBack = callBack
this.setAVPlayerCallback(this.callBack);
})
}
// 注册avplayer回调函数
setAVPlayerCallback(callBack: AVPlayerCallback) {
console.log(VoiceTag, ' avPlayerFdSrc setAVPlayerCallback begin')
// error回调监听函数,当avPlayer在操作过程中出现错误时调用reset接口触发重置流程
this.avPlayer!.on('error', (err: BusinessError) => {
console.error(VoiceTag, `Invoke avPlayer failed, code is ${err.code}, message is ${err.message}`);
this.avPlayer!.reset(); // 调用reset重置资源触发idle状态
})
// 状态机变化回调函数
this.avPlayer!.on('stateChange', async (state, reason) => {
switch (state) {
case 'idle': // 成功调用reset接口后触发该状态机上报
// callBack('idle');
console.log(VoiceTag, ' AVPlayer idle')
if (this.type == 3) {
if (this.mediaArray.length && !this.endFlag) {
this.mediaArray.splice(0, 1)
this.avPlayerFdSrc(this.mediaArray[0])
} else {
callBack('idle');
}
return
}
callBack('idle', this.playerName);
break;
case 'initialized': // avplayer 设置播放源后触发该状态上报
console.info(VoiceTag, ' AVPlayerstate initialized called.');
this.avPlayer!.prepare().then(() => {
console.info(VoiceTag, ' AVPlayer prepare succeeded.');
}, (err: BusinessError) => {
console.error(VoiceTag, ` Invoke prepare failed, code is ${err.code}, message is ${err.message}`);
});
callBack('initialized');
break;
case 'prepared': // prepare调用成功后上报该状态机
this.avPlayer!.play();
callBack('prepared');
break;
case 'playing': // play成功调用后触发该状态机上报
callBack('playing');
break;
case 'paused': // pause成功调用后触发该状态机上报
callBack('paused');
break;
case 'completed': // 播放结束后触发该状态机上报
console.info(VoiceTag, ' AVPlayer state completed called.');
this.avPlayer!.stop(); //调用播放结束接口
break;
case 'stopped': // stop接口成功调用后触发该状态机上报
console.info(VoiceTag, ' AVPlayer state stopped called.');
this.avPlayer!.reset(); // 调用reset接口初始化avplayer状态
// callBack('stopped');
break;
case 'released':
console.info(VoiceTag, ' AVPlayer state released called.');
callBack('released');
break;
default:
console.info(VoiceTag, ' AVPlayer state unknown called.');
callBack('unknown');
break;
}
})
}
// 以下为使用资源管理接口获取打包在HAP内的媒体资源文件并通过fdSrc属性进行播放示例
avPlayerFdSrc(name: string) {
const context = this.context
context.resourceManager.getRawFd(name, async (error, value) => {
if (error != null) {
console.log(VoiceTag, ` callback getRawFd failed error code: ${error.code}, message: ${error.message}.`);
} else {
console.log(VoiceTag, this.avPlayer)
if (this.avPlayer) {
await this.avPlayer!.reset()
this.avPlayer!.fdSrc = value;
}
// 为fdSrc赋值触发initialized状态机上报
// this.avPlayer.play()
console.info(VoiceTag, " click me after success value.fd " + JSON.stringify(value));
}
});
}
// 以下为通过url设置网络地址来实现播放直播码流的
avPlayerLive(url: string) {
this.avPlayer!.url = url
}
releasePlayer() {
this.avPlayer && this.avPlayer!.release();
}
avPlayerStop() {
this.avPlayer && this.avPlayer!.stop((err: BusinessError) => {
if (err == null) {
this.endFlag = true
console.info(VoiceTag, 'stop success');
} else {
console.error(VoiceTag, 'stop filed,error message is :' + err.message)
}
})
}
playAudio(param: playParams) {
this.endFlag = false
this.mediaArray = []
this.type = param.type ?? 1;
if (param.type == 1) {
this.playerName = param.name || ""
// this.nextPlayerName=param.nextName
this.avPlayerFdSrc(param.name || "");
} else if (param.type == 2) {
this.avPlayerLive(param.url!);
} else if (param.type == 3) {
if (param.value && param.value.length) {
this.mediaArray = param.value;
this.avPlayerFdSrc(param.value[0]);
}
}
}
// private callBack = function (value) {
// }
}
interface playParams {
type?: number,
name?: string,
url?: string,
value?: string[]
}