举头望明月
举头望明月
  • 发布:2019-09-05 16:27
  • 更新:2021-03-22 10:10
  • 阅读:1882

【报Bug】uni.createInnerAudioContext() 在iPhone版的微信网页中不能自动播放

分类:uni-app

详细问题描述

最近开发微信公众号网页,页面中需要设置背景音乐,但是在真机调试的时候,Android手机没有问题,iPhone无法自动播放,

// 销毁已存在的音乐上下文对象,防止重复播放
if (this.audioContext) {
this.audioContext.stop();
this.audioContext.destroy();
}
this.audioContext = uni.createInnerAudioContext();
this.audioContext.src = this.music.backgroundMusicUrl; // 背景音乐地址
this.audioContext.autoplay = true;
this.audioContext.loop = true;
this.audioContext.play();
this.playing = true; // 自定义的变量,保存音乐播放状态

之后尝试监控onCanplay()再调用播放,也无效,没有触发onCanplay回调,代码改成下面这样

// 销毁已存在的音乐上下文对象,防止重复播放
if (this.audioContext) {
this.audioContext.stop();
this.audioContext.destroy();
}
this.audioContext = uni.createInnerAudioContext();
this.audioContext.src = this.music.backgroundMusicUrl; // 背景音乐地址
this.audioContext.autoplay = true;
this.audioContext.loop = true;

let self = this;
this.audioContext.onCanplay(() => {
self.audioContext.play();
self.playing = true; // 自定义的变量,保存音乐播放状态
});

但是我设置了一个按钮,手动调用一次暂停后再播放,就可以播放出来

/**

  • 切换播放状态
    */
    onChangePlayState: function() {
    this.playing = !this.playing;
    if (this.playing) {
    this.audioContext.play();
    } else {
    this.audioContext.pause();
    }
    }
2019-09-05 16:27 负责人:无 分享
已邀请:
Junn

Junn - 面向金钱编程

遇到同样问题

筱凌薇

筱凌薇 - 前端程序媛

楼主如何解决的?

该问题目前已经被锁定, 无法添加新回复