小程序音频播放详解

<img class="static-video" @tap="startAudio" v-if="currentPlay==false" :src="imageUrl+'/voice.png'" />
<img class="play-video" v-else  :src="imageUrl+'/play.gif'"/>

data中的数据
currentPlay:false,

第一种方式

// 开启播放音频
startAudio(){
  const innerAudioContext = uni.createInnerAudioContext();//创建并返回内部 audio 
  innerAudioContext.autoplay = false;//不自动播放
  innerAudioContext.src =this.pageInfoBack.leaveReason;//音频的地址,不支持本地路径
  innerAudioContext.obeyMuteSwitch=false;//即使用户打开了静音开关,也能继续发出声音
  // 音频播放事件
  let self=this;
  self.currentPlay=true;//显示播放图标
  innerAudioContext.play();	// 点击按钮音频开始播放事件
  innerAudioContext.onError((res) => {
    self.currentPlay=false;//展示另外一个播放中图标
  });
  innerAudioContext.onEnded(res=>{
    self.currentPlay=false;//展示默认中的图标i
  })
},

官方使用示例

const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
innerAudioContext.src = 'https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3';
innerAudioContext.onPlay(() => {
  console.log('开始播放');
});
innerAudioContext.onError((res) => {
  console.log(res.errMsg);
  console.log(res.errCode);
});

为什么我不使用官方示例

因为给我返回来的那个音频地址没有后缀
导致使用官方示例无法播放
被迫使用第一种方式

官方地址:https://uniapp.dcloud.io/api/media/audio-context?id=createinneraudiocontext

支持格式

| 格式 | iOS  | Android |
| ---- | ---- | ---- |
| flac | x    | √    |
| m4a  | √    | √    |
| ogg  | x    | √    |
| ape  | x    | √    |
| amr  | x    | √    |
| wma  | x    | √    |
| wav  | √    | √    |
| mp3  | √    | √    |
| mp4  | x    | √    |
| aac  | √    | √    |
| aiff | √    | x    |
| caf  | √    | x    |
作者:流年少年
出处:https://www.cnblogs.com/ishoulgodo/

想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,或者关注博主,在此感谢!

万水千山总是情,打赏5毛买辣条行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主(っ•̀ω•́)っ✎⁾⁾!

想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!

支付宝
微信
本文版权归作者所有,欢迎转载,未经作者同意须保留此段声明,在文章页面明显位置给出原文连接
如果文中有什么错误,欢迎指出。以免更多的人被误导。
原文地址:https://www.cnblogs.com/ishoulgodo/p/15012305.html