父组件,json:"usingComponents": { "slider-audio": "../../com..._微信小程序 创建全局音频组件">
当前位置:   article > 正文

微信小程序自定义组件---音频组件_微信小程序 创建全局音频组件

微信小程序 创建全局音频组件

最近客户要求,在原有的音频功能上改动,使用户能够拖动进度条进行控制音频进度。

效果如下:

下面是代码:

父组件,wxml:

<slider-audio audio-src="{{audioUrl}}"></slider-audio>

父组件,json:

  1. "usingComponents": {
  2. "slider-audio": "../../components/audio/audio"
  3. }

父组件,js:

  1. Page({
  2. data: {
  3. audioUrl: "../../images/20191127164159_942.mp3"
  4. },
  5. onLoad: function(){
  6. }
  7. })

子组件,wxml:

  1. <view class="audio-container">
  2. <view class="audio-head">
  3. <image mode="widthFix" src="../../images/audio.png"></image>
  4. </view>
  5. <view class="audio-slider">
  6. <slider min="0" max="{{sliderMax}}" bindchange="sliderChange" block-size="14" value="{{sliderValue}}" activeColor="#eccb13" />
  7. <view class="audio-time">
  8. <text>{{currentTimeStr}}/{{durationTimeStr}}</text>
  9. </view>
  10. </view>
  11. <view class="audio-play" bindtap="changePlayState">
  12. <image mode="widthFix" src="{{audioPlay ? '../../images/pause.png' : '../../images/play.png'}}"></image>
  13. </view>
  14. </view>

子组件,json:

  1. {
  2. "component": true,
  3. "usingComponents": {}
  4. }

子组件,css:

  1. view,text,image,slider{
  2. box-sizing: border-box;
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .audio-container{
  7. width: 100%;
  8. display: flex;
  9. justify-content: space-between;
  10. align-items: center;
  11. }
  12. .audio-head{
  13. width: 60rpx;
  14. height: 60rpx;
  15. flex-shrink: 0;
  16. }
  17. .audio-head image{
  18. width: 100%;
  19. height: 100%;
  20. }
  21. .audio-slider{
  22. width: 78%;
  23. position: relative;
  24. padding: 0 20rpx;
  25. }
  26. .audio-time{
  27. width: 100%;
  28. text-align: right;
  29. font-size: 24rpx;
  30. line-height: 28rpx;
  31. }
  32. .audio-play{
  33. width: 40rpx;
  34. height: 40rpx;
  35. flex-shrink: 0;
  36. }
  37. .audio-play image{
  38. width: 100%;
  39. height: 100%;
  40. }

子组件,js:

  1. // components/audio.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. audioSrc: {
  8. type: String
  9. }
  10. },
  11. /**
  12. * 组件的初始数据
  13. */
  14. data: {
  15. audioPlay: false, //当前的播放状态控制
  16. sliderValue: 0, //进度条最小值
  17. sliderMax: 100, //进度条最大值
  18. backgroundAudioManager: "", //播放实例
  19. currentTimeStr: "00:00", //当前进度的时间
  20. durationTimeStr: "00:00", //总的时间
  21. changeValState: false //在拉动进度条的时候,拉动完成的状态
  22. },
  23. //生命周期函数列表
  24. lifetimes: {
  25. // 在组件实例进入页面节点树时执行
  26. attached: function () {
  27. console.log("播放资源", this.data.audioSrc);
  28. //this.creatAudio();
  29. },
  30. // 在组件实例被从页面节点树移除时执行
  31. detached: function () {
  32. }
  33. },
  34. /**
  35. * 组件的方法列表
  36. */
  37. methods: {
  38. creatAudio: function(){
  39. let that = this;
  40. that.data.backgroundAudioManager = wx.getBackgroundAudioManager();
  41. that.data.backgroundAudioManager.title = '音频';
  42. that.data.backgroundAudioManager.onPlay(function () { //播放监听
  43. console.log('播放!');
  44. that.setData({
  45. audioPlay: true
  46. })
  47. })
  48. that.data.backgroundAudioManager.onPause(function(){ //暂停监听
  49. console.log('暂停播放!');
  50. that.setData({
  51. audioPlay: false
  52. })
  53. })
  54. that.data.backgroundAudioManager.onEnded(function(){ //结束播放监听
  55. console.log('播放结束!');
  56. that.setData({
  57. audioPlay: false
  58. })
  59. })
  60. that.data.backgroundAudioManager.onError(function(res){// 播放失败监听
  61. console.log('播放音频失败' , res);
  62. })
  63. that.data.backgroundAudioManager.onSeeked(function(res){ //监听结束跳转事件callback(无效)
  64. console.log("结束跳转", res);
  65. that.setData({
  66. changeValState: false
  67. })
  68. })
  69. that.data.backgroundAudioManager.onTimeUpdate(function (res) { //监听背景音频播放进度更新事件
  70. if (that.data.changeValState) return false;
  71. let currentTime = that.data.backgroundAudioManager.currentTime;
  72. let duration = that.data.backgroundAudioManager.duration;
  73. if (that.data.sliderMax==100){
  74. that.setData({
  75. sliderMax: duration.toFixed(0)
  76. })
  77. }
  78. let val = parseInt((currentTime / duration) * that.data.sliderMax);
  79. that.setData({
  80. sliderValue: val
  81. })
  82. let currTimeStr = that.formatTime(currentTime);
  83. let duraTimeStr = that.formatTime(duration);
  84. that.setData({
  85. currentTimeStr: currTimeStr,
  86. durationTimeStr: duraTimeStr
  87. })
  88. })
  89. },
  90. //滑动条改变事件
  91. sliderChange: function (e) {
  92. let that = this;
  93. console.log(e);
  94. that.setData({
  95. sliderValue: e.detail.value
  96. })
  97. if (that.data.backgroundAudioManager=="") return false;
  98. that.setData({
  99. changeValState: true
  100. })
  101. that.data.backgroundAudioManager.seek(e.detail.value);
  102. setTimeout(function(){
  103. that.setData({
  104. changeValState: false
  105. })
  106. },500)
  107. },
  108. //播放按钮点击事件
  109. changePlayState: function(){
  110. let that = this;
  111. this.setData({
  112. audioPlay: !this.data.audioPlay
  113. })
  114. if (this.data.audioPlay){
  115. if (!that.data.backgroundAudioManager.src) { //初始化给backgroundAudioManager.src赋值
  116. that.creatAudio();
  117. that.data.backgroundAudioManager.src = that.data.audioSrc; //当设置了src后会自动播放
  118. }
  119. that.data.backgroundAudioManager.play();
  120. } else {
  121. that.data.backgroundAudioManager.pause();
  122. }
  123. },
  124. formatTime: function(num){ //格式化时间格式
  125. num = num.toFixed(0);
  126. let second = (num%60);
  127. if(second<10) second = '0' + second;
  128. let min = Math.floor(num / 60);
  129. if (min < 10) min = '0' + min;
  130. return min+":"+second;
  131. }
  132. }
  133. })

翻阅了微信小程序的很多api,发现只有这一个比较好用,其他的基本不是在开发工具上无法播放,就是在手机上无法播放,按理说,基础库版本都支持的,但是确实没有任何反应,我微信开发者工具调试基础库为:2.9.4,使用InnerAudioContext无法播放声音,手机上可以。

另外,在这篇例子中,开发者工具和手机上都无法监听到:backgroundAudioManager.onSeeked和backgroundAudioManager.onSeeking回调函数,不知道是小程序的bug还是我使用的问题。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1019901
推荐阅读
相关标签
  

闽ICP备14008679号