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

引言:
audio是微信小程序中的音频组件,可以轻松实现小程序中播放/停止音频等自定义动作。

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

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

 

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

 

步骤二

接着打开微信小程序的 app.json,如下图添加 "pages/audio/audio", 写入该页面路径,确保能够访问。写入之后,audio文件会生成js/json/wxml等空白配置文件。这是最基本的操作啦!

 

步骤三
直接贴出代码了,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页面结构文件)
  1. <!-- 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音频播放的效果:
更多 微信小程序开发可以关注我的博客:http://www.cnblogs.com/lanshu/
posted on 2018-02-11 10:10 微信小程序开发资源 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lanshu/p/8440572.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值