当前位置:   article > 正文

前端页面 h5使用vue搭建带播放列表切换的音频播放器 audio_vue2 h5 音乐播放器

vue2 h5 音乐播放器

一个h5的音频播放器,播放列表播放器上下首切换 样式效果对应起来。

五秒后自动收缩形成挂件(不可移动)。点击播放器图片 即可展开、收起。

audio.js    audio.css  资源下载

详细代码上篇

样式

                         

部分代码:

html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta name="format-detection" content="telephone=no"/>
  6. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
  7. <title>详情页</title>
  8. <script src="https***query_172.js" language="javascript"></script>
  9. <script src="https://cdn.bootcss.com/vue/2.5.2/vue.min.js"></script>
  10. <!--动画样式-->
  11. <link href="http:***/animate.css" rel="stylesheet" type="text/css">
  12. <!--音频播放器-->
  13. <link rel="stylesheet" type="text/css" href="./css/audio.css">
  14. <script type="text/javascript" src="./js/audio.js"></script>
  15. </head>
  16. <body>
  17. ****
  18. ****
  19. ****
  20. <!-- 播放列表 -->
  21. <ul :class="mp3_count >= 1?'music':'hid'" id="storyLin">
  22. <li v-for="(list,key) in data.mp3_list" @click="playSon(this,key)" :class="{'on':clicked==key}">
  23. <div :class="[{'hid':clicked ==key}, num]">{{key+1}}</div>
  24. <div :class="[{'hid':clicked !=key}, laba]">&#xe65e;</div>
  25. <div class="name">
  26. <p>{{list.title}}</p>
  27. <div class="dat">
  28. <span>&#xe60c;</span>{{list.num}}人
  29. <span style="margin-left: 20px;">&#xe68d;</span>{{list.time}}
  30. </div>
  31. </div>
  32. </li>
  33. </ul>
  34. ****
  35. ****
  36. ****
  37. <!-- <div :class="mp3_count >= 1 ? 'audio-box hid':'hid'">-->
  38. <div :class="[{'hid':mp3_count < 1}, audio_box]">
  39. <div class="audio-container">
  40. <div class="audio-cover" @click="goRight()"></div>
  41. <div class="audio-view">
  42. <h3 class="audio-title">未知歌曲</h3>
  43. <div class="audio-body">
  44. <div class="audio-backs">
  45. <div class="audio-this-time">00:00</div>
  46. <div class="audio-count-time">00:00</div>
  47. <div class="audio-setbacks">
  48. <i class="audio-this-setbacks">
  49. <span class="audio-backs-btn"></span>
  50. </i>
  51. <span class="audio-cache-setbacks">
  52. </span>
  53. </div>
  54. </div>
  55. </div>
  56. <div class="audio-btn">
  57. <div class="audio-select">
  58. <div class="audio-prev"></div>
  59. <div class="audio-play"></div>
  60. <div class="audio-next"></div>
  61. <!-- <div class="audio-menu"></div>-->
  62. <!-- <div class="audio-volume"></div>-->
  63. </div>
  64. <div class="audio-set-volume">
  65. <div class="volume-box">
  66. <i><span></span></i>
  67. </div>
  68. </div>
  69. <div class="audio-list">
  70. <div class="audio-list-head">
  71. <p>☺随心听</p>
  72. <span class="menu-close">关闭</span>
  73. </div>
  74. <ul class="audio-inline">
  75. </ul>
  76. </div>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. <!--音频播放器-->
  82. </div>
  83. </body>
  84. </html>

js方法

  1. var api = "htt***p";
  2. var song = [];//定义播放列表(可后续添加)
  3. var audioFn = [];//播放器类
  4. var main = new Vue({
  5. el: '#main',
  6. data: {
  7. ****
  8. audio_type: 2,//播放器是否展示隐藏,
  9. clicked: -1,
  10. num: 'num',
  11. laba: 'laba',
  12. audio_box:'audio-box audio-boxhid'
  13. },
  14. created() {
  15. var that = this;
  16. that.get_data();//内含实例化 播放器
  17. },
  18. methods: {
  19. //获取专辑信息
  20. get_data() {
  21. var that = this;
  22. $.get(
  23. api + '?meth***r_id=' + that.user_id + '&id=' + that.id,
  24. function (data) {
  25. if (data.ret) {
  26. that.data = data.data;
  27. //音频列表赋值,并实例化音频组件
  28. song = that.data.mp3_list;//播放列表
  29. that.mp3_count = song.length
  30. if (that.mp3_count >= 1) {
  31. that.new_audio(song);
  32. }
  33. } else {
  34. alert('数据获取失败!')
  35. }
  36. },
  37. 'json'
  38. )
  39. },
  40. //实例化音频播放
  41. new_audio(song) {
  42. var that = this;
  43. audioFn = audioPlay({
  44. song: song,
  45. autoPlay: false //是否立即播放第一首,autoPlay为true且song为空,会alert文本提示并退出
  46. });
  47. //获取音频播放key
  48. if (!songEq) {
  49. songEq = -1
  50. }
  51. //监听当前播放曲目的key值,对应播放列表样式
  52. setInterval(function () {
  53. that.clicked = songEq;
  54. }, 300)
  55. },
  56. /*
  57. 音频播放器相关操作
  58. */
  59. playSon(th, key) {
  60. var that = this;
  61. clearTimeout(that.clock);//清除定时器
  62. $(".audio-box").removeClass("hid");
  63. $(".audio-box").removeClass("audio-boxhid");
  64. audioFn.selectMenu(key, true);
  65. that.clicked = key;
  66. that.clock = setTimeout(function () {
  67. $(".audio-box").addClass("audio-boxhid");
  68. that.audio_type = 2;
  69. }, 5000);
  70. },
  71. //控制播放器是否展开
  72. goRight() {
  73. var that = this;
  74. //清除定时器
  75. clearTimeout(that.clock);
  76. //播放器是否收起
  77. if (that.audio_type == 1) {
  78. $(".audio-box").addClass("audio-boxhid");
  79. that.audio_type = 2;
  80. } else {
  81. $(".audio-box").removeClass("audio-boxhid");
  82. that.audio_type = 1;
  83. }
  84. //5秒定时器 过后自动收起
  85. that.clock = setTimeout(function () {
  86. $(".audio-box").addClass("audio-boxhid");
  87. that.audio_type = 2;
  88. }, 5000);
  89. }
  90. }
  91. })
  92. /*
  93. $(function () {
  94. // 向歌单中添加新曲目,第二个参数true为新增后立即播放该曲目,false则不播放
  95. audioFn.newSong({
  96. 'cover': 'images/audio/cover4.jpg',
  97. 'src': 'http://filebaby.qubaobei.com/story/low/105.mp3',
  98. 'title': '极乐净土 - GARNiDELiA'
  99. }, false);
  100. // 暂停播放
  101. audioFn.stopAudio();
  102. // 开启播放
  103. audioFn.playAudio();
  104. // 选择歌单中索引为3的曲目(索引是从0开始的),第二个参数true立即播放该曲目,false则不播放
  105. audioFn.selectMenu(3,true);
  106. // 查看歌单中的曲目
  107. console.log(audioFn.song);
  108. // 当前播放曲目的对象
  109. console.log(audioFn.audio);
  110. });
  111. */

 

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

闽ICP备14008679号