How to add your own recording in vue

王林
Release: 2023-05-24 09:00:36
Original
981 people have browsed it

How to add your own recording in Vue

With the popularity of mobile Internet, audio recording has gradually become a standard feature of many applications. For example, applications such as audio notes and language learning all require voice recording functions. It is also very easy to add your own recording function in the Vue framework. Below we will introduce how to add your own recording in Vue.

1. Install dependencies

Vue implements the recording function by installing third-party dependencies. We can use the Chinese name of the library called "recorder" and install it through npm:

npm install record-vue

2. Add the recording component

Next we need to add a recording component to Vue. This recording component allows you to easily record audio and save the recorded audio in browser storage. Let’s take a look at the following code:

  <div>
    <button @click="start">开始录制</button>
    <button @click="stop">停止录制</button>
    <audio ref="audio"></audio>
  </div>
</template>

<script>
import Record from 'record-vue';

export default {
  name: 'RecordComponent',

  components: {
    Record
  },

  data() {
    return {
      mediaRecorder: null,
    }
  },

  methods: {
    start() {
      this.mediaRecorder = this.$refs.recorder.getMediaRecorder();
      this.mediaRecorder.start();
    },

    stop() {
      this.mediaRecorder.stop();
    }
  },

  mounted() {
    this.mediaRecorder = this.$refs.recorder.getMediaRecorder();

    this.mediaRecorder.ondataavailable = (blob) => {
      const audio = this.$refs.audio;
      audio.src = URL.createObjectURL(blob);
    }
  }
}
</script>```

三、使用录音组件

通过上面的代码,我们定义了一个包含开始录制、停止录制和音频播放的录音组件。现在,我们只需要在需要使用录音的组件中引入录音组件,然后使用它即可。例如,我们可以在下面的代码中使用录音组件:
Copy after login

<RecordComponent />
Copy after login


<script><br>import RecordComponent from "@/components/RecordComponent.vue";</p><p>export default {<br> name: "App",<br> components: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>RecordComponent,</pre><div class="contentsignin">Copy after login</div></div><p>},<br>};<br></script>`

We use this recording component as a subcomponent of another Vue component, and then start the recording function by clicking the button, and during the recording Stop recording when finished. Of course, in actual application, you can adjust it according to your own needs.

4. Summary

This article introduces the steps to add your own recording function in Vue. To implement the recording function in Vue, you need to use a third-party library, then add the recording component and introduce it where recording is needed. Now, you can try to add your own recording function in Vue and learn more about Vue.

The above is the detailed content of How to add your own recording in vue. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!