开发 | 微信小程序audio音频播放组件+api_wx.createAudioContext

发布时间:2019-06-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了开发 | 微信小程序audio音频播放组件+api_wx.createAudioContext脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
引言: audio是微信小程序中的音频组件,可以轻松实现小程序中播放/停止音频等自定义动作。

附上微信小程序audio组件的相关属性说明:
https://mp.weixin.qq.com/debu...

本次将通过小程序audio的 poster、name、author、src、id、controls 属性,以及相关api:wx.createAudioContext 的使用,来制作一个简单的音频播放控制页面
图片描述

步骤一
打开微信开发者工具,创建小程序项目,选择新建的空白文件夹即可,工具为自动为你生成小程序必要文件!接着在 pages 下创建一个文件夹命名 audio
图片描述

步骤二
接着打开 app.json,如下图添加 "pages/audio/audio", 写入该页面路径,确保能够访问。写入之后,audio文件会生成js/json/wXMl等空白配置文件

开发 | 微信小程序audio音频播放组件+api_wx.createAudioContext

步骤三
直接贴出代码了,audio.JSON 默认即可
audio.js (audio脚本文件)

// audio.js  
Page({  
  data: {  
    poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',  
    name: '此时此刻',  
    author: '许巍',  
    src: 'http://ws.stream.QQmusic.qq.COM/M500001VfvsJ21xFqb.mP3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&Fromtag=46',  
  },  
  onReady: function (e) {  
    // 使用 wx.createAudioContext 获取 audio 上下文 context  
    this.audioCtx = wx.createAudioContext('myAudio')  
  },  
  audioplay: function () {  
    this.audioCtx.play()  
  },  
  audioPause: function () {  
    this.audioCtx.pause()  
  },  
  audio14: function () {  
    this.audioCtx.seek(14)  
  },  
  audioStart: function () {  
    this.audioCtx.seek(0)  
  }  
})  

audio.wxML(audio页面结构文件)

<!-- audio.wxml -->  
<audio poster="{{poster}}" name="{{name}}" author="{{author}}" src="{{src}}" id="myAudio" controls></audio>  
   
<button class="button-style" bindtap="audioPlay">播放</button>  
<button class="button-style" bindtap="audioPause">暂停</button>  
<button class="button-style" bindtap="audio14">设置当前播放时间为14秒</button>  
<button class="button-style" bindtap="audioStart">回到开头</button>  

audio.wxss(audio页面样式文件)

/* pages/audio/aduio.wxss */  
.button-style{   
    background-color: #eee;    
    border-radius: 8rpx;    
    margin: 20rpx;    
}  

微信小程序audio音频播放组件的效果:

开发 | 微信小程序audio音频播放组件+api_wx.createAudioContext

脚本宝典总结

以上是脚本宝典为你收集整理的开发 | 微信小程序audio音频播放组件+api_wx.createAudioContext全部内容,希望文章能够帮你解决开发 | 微信小程序audio音频播放组件+api_wx.createAudioContext所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。