Home  >  Article  >  Web Front-end  >  How to add mp3 audio files by using vue

How to add mp3 audio files by using vue

亚连
亚连Original
2018-06-02 09:49:005491browse

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>

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;)
   }
}

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"
 }
}

3. Add the audio tag and introduce the resource file

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

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!

Statement:
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