Vue+axio实现在线音乐播放器

概述:本教程仅实现了功能逻辑代码,不包含前端CSS样式。
工具:IntelliJ IDEA 2018

一,导入开发必要的环境

<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 官网提供的在线axios地址 -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

二,设计页面展示效果

<div id="app">
    <input type="text" v-model="name" placeholder="请输入歌手或者歌曲名" @keyup.enter="searchAll">
    <button @click="searchAll">搜索</button><br>
    <audio ref="audio" v-bind:src="musicUrl" controls autoplay loop></audio>
    <ul>
        <li v-for="item in songs">
           {{item.name}}
            <a href="javascript:;" @click="playMusic(item.id)">播放</a>
        </li>
    </ul>
</div>

三,填写点击响应事件

<script>
    var app = new Vue({
        el:"#app",
        data:{
            name:"",
            songs:[],
            musicUrl:""
        },
        methods:{
            searchAll:function () {
                var that = this;
                axios.get("https://autumnfish.cn/search?keywords="+this.name)
                    .then(function (response) {
                        console.log(response.data.result.songs);
                        that.songs = response.data.result.songs;
                    })
                    .catch(function (err) {  })
            },
            playMusic:function (musicId) {
                var that = this;
                axios.get("https://autumnfish.cn/song/url?id="+musicId)
                    .then(function (response) {
                         console.log(response);
                         that.musicUrl = response.data.data[0].url;
                }).catch(function (err) {  })
            }
        }
    })
</script>

四,效果展示:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值