2024-08-29 08:10:20 +08:00

151 lines
5.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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