ios真机调试在uni-app中mp3文件播放报错TypeError: Attempted to assign to readonly property. __ERROR

ios真机调试在uni-app中mp3文件播放报错TypeError: Attempted to assign to readonly property. __ERROR

开发环境 版本号 项目创建方式
Mac 11.2.3 HBuilderX
iOS iOS 15
### 示例代码:

```javascript
var innerAudioContext = uni.createInnerAudioContext();  
innerAudioContext.autoplay = true;  
innerAudioContext.obeyMuteSwitch = false;  
innerAudioContext.src = 'url播放地址';  
innerAudioContext.play()

操作步骤:

var innerAudioContext = uni.createInnerAudioContext();  
innerAudioContext.autoplay = true;  
innerAudioContext.obeyMuteSwitch = false;  
innerAudioContext.src = 'url播放地址';  
innerAudioContext.play()

预期结果:

按官网文档可正常使用

实际结果:

控制台报错,音频无法播放 17:02:44.108 [Vue warn]: Error in v-on handler: “TypeError: Attempted to assign to readonly property.” [ERROR] : [Vue warn]: Error in v-on handler: “TypeError: Attempted to assign to readonly property.”(found at pages/index.vue:1) ERROR
17:02:44.109 TypeError: Attempted to assign to readonly property. ERROR

bug描述:

mp3文件播放报错 设置 innerAudioContext.obeyMuteSwitch = false; 后控制台报错如下: TypeError: Attempted to assign to readonly property. 不设置,可以播放


更多关于ios真机调试在uni-app中mp3文件播放报错TypeError: Attempted to assign to readonly property. __ERROR的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

obeyMuteSwitch 只能在小程序端使用,APP没法用的。

更多关于ios真机调试在uni-app中mp3文件播放报错TypeError: Attempted to assign to readonly property. __ERROR的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这个错误是由于iOS系统对obeyMuteSwitch属性的限制导致的。在iOS平台上,obeyMuteSwitch是一个只读属性,不能通过代码修改。

解决方案:

  1. 直接移除innerAudioContext.obeyMuteSwitch = false这行代码
  2. 或者使用条件编译针对iOS平台做特殊处理:
var innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
// #ifndef APP-PLUS
innerAudioContext.obeyMuteSwitch = false;
// #endif
innerAudioContext.src = 'url播放地址';
innerAudioContext.play();
回到顶部