Home > Web Front-end > Vue.js > body text

How to create a music player using Vue and NetEase Cloud API

WBOY
Release: 2023-07-19 21:57:21
Original
1693 people have browsed it

How to create a music player using Vue and NetEase Cloud API

In today's digital age, music has become an indispensable part of people's lives. As the Internet has grown, music streaming services have become more and more popular. NetEase Cloud Music is one of the most popular streaming music platforms in China. It provides a wealth of music resources and has an open API for developers to use. This article will introduce how to use the Vue framework and NetEase Cloud API to create a simple music player.

First, we need to create a Vue project. Open the command line tool and execute the following command in the appropriate directory:

vue create music-player
Copy after login

Select the default configuration and wait for the project to be created. Enter the project directory and install the Axios library for sending HTTP requests:

cd music-player
npm install axios --Save
Copy after login

Next, we need to apply for a NetEase Cloud developer account and obtain the API access key. Go to the official website of NetEase Cloud Music Open Platform, register a developer account and create an application. The obtained access key will be used for subsequent API requests.

Create a file named api.js in the project to encapsulate the interaction with NetEase Cloud API:

import axios from 'axios';

const API_BASE_URL = 'https://api.music.163.com';

export default {
  async searchSongs(keyword) {
    const response = await axios.get(
      `${API_BASE_URL}/search?keywords=${encodeURIComponent(keyword)}`
    );
    return response.data;
  },

  async getSongUrl(id) {
    const response = await axios.get(`${API_BASE_URL}/song/url?id=${id}`);
    return response.data;
  },
};
Copy after login

The above code encapsulates two API requests . searchSongsThe function receives a keyword parameter, initiates a search request, and returns data containing the search results. getSongUrlThe function receives a music ID parameter and returns the playback address of the music.

Next, we can use these encapsulated APIs in Vue components. Create a component file named Player.vue and write the following code:

<template>
  <div class="player">
    <input type="text" v-model="searchKeyword" @keyup.enter="searchSongs" placeholder="搜索歌曲" />
    <ul>
      <li v-for="song in songs" :key="song.id" @click="playSong(song.id)">{{ song.name }}</li>
    </ul>
    <audio :src="currentSongUrl" controls autoplay></audio>
  </div>
</template>

<script>
import api from '@/api';

export default {
  data() {
    return {
      searchKeyword: '', // 搜索关键字
      songs: [], // 搜索结果
      currentSongUrl: '', // 当前播放音乐的URL
    };
  },

  methods: {
    async searchSongs() {
      const response = await api.searchSongs(this.searchKeyword);
      this.songs = response.result.songs;
    },

    async playSong(id) {
      const response = await api.getSongUrl(id);
      this.currentSongUrl = response.data[0].url;
    },
  },
};
</script>

<style scoped>
.player {
  max-width: 600px;
  margin: auto;
  padding: 20px;
  text-align: center;
}

input {
  width: 100%;
  height: 40px;
  margin-bottom: 10px;
  padding: 0 10px;
}

ul {
  list-style: none;
  padding: 0;
}

li {
  cursor: pointer;
  margin-bottom: 10px;
}

audio {
  width: 100%;
  margin-top: 20px;
}
</style>
Copy after login

The above code defines a simple music player component. There is a search input box and a music list in the component. Users can enter keywords to search for songs and click on the list items to play them. The player uses the <audio> tag of HTML5 to implement the playback function by binding the src attribute to the URL of the current music.

Finally, register this component in your main Vue file (usually main.js):

import Vue from 'vue';
import App from './App.vue';
import Player from './Player.vue';

Vue.component('Player', Player);

new Vue({
  render: h => h(App),
}).$mount('#app');
Copy after login

Now, run your Vue project:

npm run serve
Copy after login

Openhttp://localhost:8080 in the browser, you will see a simple music player interface. You can enter song keywords in the search box, and the search results will be displayed in the list. Click on a song in the list to play the music using HTML5's <audio> tag.

This is just a simple example, you can further extend this music player according to your needs. For example, you can add playlist functions, music cover display, etc. to make it more complete. At the same time, NetEase Cloud Music provides more API interfaces to obtain song details, lyrics and other information, which you can explore and try on your own.

To summarize, this article introduces how to use the Vue framework and NetEase Cloud API to create a simple music player. Through simple code examples, the basic functions of searching for songs and playing music are demonstrated. I hope this article can help you start developing your own music player using Vue and NetEase Cloud API.

The above is the detailed content of How to create a music player using Vue and NetEase Cloud API. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!