微信小程序wx.createInnerAudioContext使用方法
程序员文章站
2022-05-06 08:00:13
...
1.创建audio对象
js开头:
const myaudio = wx.createInnerAudioContext({});
2.设置资源路径,onShow或者onLoad内设置
onShow: function () {
myaudio.src = "http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46"
},
3.页面
<view class='audioContainer'>
<view>录音</view>
<!--当前为停止状态 -->
<view class='audioImg' wx:if="{{isplay==false}}" bindtap='play'>
<image style="width:100%;height:100%;border-radius:50%;" src='/pages/image/play.png' />
</view>
<!--当前为播放状态 -->
<view class='audioImg' wx:if="{{isplay==true}}" bindtap='stop'>
<image style="width:100%;height:100%;border-radius:50%;" src='/pages/image/pause.png' />
</view>
</view>
<view style='width:50%'>
<button bindtap='review' type='primary'>重新播放</button>
</view>
4.为页面注册方法,播放暂停重播
//播放
play: function () {
myaudio.play();
console.log(myaudio.duration);
this.setData({ isplay: true });
},
// 停止
stop: function () {
myaudio.pause();
this.setData({ isplay: false });
},
/**
* 重播
*/
review:function(){
myaudio.stop();
myaudio.play()
this.setData({ isplay: true });
}
5.监听事件,在onLoad或onshow中设置
onLoad:function(){
myaudio.onPause(function(){
console.log('mmp')
wx.showToast({
title: 'heheda',
})
})
},
6.这个组件应该可以实现背景音乐~~~
下一篇: Spring Cloud