收藏
回答

十万火急,为什么开发工具可以播放音频,预览的时候不能播放?

小弟我刚刚做微信小程序,里面有个播放语音的本来很高兴在微信开发者工具里都正常了,结果放到手机预览的时候坏菜了,也没有错误,就是播放不出来。

补充一下,我为了测试在assets文件加下放了一个测试mp3.然后代码改成了:

    this.currentAudioContext.src = '/assets/test.mp3';

    this.currentAudioContext.play();

还是不出声音,用预览模式用的ipthon16 pro max测试的。

我的日志:

我的代码:

playVoice() {

    if (!this.data.audioPath) {

      console.error('音频路径无效');

      this.showErrorToast('暂无有效音频');

      return;

    }

    // 停止当前播放

    if (this.currentAudioContext) {

      this.currentAudioContext.stop();

      this.currentAudioContext.destroy();

    }

    // 创建新的音频上下文

    this.currentAudioContext = wx.createInnerAudioContext();

    this.currentAudioContext.obeyMuteSwitch = false;

    // 错误处理

    this.currentAudioContext.onError((res) => {

      console.error('播放错误:', {

        errCode: res.errCode,

        errMsg: res.errMsg,

        path: this.data.audioPath

      });

      this.showErrorToast(this.getPlayErrorMsg(res.errCode));

    });

    this.currentAudioContext.onPlay(() => {

      console.log('音频开始播放:', this.data.audioPath);

    });

    console.log('尝试播放音频:', this.data.audioPath);

    this.currentAudioContext.src = this.data.audioPath;

    this.currentAudioContext.play();

  },

愁的我都睡不着了,咋办呢?

最后一次编辑于  04-13
回答关注问题邀请回答
收藏

2 个回答

  • showms
    showms
    04-13

    可能是音频文件用的是临时文件路径的缘故。

    04-13
    有用
    回复 10
    • 光哥
      光哥
      04-13
      怎么修改呢
      04-13
      回复
    • showms
      showms
      04-13回复光哥
      试试把临时文件上传到云存储后拿到http地址传入给audioContext.src
      04-13
      回复
    • 光哥
      光哥
      04-13回复showms
      刚才试着在assets下面放了个mp3文件还是不可以,是不是预览就是不可以呢
      04-13
      回复
    • showms
      showms
      04-13回复光哥
      打开调试模式,看看报什么错
      04-13
      回复
    • 光哥
      光哥
      发表于移动端
      04-13回复showms
      什么错误都没有,可以看上面手机贴图,所以很懵逼
      04-13
      回复
    查看更多(5)
  • 智能回答 智能回答 该问答由AI生成
    04-13
    有用
登录 后发表内容