• 使用vue实现简易的音乐播放器


    先看效果

    代码中使用的ivewUI前端框架

    使用的是font-awesome字体图标  需要先安装 npm install font-awesome --save

    1. <template>
    2. <Card style="width:100%">
    3. <template #title >
    4. <Icon type="ios-musical-notes">Icon>
    5. 音乐播放器
    6. template>
    7. <audio ref="audio" controls="controls" style="width:100%;" :preload="preload">
    8. <source />
    9. audio>
    10. <h4 style="width: 100%;height: 50px;line-height: 50px; text-align: center;">{{namesong}}h4>
    11. <div wrap style="width: 100%; height: 150px;line-height: 150px; text-align: center;">
    12. <ButtonGroup shape="circle">
    13. <Button type="info" title="上一首" size="large" @click="up(Indexsong)"><i class="fa fa-backward">i> Button>
    14. <Button type="info" title="播放/暂停" size="large" @click="play(namesong,Indexsong)"><i :class="playButton">i>Button>
    15. <Button type="info" title="下一首" size="large" @click="down(Indexsong)" ><i class="fa fa-forward">i>Button>
    16. <Button type="info" title="列表" size="large" @click="IssongListshowhide" ><i class="fa fa-th-list">i>Button>
    17. ButtonGroup>
    18. div>
    19. <Table :columns="columns" :data="songList" v-show="songListhidden" @click="IssongListshowhide">Table>
    20. Card>
    21. template>
    22. <script>
    23. export default {
    24. data () {
    25. return {
    26. optiontype:["up","play","down"],
    27. preload:'auto',
    28. Indexsong:0,
    29. namesong: '',
    30. playButton:'fa fa-play',
    31. musicUrl:'',
    32. columns: [
    33. {
    34. type: 'index',
    35. title: '序号',
    36. align: 'center',
    37. width: 100,
    38. render: (h, params) => {
    39. return h(
    40. 'span',
    41. params.index
    42. )
    43. }
    44. },
    45. {
    46. title: '歌曲',
    47. key: 'song'
    48. },
    49. {
    50. title: '操作',
    51. render: (h, params) => {
    52. return h('div', [
    53. h(
    54. 'Button',
    55. {
    56. props: {
    57. type: 'info',
    58. size: 'small'
    59. },
    60. style: {
    61. marginRight: '5px'
    62. },
    63. on: {
    64. click: () => {
    65. this.play(params.row.song,params.index);
    66. }
    67. }
    68. },
    69. '播放'
    70. )
    71. ])
    72. }
    73. }
    74. ],
    75. songList: [
    76. {
    77. song: '金莎-星月神话.mp3',
    78. },
    79. {
    80. song: '萌萌哒天团-帝都.mp3',
    81. },
    82. {
    83. song: '文武贝-夜的钢琴曲5.mp3',
    84. },
    85. {
    86. song: '乌兰托娅-花桥流水.mp3',
    87. },
    88. {
    89. song: '许嵩-山水之间.mp3',
    90. },
    91. {
    92. song: '张杰-三生三世.mp3',
    93. }
    94. ],
    95. songListhidden:false
    96. }
    97. },
    98. // computed: { ///存在问题,未能 到底预期效果以换他方式created里实现
    99. // namesong:{
    100. // // setter
    101. // get() {
    102. // console.log("get:"+)
    103. // return this.songList[1].song;
    104. // }
    105. // ,
    106. // set(newval) {
    107. // console.log("set:"+newval)
    108. // return newval;
    109. // }
    110. // }
    111. // },
    112. methods:{
    113. //切换上一曲
    114. up(index){
    115. let vm = this;
    116. if(index===0){
    117. this.$Message.success({ title: '提示', content: '已经到顶了喔' })
    118. return
    119. }
    120. vm.Indexsong=--index;
    121. vm.playButton='fa fa-pause';
    122. vm.namesong=vm.songList[vm.Indexsong].song;
    123. console.log("Indexsong:"+vm.Indexsong+",namesong:"+vm.namesong)
    124. let audioplay= this.$refs.audio//播放
    125. vm.musicUrl= require("@/assets/Music/"+vm.namesong)
    126. audioplay.src = vm.musicUrl;
    127. audioplay.play();
    128. },
    129. //切换下一曲
    130. down(index){
    131. let vm = this;
    132. if(index===vm.songList.length-1){
    133. this.$Message.success({ title: '提示', content: '已经到底了喔' })
    134. return
    135. }
    136. vm.Indexsong=++index;
    137. vm.playButton='fa fa-pause';
    138. vm.namesong=vm.songList[vm.Indexsong].song;
    139. console.log("Indexsong:"+vm.Indexsong+",namesong:"+vm.namesong)
    140. let audioplay= this.$refs.audio
    141. vm.musicUrl= require("@/assets/Music/"+vm.namesong)
    142. audioplay.src = vm.musicUrl;
    143. audioplay.play();//播放
    144. },
    145. play(song,index){
    146. let vm = this;
    147. vm.Indexsong=index;
    148. console.log("Indexsong:"+
    149. vm.Indexsong+",song:"+song+",+playButton:"+vm.playButton)
    150. if(vm.namesong===song){
    151. if(vm.playButton==="fa fa-play"){
    152. vm.playButton='fa fa-pause';
    153. let audioplay= this.$refs.audio
    154. vm.musicUrl= require("@/assets/Music/"+vm.namesong)
    155. audioplay.src = vm.musicUrl;
    156. audioplay.play();//播放
    157. }else{
    158. vm.playButton="fa fa-play";
    159. this.$refs.audio.pause();//暂停
    160. }
    161. }else if(vm.namesong!=song){
    162. console.log("song:"+song)
    163. console.log("playButton:"+vm.playButton)
    164. vm.playButton='fa fa-pause';
    165. vm.namesong=song;
    166. let audioplay= this.$refs.audio
    167. console.log("namesong:"+vm.namesong)
    168. vm.musicUrl= require("@/assets/Music/"+vm.namesong)
    169. audioplay.src = vm.musicUrl;
    170. audioplay.play();//播放
    171. }
    172. },
    173. IssongListshowhide(){
    174. let vm = this;
    175. vm.songListhidden = !vm.songListhidden;
    176. }
    177. }
    178. ,
    179. created(){
    180. //赋值变量
    181. this.namesong = this.songList[1].song;
    182. this.Indexsong=1;
    183. }
    184. }
    185. script>

  • 相关阅读:
    用AR Engine手部骨骼跟踪能力实现虚拟手表试戴
    技术架构职责和应该注意哪些
    微服务框架 SpringCloud微服务架构 8 Gateway 网关 8.7 网关的cors 跨域配置
    嵌入式基础知识学习:Flash、EEPROM、RAM、ROM
    设计模式-享元模式(Flyweight)
    【ArcGIS Pro二次开发】(70):杂七杂八的记录
    C++环形缓冲区
    【Web基础】Web应用体系结构 — 容器 + MVC设计模式
    php资源列表|开发者应知晓
    Spring初始化项目
  • 原文地址:https://blog.csdn.net/qq_42335551/article/details/126200972