微信小程序音频播放
2017-09-21 18:42
274 查看
今天做微信小程序涉及到播放音频文件:
使用audio标签来绑定音频路径
<audio id='audioid' src='{{vidioUrl}}' binderror="audioError" bindplay="audioPlay" bindeneded="playEnd" bindtimeupdate="timeUpdate"> </audio>
目前没有做到播放下一首的功能,只有暂停和播放。
最上面的播放条是通过progress的percent来实现的,
<progress percent="{{currtRate}}" stroke-width="2" activeColor="#ffd200" backgroundColor="#bcbcbc" />通过currtRate来实现播放条的已播放长度
界面:
<view class="ub-ver info-container" style="height:5em;">
<!-- 音频播放 -->
<view class='ub fonts12 align-items-center' >
<view>{{currentTime}}</view>
<view class="umw-6"></view>
<view class='ub-f1'>
<progress percent="{{currtRate}}" stroke-width="2" activeColor="#ffd200" backgroundColor="#bcbcbc" />
<!-- {{vidioUrl}} -->
</view>
<view class="umw-6"></view>
<view>{{duration}}</view>
</view>
<audio id='audioid' src='{{vidioUrl}}' binderror="audioError" bindplay="audioPlay" bindeneded="playEnd" bindtimeupdate="timeUpdate"> </audio>
<view class='ub ub-f1 align-items-center' style="justify-content:center">
<view class="icon-bofanglist align-items-center" style="background:url('http://inheater-bbs.oss-cn-shenzhen.aliyuncs.com/xcx/icons/bofanglist.png') no-repeat;background-size: contain" bindtap="listClick">
</view>
<view class="umw1-5"></view>
<view wx:if="{{flags}}" class="icon-play align-items-center" style="background:url('http://inheater-bbs.oss-cn-shenzhen.aliyuncs.com/xcx/icons/pause.png') no-repeat;background-size: contain" bindtap="pause">
</view>
<view wx:if="{{flagp}}" class="icon-next align-items-center" style="background:url('../../images/zanting@3x.png') no-repeat;background-size: contain" bindtap="play">
</view>
<view class="umw1-5"></view>
<view class="icon-next align-items-center" style="background:url('http://inheater-bbs.oss-cn-shenzhen.aliyuncs.com/xcx/icons/next.png') no-repeat;background-size: contain">
</view>
</view>
</view>
js文件:
onLoad: function (options) {
var that = this;
console.log("topicID" + options.infoId);
wx.request({
url: 'https://***********/' + *******,
header: {
'content-type': 'application/json'
},
success: function (res) {
that.setData({
vidioUrl: res.data.data.videoUrl
})
}
})
//时间戳转换为时间日期格式
function transDate(mescStr) {
var n = mescStr;
var date = new Date(n);
var Y = date.getFullYear() + '-';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
return (Y + M + D)
}
},
//以下是状态监听
audioError: function (resp) {
console.log(resp);
},
audioPlay: function (resp) {
console.log(resp);
console.log('开始播放');
},
playEnd: function (resp) {
console.log(resp);
console.log('播放结束');
},
timeUpdate: function (resp) {
this.setData({
currtRate: (100 * resp.detail.currentTime / resp.detail.duration),
duration: (resp.detail.duration / 60).toPrecision(2),//总时长 保留两位
currentTime: (resp.detail.currentTime / 60).toFixed(2)//toPrecision和toFixed的区别
});
// this.currentTime = resp.detail.currentTime;//当前时长
// console.log(resp);
},
//以下是操作
play: function () {
this.audioContext.play();
this.setData({
flags:true,
flagp:false
})
},
pause: function () {
this.audioContext.pause();
this.setData({
flags: false,
flagp: true
})
},
audioStart: function () {
this.audioCtx.seek(0)//重新播放
},
实现了音频的播放,对于下一首播放,目前没有头绪,刚接触两天,明天继续做
使用audio标签来绑定音频路径
<audio id='audioid' src='{{vidioUrl}}' binderror="audioError" bindplay="audioPlay" bindeneded="playEnd" bindtimeupdate="timeUpdate"> </audio>
目前没有做到播放下一首的功能,只有暂停和播放。
最上面的播放条是通过progress的percent来实现的,
<progress percent="{{currtRate}}" stroke-width="2" activeColor="#ffd200" backgroundColor="#bcbcbc" />通过currtRate来实现播放条的已播放长度
界面:
<view class="ub-ver info-container" style="height:5em;">
<!-- 音频播放 -->
<view class='ub fonts12 align-items-center' >
<view>{{currentTime}}</view>
<view class="umw-6"></view>
<view class='ub-f1'>
<progress percent="{{currtRate}}" stroke-width="2" activeColor="#ffd200" backgroundColor="#bcbcbc" />
<!-- {{vidioUrl}} -->
</view>
<view class="umw-6"></view>
<view>{{duration}}</view>
</view>
<audio id='audioid' src='{{vidioUrl}}' binderror="audioError" bindplay="audioPlay" bindeneded="playEnd" bindtimeupdate="timeUpdate"> </audio>
<view class='ub ub-f1 align-items-center' style="justify-content:center">
<view class="icon-bofanglist align-items-center" style="background:url('http://inheater-bbs.oss-cn-shenzhen.aliyuncs.com/xcx/icons/bofanglist.png') no-repeat;background-size: contain" bindtap="listClick">
</view>
<view class="umw1-5"></view>
<view wx:if="{{flags}}" class="icon-play align-items-center" style="background:url('http://inheater-bbs.oss-cn-shenzhen.aliyuncs.com/xcx/icons/pause.png') no-repeat;background-size: contain" bindtap="pause">
</view>
<view wx:if="{{flagp}}" class="icon-next align-items-center" style="background:url('../../images/zanting@3x.png') no-repeat;background-size: contain" bindtap="play">
</view>
<view class="umw1-5"></view>
<view class="icon-next align-items-center" style="background:url('http://inheater-bbs.oss-cn-shenzhen.aliyuncs.com/xcx/icons/next.png') no-repeat;background-size: contain">
</view>
</view>
</view>
js文件:
onLoad: function (options) {
var that = this;
console.log("topicID" + options.infoId);
wx.request({
url: 'https://***********/' + *******,
header: {
'content-type': 'application/json'
},
success: function (res) {
that.setData({
vidioUrl: res.data.data.videoUrl
})
}
})
//时间戳转换为时间日期格式
function transDate(mescStr) {
var n = mescStr;
var date = new Date(n);
var Y = date.getFullYear() + '-';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
return (Y + M + D)
}
},
//以下是状态监听
audioError: function (resp) {
console.log(resp);
},
audioPlay: function (resp) {
console.log(resp);
console.log('开始播放');
},
playEnd: function (resp) {
console.log(resp);
console.log('播放结束');
},
timeUpdate: function (resp) {
this.setData({
currtRate: (100 * resp.detail.currentTime / resp.detail.duration),
duration: (resp.detail.duration / 60).toPrecision(2),//总时长 保留两位
currentTime: (resp.detail.currentTime / 60).toFixed(2)//toPrecision和toFixed的区别
});
// this.currentTime = resp.detail.currentTime;//当前时长
// console.log(resp);
},
//以下是操作
play: function () {
this.audioContext.play();
this.setData({
flags:true,
flagp:false
})
},
pause: function () {
this.audioContext.pause();
this.setData({
flags: false,
flagp: true
})
},
audioStart: function () {
this.audioCtx.seek(0)//重新播放
},
实现了音频的播放,对于下一首播放,目前没有头绪,刚接触两天,明天继续做
相关文章推荐
- 微信小程序-图片、录音、音频播放、音乐播放、视频、文件代码实例
- 微信小程序 audio音频播放详解及实例
- 微信小程序音频相关问题:播放,录音等相关
- 微信小程序-图片、录音、音频播放、音乐播放、视屏、文件
- 微信小程序之音频播放-wx.createInnerAudioContext()小坑
- 微信小程序之----audio音频播放
- 微信小程序 —— 自定义带进度条的音频播放控件audio
- 微信小程序开发之录音机 音频播放 动画 (真机可用)
- 微信小程序开发之录音机 音频播放 动画 (真机可用)
- 开发 | 微信小程序audio音频播放组件+api_wx.createAudioContext
- 微信小程序开发之录音机 音频播放 动画实例 (真机可用)
- 微信小程序获取音频时长与实时获取播放进度问题
- IOS中微信小程序播放缓存的音频文件的方法
- 微信小程序开发之录音机 音频播放 动画 (真机可用)
- 微信小程序多音频播放进度条问题
- 微信小程序开发之录音机 音频播放 动画 (真机可用)
- 微信小程序播放音频列表
- 优化微信小程序音频播放,实现多音频同时播放,互不干扰。
- 微信小程序page的生命周期和音频播放及监听实例详解
- EA&UML日拱一卒-微信小程序实战:位置闹铃 (6)-播放音频