HarmonyOS 鸿蒙Next 使用AVPlayer播放短促音效时有明显延迟
HarmonyOS 鸿蒙Next 使用AVPlayer播放短促音效时有明显延迟
调用时
avp: AVPlayerDemo = new AVPlayerDemo()
Button().onClick(() => {
this.avp.avPlayerFdSrcDemo()
})
按下按钮后总是过100-200毫秒才播放音效,并不是点击后马上播放。我也尝试过将await部分的逻辑单独抽取到init中提前执行,但是没有效果。
我下载过其他app带有点击按钮播放音效的,没有出现延迟现象。应该是我使用api有问题,但是看不出是哪里的问题,求大神帮助。
以下是代码,就是官方demo中的代码,把case ‘playing’:里面的bug去掉了
export class AVPlayerDemo {
private avPlayer;
setAVPlayerCallback() {
this.avPlayer.on(‘seekDone’, (seekDoneTime) => {
console.info(AVPlayer seek succeeded, seek time is ${seekDoneTime}
);
})
this.avPlayer.on(‘error’, (err) => {
console.error(Invoke avPlayer failed, code is ${err.code}, message is ${err.message}
);
this.avPlayer.reset();
})
this.avPlayer.on(‘stateChange’, async (state, reason) => {
switch (state) {
case ‘idle’:
console.info(‘AVPlayer state idle called.’);
this.avPlayer.release();
break;
case ‘initialized’:
await this.avPlayer.prepare()
break;
case ‘prepared’:
console.info(‘AVPlayer state prepared called.’);
this.avPlayer.play();
break;
case ‘playing’:
console.info(‘AVPlayer state playing called.’);
break;
case ‘paused’:
console.info(‘AVPlayer state paused called.’);
this.avPlayer.play();
break;
case ‘completed’:
console.info(‘AVPlayer state completed called.’);
this.avPlayer.stop();
break;
case ‘stopped’:
console.info(‘AVPlayer state stopped called.’);
this.avPlayer.reset();
break;
case ‘released’:
console.info(‘AVPlayer state released called.’);
break;
default:
console.info(‘AVPlayer state unknown called.’);
break;
}
})
}
public async avPlayerFdSrcDemo() {
this.avPlayer = await media.createAVPlayer();
this.setAVPlayerCallback();
let context = getContext(this) as common.UIAbilityContext;
let fileDescriptor = await context.resourceManager.getRawFd(‘click_def0.mp3’);
this.avPlayer.fdSrc = fileDescriptor;
}
4 回复
遇到一样的问题,请问下这个问题是否还有救?延迟确实有点严重
这个问题通过在线提单进一步解决:https://developer.huawei.com/consumer/cn/support/feedback/#/,感谢您的反馈和支持
我最初就在这里反馈的,官方回复是代码没问题,但是有延迟问题,不知道是哪里写的不对。
HarmonyOS鸿蒙Next使用AVPlayer播放短促音效时出现明显延迟,可能由多种因素导致,包括音频格式、解码效率、设备性能等。建议检查音频格式是否与设备兼容,尝试使用低延迟的音频编码格式如AAC。同时,确保AVPlayer配置正确,并优化音频数据处理流程,如减少不必要的解码和渲染开销。如果问题依旧,请检查设备性能及系统资源占用情况,或考虑使用其他音频播放库。如果问题依旧没法解决请加我微信,我的微信是itying888。