当前位置:   article > 正文

uni-app实现全局音乐播放器_uniapp音乐全局播放器

uniapp音乐全局播放器

项目需要,要求每一页内需要有一个播放器,管理播放状态以及切换歌曲

实现思路:vuex存储播放状态以及歌曲列表等信息

  1. 根目录下创建store目录
  2. store下创建music.js

music.js内容

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. const store = new Vuex.Store({
  5. state: {
  6. url:'',//地址
  7. max:0,//总时长
  8. schedule:0,//进度
  9. list:[],//目录
  10. music:uni.createInnerAudioContext(),//创建播放器对象
  11. is_bf:false,//当前播放状态
  12. index:0,//索引
  13. },
  14. mutations: {
  15. //设置播放列表
  16. setList(state,list){
  17. state.list=list;
  18. },
  19. //设置播放路径
  20. setMusicUrl(state,index) {
  21. console.log(index)
  22. //设置索引
  23. state.index=index;
  24. //设置路径
  25. state.url = state.list[index].url;
  26. //播放器引入url
  27. state.music.src = state.list[index].url;
  28. //初始化播放进度
  29. state.music.startTime=0;
  30. //开始播放
  31. state.music.play(); //执行播放
  32. //设置播放状态
  33. state.is_bf=true;
  34. //获取播放进度
  35. state.music.onTimeUpdate(() => {
  36. //设置总时长
  37. state.max=state.music.duration;
  38. //修改进度
  39. state.schedule=state.music.currentTime;
  40. })
  41. },
  42. //修改播放进度
  43. setSchedule(state,num) {
  44. state.schedule = state.max*num;
  45. state.music.currentTime=state.schedule;
  46. },
  47. //开始播放音频
  48. play(state,num) {
  49. state.is_bf=true;
  50. state.music.play();
  51. },
  52. //暂停当前播放的音频
  53. pause(state,num) {
  54. state.is_bf=false;
  55. state.music.pause();
  56. },
  57. },
  58. })
  59. export default store

components目录下新建music.vue

  1. <template>
  2. <view class="music_box">
  3. <view class="left_box">
  4. <view class="prev" @click="prev(index)">
  5. 上一首
  6. </view>
  7. <view class="bf" @click="setBf(!is_bf)">
  8. <view v-if="!is_bf">播放</view>
  9. <view v-else>暂停</view>
  10. </view>
  11. <view class="next" @click="next(index,list)">
  12. 下一首
  13. </view>
  14. </view>
  15. <!-- 进度条 -->
  16. <view class="jd_box" @click="getXy">
  17. <view class="jd_n" :style="{width:schedule/max*100+'%'}"></view>
  18. </view>
  19. <!-- 时间 -->
  20. <view class="timer_box">
  21. {{getTimer(schedule)}}/{{getTimer(max)}}
  22. </view>
  23. <view class="list_icon" @click.stop="list_show=true">
  24. 列表
  25. <view class="list_box" v-show="list_show" :style="{height:list.length*30+'px'}" >
  26. <view v-for="(item,id) in list" :class="{active:index==id}" @click.stop="setIndex(id)">
  27. {{item.title}}
  28. </view>
  29. </view>
  30. </view>
  31. <!-- {{schedule}}/{{max}} -->
  32. </view>
  33. </template>
  34. <script>
  35. import {
  36. mapGetters,
  37. mapMutations,
  38. mutations,
  39. mapState
  40. } from 'vuex'
  41. export default {
  42. data() {
  43. return {
  44. list_show:false,
  45. }
  46. },
  47. //监听数据发生变化
  48. watch: {
  49. schedule(val) {
  50. }
  51. },
  52. created() {
  53. },
  54. mounted() {
  55. // this.setSchedule('123');//修改进度条时间
  56. },
  57. computed: {
  58. ...mapState(["schedule", "url", "max", "list","is_bf","index"])
  59. },
  60. methods: {
  61. ...mapMutations(['setSchedule', 'setTestArrInc','play','pause','setMusicUrl']),
  62. prev(index){
  63. if(index<=0){
  64. return
  65. }
  66. this.setMusicUrl(index-1)
  67. },
  68. next(index,list){
  69. if(index>=list.length-1){
  70. return
  71. }
  72. this.setMusicUrl(index+1)
  73. },
  74. setIndex(index){
  75. this.setMusicUrl(index);
  76. this.list_show=false;
  77. },
  78. getTimer(second) {
  79. let h = parseInt(second / 60 / 60 % 24);
  80. h = h < 10 ? '0' + h : h;
  81. let m = parseInt(second / 60 % 60);
  82. m = m < 10 ? '0' + m : m;
  83. let s = parseInt(second % 60);
  84. s = s < 10 ? '0' + s : s;
  85. let res = [h, m, s];
  86. return res[0]+':'+res[1]+':'+res[2]
  87. },
  88. getXy(e){
  89. console.log((e.detail.x-(e.currentTarget.offsetLeft*(document.documentElement.clientWidth/1920))))
  90. this.setSchedule(((e.detail.x/(document.documentElement.clientWidth/1920))-e.currentTarget.offsetLeft)/500);
  91. },
  92. //设置播放状态
  93. setBf(is){
  94. if(is){
  95. this.play();
  96. }else{
  97. this.pause();
  98. }
  99. }
  100. },
  101. }
  102. </script>
  103. <style>
  104. .music_box {
  105. width: 100%;
  106. height: 80px;
  107. background-color: rgba(0,0,0,0.7);
  108. position: fixed;
  109. bottom: 0;
  110. left: 0;
  111. z-index: 99;
  112. display: flex;
  113. color: #fff;
  114. align-items: center;
  115. padding: 0 60px;
  116. box-sizing: border-box;
  117. justify-content: center;
  118. font-size: 20px;
  119. }
  120. .left_box{
  121. margin-right: 20px;
  122. display: flex;
  123. align-items: center;
  124. }
  125. .bf{
  126. width: 50px;
  127. height: 50px;
  128. border: 2px solid #fff;
  129. border-radius: 50%;
  130. display: flex;
  131. align-items: center;
  132. justify-content: center;
  133. margin: 0 20px;
  134. }
  135. .jd_box{
  136. width: 500px;
  137. height: 15px;
  138. background-color: rgba(255,255,255,0.5);
  139. margin-right: 20px;
  140. position: relative;
  141. }
  142. .jd_n{
  143. height: 100%;
  144. background-color: rgba(255,255,255,0.8);
  145. }
  146. .list_icon{
  147. margin-left: 20px;
  148. padding: 5px;
  149. border: 1px solid #fff;
  150. position: relative;
  151. }
  152. .list_box{
  153. width: 200px;
  154. position: absolute;
  155. left: 0;
  156. background-color: rgba(0,0,0,0.7);
  157. bottom: 60px;
  158. }
  159. .list_box>view{
  160. width: 100%;
  161. text-align: center;
  162. font-size: 16px;
  163. line-height: 30px;
  164. border-bottom: 1px solid #ccc;
  165. }
  166. .active{
  167. background: red;
  168. }
  169. </style>

在需要播放器的页面内应用

  1. <script>
  2. import {
  3. mapGetters,
  4. mapMutations,
  5. mutations,
  6. mapState
  7. } from 'vuex'
  8. export default {
  9. data() {
  10. return {
  11. }
  12. },
  13. onLoad() {
  14. },
  15. methods: {
  16. ...mapMutations(['setList','setSchedule', 'setMusicUrl']),
  17. },
  18. mounted() {
  19. //设置目录
  20. this.setList([{
  21. title:'第一回',
  22. url:'http://sns-oss.oss-cn-beijing.aliyuncs.com/masterpieces/28/001.mp3'
  23. },{
  24. title:'第二回',
  25. url:'http://sns-oss.oss-cn-beijing.aliyuncs.com/masterpieces/28/002.mp3'
  26. }]);
  27. //设置播放索引
  28. this.setMusicUrl(0);
  29. }
  30. }
  31. </script>

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

闽ICP备14008679号