Home > Web Front-end > Front-end Q&A > How to import songs into vue music

How to import songs into vue music

王林
Release: 2023-05-11 10:50:06
Original
731 people have browsed it

With the development of Internet technology, music has become an indispensable part of modern people's life. In the music playback interface, Vue.js, as one of the most popular JavaScript frameworks at present, is popular for its concise code and high reproducibility. So, how to import songs when building a music player using Vue.js? Below we will introduce the Vue.js song import method in detail.

First of all, please make sure you have installed Vue.js and imported Vue.js in your project.

  1. Create a song list

First, create a song list in your Vue.js project. Considering the scalability of the program, we do not recommend manually filling in each song in the html file, but using the data binding function of Vue.js to automatically generate a song list.

You can write the following code within the Vue.js component:

<template>
  <div>
    <ul>
      <li v-for="song in songs">
        {{song.name}}
      </li>
    </ul>
  </div>
</template>
<script>
  export default{
    data () {
      return {
        songs: [
          {'name': '歌曲1', 'src': 'http://xxx.mp3'},
          {'name': '歌曲2', 'src': 'http://xxx.mp3'},
          {'name': '歌曲3', 'src': 'http://xxx.mp3'}
        ]
      }
    }
  }
</script>
Copy after login

This code will automatically generate a list of three songs in your page. You need to replace the songs array with your song list.

  1. Import the song source file

Now, you have successfully generated a song list and assigned it to the songs attribute of the Vue.js component . Next, we need to import the song source files into the project.

You can import the song source file in the Vue.js component through the following code:

<template>
  <div>
    <ul>
      <li v-for="(song, index) in songs">
        <audio :src="song.url + '/' + song.fileName"></audio>
        {{song.name}}
      </li>
    </ul>
  </div>
</template>
<script>
  export default{
    data () {
      return {
        songs: [
          {'name': '歌曲1', 'url': 'http://xxx.com/songs', 'fileName': 'song1.mp3'},
          {'name': '歌曲2', 'url': 'http://xxx.com/songs', 'fileName': 'song2.mp3'},
          {'name': '歌曲3', 'url': 'http://xxx.com/songs', 'fileName': 'song3.mp3'}
        ]
      }
    }
  }
</script>
Copy after login

This code updates the song list to a file containing the song name, path and file where the song source file is located. Name object array, and use the <audio> tag to import each song source file into the page as a component block of Vue.js.

  1. Realize the playback of songs

After completing the import of songs, we also need to implement the playback function of the songs.

You can play the song through the following Vue.js code:

<template>
  <div>
    <ul>
      <li v-for="(song, index) in songs" 
        @click="playSong(index)">
        {{song.name}}
      </li>
      <audio ref="player"></audio>
    </ul>
  </div>
</template>
<script>
  export default{
    data () {
      return {
        songs: [
          {'name': '歌曲1', 'url': 'http://xxx.com/songs', 'fileName': 'song1.mp3'},
          {'name': '歌曲2', 'url': 'http://xxx.com/songs', 'fileName': 'song2.mp3'},
          {'name': '歌曲3', 'url': 'http://xxx.com/songs', 'fileName': 'song3.mp3'}
        ]
      }
    },
    methods: {
      playSong (index) {
        let player = this.$refs.player
        player.src = this.songs[index].url + '/' + this.songs[index].fileName
        player.play()
      }
    }
  }
</script>
Copy after login

This code contains a playSong(index) method, which is used when the user clicks Triggered when the song list is triggered, add the path of the specified song source file to the src attribute of the <audio> tag of Vue.js, and call play() Method to play songs.

Now, you have successfully built a simple music player using Vue.js and successfully imported songs.

Summary:

Vue.js is an extremely excellent javascript framework, which brings us a lot of convenience. In this article, we introduce the method of using Vue.js to import songs and realize the playback of songs. If you are not yet familiar with Vue.js, I hope this article will be helpful to you.

The above is the detailed content of How to import songs into vue music. For more information, please follow other related articles on the PHP Chinese website!

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