19.[Vue入门]音乐播放器案例

完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>音乐播放器</title>
</head>
<body>
    <div id="player">
        <input type="text" v-model="query" @keyup.enter="searchMusic">
        <hr>
        <ul>
            <li v-for="item in musicList">
                <a href="javascript:;" @click="playMusic(item.id)">
                    <b>{{item.name}}</b>
                </a>
            </li>
        </ul>
        <hr>
        <div>
            <audio ref='audio' :src="musicUrl" controls autoplay loop></audio>
        </div>
        <div>
            <img :src="musicImg" style="width: 200px;height: 200px" v-show="musicImg!=''" />
        </div>
        <table style="border:1px solid #0094ff;">
            <tr>
                <th>头像</th>
                <th>昵称</th>
                <th>评论</th>
                <th>点赞</th>
            </tr>
            <tr v-for="comment in commentList">
                <td>
                    <img :src="comment.user.avatarUrl" style="width: 50px;height: 50px">
                </td>
                <td v-text="comment.user.nickname"></td>
                <td v-text="comment.content"></td>
                <td v-text="comment.likedCount"></td>
            </tr>
        </table>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios@0.21.1/dist/axios.min.js"></script>

    <script>
        /*
            https://autumnfish.cn/

            搜索歌曲
            https://autumnfish.cn/search?keywords=歌曲名

            歌曲地址
            https://autumnfish.cn/song/url?id=歌曲ID

            歌曲详情
            https://autumnfish.cn/song/detail?ids=歌曲ID

            歌曲评论
            https://autumnfish.cn/comment/hot?type=0&id=歌曲ID


         */

        var app = new Vue({
            el: '#player',
            data: {
                query: '',
                musicList: [],
                musicUrl: '',
                musicImg: '',
                commentList: []
            },
            methods: {
                // 歌曲地址
                searchMusic: function () {
                    var that = this;
                    axios.get('https://autumnfish.cn/search?keywords=' + this.query)
                        .then(function (response) {
                            console.log(response.data.result.songs);
                            that.musicList = response.data.result.songs;

                        }, function (err) {
                            console.log(err);
                        })
                },
                playMusic: function (musicId) {
                    var that = this;
                    console.log(musicId);
                    // 歌曲地址
                    axios.get('https://autumnfish.cn/song/url?id=' + musicId)
                        .then(function (response) {
                            console.log(response.data.data[0].url);
                            that.musicUrl = response.data.data[0].url;
                        }, function (err) {
                            console.log(err);
                        })
                    // 歌曲详情
                    axios.get('https://autumnfish.cn/song/detail?ids=' + musicId)
                        .then(function (response) {
                            console.log(response.data.songs[0].al.picUrl);
                            that.musicImg = response.data.songs[0].al.picUrl;
                        }, function (err) {
                            console.log(err);
                        })
                    // 歌曲评论
                    axios.get('https://autumnfish.cn/comment/hot?type=0&id=' + musicId)
                        .then(function (response) {
                            console.log(response.data.hotComments);
                            that.commentList = response.data.hotComments;
                        }, function (err) {
                            console.log(err);
                        })
                }

            }
        })
    </script>
</body>
</html>

19.[Vue入门]音乐播放器案例
https://元气码农少女酱.我爱你/688969c178db/
作者
元气码农少女酱
发布于
2023年5月28日
许可协议