小程序api-innerAudioContext的使用

本文详细介绍如何在微信小程序中使用innerAudioContext组件控制音频播放,包括设置源文件、自动播放、监听播放状态及错误处理等关键步骤。通过具体代码示例,帮助开发者快速掌握音频播放功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

innerAudioContext的使用

因为官方文档中innerAudioContext直接复制使用可能会有问题,这里应该将const innerAudioContext = wx.createInnerAudioContext()放到Page外面。其他放到onLoad函数中。

<!-- audio.wxml -->
<audio  src="{{src}}" id="myAudio" ></audio>

<button type="primary" bindtap="audioPlay">播放</button>
<button type="primary" bindtap="audioPause">暂停</button>


// audio.js


const innerAudioContext = wx.createInnerAudioContext()

Page({
  data: {
    src: ''
  },
  onLoad:function(options){
    innerAudioContext.autoplay = true
    innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  
    innerAudioContext.onPause(
      () =>{
        console.log('停止播放')
      }
    )
  }
    
  ,

  audioPlay: function () {
    innerAudioContext.play()
  },
  audioPause:function(){
    innerAudioContext.pause()
  }
  

})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值