vue实现的网易云音乐在线播放和下载功能案例

时间:2021-05-26

本文实例讲述了vue实现的网易云音乐在线播放和下载功能。分享给大家供大家参考,具体如下:

效果如图:

完整代码:

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style lang=""> html, body { height: 100%; padding: 0; margin: 0; } #app { height: 100%; display: flex; } #app>#left { flex: 1; background-color: skyblue; text-align: center; overflow: scroll; } #app>#right { flex: 1; background-color: orange; } ul { list-style: none; padding: 0; } input { width: 469px; height: 56px; margin: 10px auto; border-radius: 10px; outline: none; font-size: 24px; border: 0; padding-left: 15px; } #left li { width: 451px; margin: 0 auto; font-weight: 700; border: 2px solid black; line-height: 35px; color: white; background-color: cadetblue; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; max-height: 35px; -webkit-line-clamp: 1; -webkit-box-orient: vertical; } #left li:hover { cursor: pointer; background-color: greenyellow; color: red; } #right { position: relative; overflow: scroll; } audio { display: block; margin: 0 auto; } .list-item { display: inline-block; margin-right: 10px; } .list-enter-active, .list-leave-active { transition: all 1s; } .list-enter, .list-leave-to{ opacity: 0; transform: translateX(100px); } .cover{ width: 260px; height: 260px; border-radius: 50%; display: block; margin: 10px auto; } @keyframes autoRotate{ to{ transform: rotateZ(360deg); } } .autoRotate{ animation-name:autoRotate; animation-iteration-count:infinite; animation-timing-function: linear; animation-duration:2s; animation-play-state:running; } .pause{ animation-play-state:paused; } .comment{ height: 150px; } .comment li{ display: flex; padding: 5px; } .comment li .left{ width: 120px; height: 120px; } .comment li .left img{ width: 100px; } .comment li a{ text-decoration: none; font-weight: bold; color: crimson; } </style></head><body> <div id="app"> <!-- 左边 --> <div id="left"> <input type="text" value="请输入你要搜索的歌名" v-model="inputValue" @keyup.enter="search"> <!-- 给li添加过渡 ;v-on:after-enter="afterEnter":钩子函数--> <transition-group name="list" tag="ul" v-on:after-enter="afterEnter"> <!-- play(item.id):把id传过去 --> <li v-for="(item, index) in musicList" :key="item.id" @dblclick="playMusic(item.id,item.album.id)" :style="{'transition-delay':index*100+'ms'}" > {{item.name}}-----演唱者:{{item.artists[0].name}} </li> </transition-group> </div> <!-- 右边,播放 --> <div id="right"> <!-- 专辑页面 --> <img :src="picUrl" alt="" class="cover autoRotate" :class="{pause:isPause}"> <!-- autoplay:自动播放,controls显示控件 ;@play="play"是自定义方法--> <audio :src="songUrl" autoplay controls @play="play" @pause="pause" ></audio> <h3>精彩评论</h3> <div class="comment"> <ul> <!-- 遍历数组时,需要动画时才用到key --> <li v-for="(item, index) in comments" > <div class="left"> <img :src="item.user.avatarUrl" alt=""> </div> <div class="right"> <a href="#" rel="external nofollow" >{{item.user.nickname}}</a> <p>{{item.content}}</p> </div> </li> </ul> </div> </div> </div> rightv></body><!-- 导入vue --><script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script><!-- 导入vue插件 --><script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script><script> //代码 let app = new Vue({ el: "#app", data: { inputValue: '',//输入的值 musicList: [], //存储歌列表 songUrl: '',//播放歌曲的url picUrl:'',//获取专辑信息 isPause:false,//专辑是否暂停 comments:[]//评论内容 }, methods: { // li标签过渡的事件 randomIndex: function () { return Math.floor(Math.random() * this.items.length) }, add: function () { this.items.splice(this.randomIndex(), 0, this.nextNum++) }, remove: function () { this.items.splice(this.randomIndex(), 1) }, //搜索歌曲事件 search() { //调用接口 this.$http.get(`https://autumnfish.cn/search?keywords=${this.inputValue}`).then(response => { // console.log(response); //将结果添加到musicList中 this.musicList = response.body.result.songs; }, response => { // error callback alert("出错了") }); }, // 双击播放歌曲事件,接收传过来的id playMusic(id,albumId) { //获取歌曲的url this.$http.get(`https://autumnfish.cn/song/url?id=${id}`).then(response => { // console.log(response); //将结果添加到musicList中 this.songUrl = response.body.data[0].url; }, response => { // error callback alert("出错了") }); // 获取专辑信息 this.$http.get(`https://autumnfish.cn/album?id=${albumId}`).then(res=>{ this.picUrl=res.body.album.blurPicUrl; }),err=>{} //获取评论内容接口 this.$http.get(`https://autumnfish.cn/comment/music?id=${id}&limit=1`).then(res=>{ console.log(res); this.comments=res.body.hotComments; }),err=>{ alert('信息错误') } }, //钩子函数:动画执行完后去除了style属性,不去掉会卡顿 afterEnter(el){ el.style=''; }, // 专辑图片旋转事件 play(){ console.log('播放'); this.isPause=false; }, pause(){ console.log('暂停'); this.isPause=true; } }, })</script></html>

如果接口不能使用:请登录https://github.com/huanggengzhong/NeteaseCloudMusicApi,重新下载开启服务器即可

希望本文所述对大家vue.js程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章