Home  >  Article  >  Web Front-end  >  How to add your own recording in vue

How to add your own recording in vue

王林
王林Original
2023-05-24 09:00:361004browse

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>```

三、使用录音组件

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

dc6dce4a544fdca2df29d5ac0ea9906b

<RecordComponent />

16b28748ea4df4d9c2150843fecfba68
21c97d3a051048b8e55e3c8f199a54b2

3f1c4e4b6b16bbbd69b2ee476dc4f83a
import RecordComponent from "@/components/RecordComponent.vue";

export default {
name: "App",
components: {

RecordComponent,

},
};
2cacc6d41bbb37262a98f745aa00fbf0`

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!

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
Previous article:vue.js list to arrayNext article:vue.js list to array