Home > Web Front-end > JS Tutorial > body text

How to add mp3 audio files by using vue

亚连
Release: 2018-06-02 09:49:00
Original
5432 people have browsed it

This article mainly introduces the method of adding mp3 audio files in vue. Now I will share it with you and give you a reference.

Sometimes we need to add audio files in vue, but when we place the audio files directly in the assets directory, we will find that they cannot play normally. The following are two commonly used configuration methods:

Method 1. Place the audio file in the static directory and then call it, as shown below

<audio class="success" 
    src="/static/audios/do_wrong.mp3">
</audio>
Copy after login

The above This way can solve this problem.

Method 2. Configure the mp3 format parser for the project

1. Add the loader to webpack.base.conf.js, as follows

{
   test: /\.(mp3)(\?.*)?$/,
   loader: &#39;url-loader&#39;,
   options: {
    name: utils.assetsPath(&#39;assets/[name].[hash:7].[ext]&#39;)
   }
}
Copy after login

2. Add a conversion attribute option for the src attribute of audio in the vue-loader.conf.js file to let vue-loader know that it needs to convert the src attribute of audio. Content is converted into modules.

var utils = require(&#39;./utils&#39;)
var config = require(&#39;../config&#39;)
var isProduction = process.env.NODE_ENV === &#39;production&#39;

module.exports = {
 loaders: utils.cssLoaders({
  sourceMap: isProduction
   ? config.build.productionSourceMap
   : config.dev.cssSourceMap,
  extract: isProduction
 }),
 transformToRequire: {
  "audio": "src"
 }
}
Copy after login

3. Add the audio tag and introduce the resource file

<audio autoplay="autoplay" 
    controls="controls"
    preload="auto"
    src="../assets/allright.mp3">
</audio>
Copy after login

At this time Resource files can be placed in the assets directory.

4. Restart the project or package and publish, and you can hear the sound.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

About two-way binding of parent components and child components in Vue2.x (detailed tutorial)

Introducing in detail the changes in v-for iteration syntax in Vue2.0 (detailed tutorial)

Looping through and loading different images in vue2.0 (detailed tutorial)

The above is the detailed content of How to add mp3 audio files by using vue. 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!