You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AdvancedAudioPlayer未播放时getDurationMs返回0且setPosition无效问题求助

Superpowered AdvancedAudioPlayer 预播放阶段时长获取与定位问题解析

问题原因

Superpowered的AdvancedAudioPlayer采用按需解析的设计:调用openMemory加载音频文件后,并不会立刻完整解析音频的元数据(包括时长),只有当播放器进入播放状态(调用play())时,才会完成音频解码和元数据的初始化工作。所以预播放阶段getDurationMs()返回0,而setPosition()因为没有有效的时长基准,自然无法生效。

解决办法

方案1:触发预解析但不产生播放

可以短暂调用play()后立刻暂停,强制播放器完成元数据解析,且不会有音频输出。修改后的代码示例:

// 加载曲目时
this.player.openMemory(Superpowered.arrayBufferToWASM(buffer), false, false);
// 触发元数据解析
this.player.play();
this.player.pause();
// 此时即可获取正确时长
this.sendMessageToMainScope({
    data: { duration: this.player.getDurationMs() },
});

完成这一步后,setPosition()也能正常工作。

方案2:结合Decoder与播放器延迟定位

先用Superpowered.Decoder解析出时长,等播放器调用play()初始化完成后,再执行定位操作:

// 提前用Decoder解析时长
const decoder = new Superpowered.Decoder();
decoder.decodeChunk(buffer, (success, durationMs) => {
    if (success) {
        this.audioDuration = durationMs;
        // 加载音频到播放器
        this.player.openMemory(Superpowered.arrayBufferToWASM(buffer), false, false);
    }
});

// 当需要定位时
this.player.play();
// 此时setPosition生效,用解析好的时长计算定位比例
this.player.setPosition(targetPositionMs / this.audioDuration);

内容的提问来源于stack exchange,提问作者Mateus Pires

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 01:25:01