如何编写微信小程序音频播放进度实例代码?
- 内容介绍
- 相关推荐
本文共计915个文字,预计阅读时间需要4分钟。
问题描述:在微信小程序中,经常需要使用控制文件播放的滑动条,通过滑动条可以控制音频播放进度。
解决方案:
1.使用.wxml和.wmss文件实现进度条效果。
2.通过.js文件控制文件播放。
具体实现:
1. wxml文件:
2. wmss文件:css.progress-container { width: 100%; height: 10px; background-color: #eee;}
.progress-slider { width: 100%; height: 100%; background-color: #0094ff;}
3. js文件:javascriptPage({ data: { audioContext: null, progress: 0, duration: 0 }, onLoad: function() { this.audioContext=wx.createInnerAudioContext(); this.audioContext.onPlay(()=> { this.startProgressUpdate(); }); this.audioContext.onEnded(()=> { this.stopProgressUpdate(); }); }, onSliderChange: function(e) { const value=e.detail.value; this.audioContext.seek(value); }, startProgressUpdate: function() { this.updateProgress(); setInterval(this.updateProgress, 1000); }, updateProgress: function() { const currentTime=this.audioContext.getCurrentTime(); const duration=this.audioContext.duration; this.setData({ progress: currentTime / duration * 100 }); }, stopProgressUpdate: function() { clearInterval(this.updateProgress); }, onUnload: function() { this.audioContext.destroy(); }});
以上代码实现了通过滑动条控制音频播放进度的功能。
问题描述
在微信小程序中经常会用到控制文件播放的滑块,通过滑块可控制音频播放进度,下面即用代码实现。
解决方案
首先用.wxml与 .wmss 代码实现进度条的效果,再通过 .js 文件控制进度条的进度和进度条的时间显示。
.wxml中(播放进度结构的代码):
<view class="content-play-progress"> <text>{{play.currentTime}}</text> <view> <slider activeColor="#d33a31" block-size="12" backgroundColor="#dadada" value="{{play.percent}}"/> </view> <text>{{play.duration}}</text> </view>
在上述代码中,第五行用到了slider组件,其值为播放进度 play.percent。
.wxss中(播放进度样式的代码) :
.content-play-progress{ display: flex; align-items: center; margin: 0 35rpx; font-size: 9pt; text-align: center; } .content-play-progress>view{ flex: 1; }
保存上述代码后,运行程序,效果如图:
图 1 微信小程序进度条的实现
.js中(控制进度条的进度和时间的代码) :
onReady: function(){ this.audioCtx=wx.createInnerAudioContext() var that=this //播放失败检测 this. audioCtx.onError(function(){ console.log( ‘ 播放失败: ' +that.audioCtx.src) }) //播放完成自动换下一曲 this. audioCtx.OnEnded(function(){ that.next() }) //自动更新播放进度 this. audioCtx.onPlay(function(){ this. audioCtx.onTimeUpdate(function(){ that.setData({ ‘ play.duration ' : formatTime(that.audioCtx.duration), ‘ play.currrentTime ' : formatTime(that.audioCtx. currrentTime), ‘ play.percent ' : that.audioCtx. currrentTime / that.audioCtx.duration*100 }) }) //默认选择第一曲 T his.setMusic(0) //格式化时间 function formatTime(time){ var minute=Math.floor(time/60)%60; var second=Math.floor(time)%60 return (minute<10? ' 0 ' +minute:minute)+ ' : ' + (second<10? ' 0 ' +second:second) } }) }
上述代码中,通过调用audioCtx的 onTimeUpdate() 的方法,获取音视频状态信息,并通过 formatTime() 函数处理时间格式,最后渲染到页面实现实时更新效果,效果如图:
图 2 微信小程序进度条的滑动
在slider组件上绑定 bindchange 事件,可以实现滑动进度条调节音视频文件播放进度,代码示例 :
<slider bindchange= ” sliderChange ” activeColor= ” #d33a31 ” block-size= ” 12 ” backgroundColor= ” #dadada ” value= ” {{play.percent}} ” / >
在.js文件中编写 sliderChange 函数获取用户当前选择的进度,将时间通过 audioCtx 对象的 seek() 方法进行设置,代码示例:
sliderChange: function(e){ var second=e.detail.value* that.audioCtx.duration/100 that.audioCtx.seek(secend) },
总结
到此这篇关于微信小程序实现音频文件播放进度的实例代码的文章就介绍到这了,更多相关小程序音频文件播放进度内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计915个文字,预计阅读时间需要4分钟。
问题描述:在微信小程序中,经常需要使用控制文件播放的滑动条,通过滑动条可以控制音频播放进度。
解决方案:
1.使用.wxml和.wmss文件实现进度条效果。
2.通过.js文件控制文件播放。
具体实现:
1. wxml文件:
2. wmss文件:css.progress-container { width: 100%; height: 10px; background-color: #eee;}
.progress-slider { width: 100%; height: 100%; background-color: #0094ff;}
3. js文件:javascriptPage({ data: { audioContext: null, progress: 0, duration: 0 }, onLoad: function() { this.audioContext=wx.createInnerAudioContext(); this.audioContext.onPlay(()=> { this.startProgressUpdate(); }); this.audioContext.onEnded(()=> { this.stopProgressUpdate(); }); }, onSliderChange: function(e) { const value=e.detail.value; this.audioContext.seek(value); }, startProgressUpdate: function() { this.updateProgress(); setInterval(this.updateProgress, 1000); }, updateProgress: function() { const currentTime=this.audioContext.getCurrentTime(); const duration=this.audioContext.duration; this.setData({ progress: currentTime / duration * 100 }); }, stopProgressUpdate: function() { clearInterval(this.updateProgress); }, onUnload: function() { this.audioContext.destroy(); }});
以上代码实现了通过滑动条控制音频播放进度的功能。
问题描述
在微信小程序中经常会用到控制文件播放的滑块,通过滑块可控制音频播放进度,下面即用代码实现。
解决方案
首先用.wxml与 .wmss 代码实现进度条的效果,再通过 .js 文件控制进度条的进度和进度条的时间显示。
.wxml中(播放进度结构的代码):
<view class="content-play-progress"> <text>{{play.currentTime}}</text> <view> <slider activeColor="#d33a31" block-size="12" backgroundColor="#dadada" value="{{play.percent}}"/> </view> <text>{{play.duration}}</text> </view>
在上述代码中,第五行用到了slider组件,其值为播放进度 play.percent。
.wxss中(播放进度样式的代码) :
.content-play-progress{ display: flex; align-items: center; margin: 0 35rpx; font-size: 9pt; text-align: center; } .content-play-progress>view{ flex: 1; }
保存上述代码后,运行程序,效果如图:
图 1 微信小程序进度条的实现
.js中(控制进度条的进度和时间的代码) :
onReady: function(){ this.audioCtx=wx.createInnerAudioContext() var that=this //播放失败检测 this. audioCtx.onError(function(){ console.log( ‘ 播放失败: ' +that.audioCtx.src) }) //播放完成自动换下一曲 this. audioCtx.OnEnded(function(){ that.next() }) //自动更新播放进度 this. audioCtx.onPlay(function(){ this. audioCtx.onTimeUpdate(function(){ that.setData({ ‘ play.duration ' : formatTime(that.audioCtx.duration), ‘ play.currrentTime ' : formatTime(that.audioCtx. currrentTime), ‘ play.percent ' : that.audioCtx. currrentTime / that.audioCtx.duration*100 }) }) //默认选择第一曲 T his.setMusic(0) //格式化时间 function formatTime(time){ var minute=Math.floor(time/60)%60; var second=Math.floor(time)%60 return (minute<10? ' 0 ' +minute:minute)+ ' : ' + (second<10? ' 0 ' +second:second) } }) }
上述代码中,通过调用audioCtx的 onTimeUpdate() 的方法,获取音视频状态信息,并通过 formatTime() 函数处理时间格式,最后渲染到页面实现实时更新效果,效果如图:
图 2 微信小程序进度条的滑动
在slider组件上绑定 bindchange 事件,可以实现滑动进度条调节音视频文件播放进度,代码示例 :
<slider bindchange= ” sliderChange ” activeColor= ” #d33a31 ” block-size= ” 12 ” backgroundColor= ” #dadada ” value= ” {{play.percent}} ” / >
在.js文件中编写 sliderChange 函数获取用户当前选择的进度,将时间通过 audioCtx 对象的 seek() 方法进行设置,代码示例:
sliderChange: function(e){ var second=e.detail.value* that.audioCtx.duration/100 that.audioCtx.seek(secend) },
总结
到此这篇关于微信小程序实现音频文件播放进度的实例代码的文章就介绍到这了,更多相关小程序音频文件播放进度内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

