In recent years, with the popularity of mobile applications, the demand for voice recording and uploading has been increasing day by day. In the Vue framework, combined with the new features of HTML5, we can easily implement the APP recording function and upload it. This article will share how to use Vue to implement the APP recording function and upload it, so that you can easily implement the voice recording and uploading function.
1. Preconditions
First of all, we need to make it clear that the recording function needs to use the new features of HTML5, so before implementing the recording function, we need to check the browser compatibility sex.
In 2019, most mobile browsers already support new HTML5 features, but there are still a few browsers that are incompatible. In practical applications, we can use the Modernizr library to check whether the browser supports new HTML5 features.
2. Implementation steps
Before installing Vue, we need to install Node.js and NPM. After the installation is complete, open the command line tool and execute the following command to install Vue-cli:
npm install -g vue-cli
After the installation is complete, we can use the following command to create a new Vue project:
vue init webpack my-project
where , "my-project" is the project name, which can be defined by the user. Next, just follow the steps prompted by the command line to configure it.
After creating the Vue project, we need to install some necessary dependencies, includingvue-audio-recorder
andaxios
. First, we need to installvue-audio-recorder
using the following command:
npm install vue-audio-recorder --save
Then, use the following command to installaxios
:
npm install axios --save
The above two This dependency is a must to implement the recording and uploading functions.
After completing the environment configuration and dependency installation, we need to write code to implement the recording function. First, import the following code in the Vue component:
import AudioRecorder from 'vue-audio-recorder' import axios from 'axios'
Then, define the following state in the Vue component:
data() { return { audioSrc: '', audioName: '' }; }
Among them,audioSrc
represents the link address of the recording,audioName
is the name of the recording file.
Next, write the following code in the Vue component to implement recording:
mounted() { this.audioRecorder = new AudioRecorder({ onComplete: (blob)=>{ // 上传录音文件 this.uploadAudio(blob); }, }); }, methods: { startRecord() { this.audioRecorder.start(); }, stopRecord() { return this.audioRecorder.stop() .then((blob) => { // 保存录音blob对象到变量中 this.audioBlob = blob; // 设置录音链接地址 this.audioSrc = URL.createObjectURL(blob) }) .catch(() => { console.log('Media recording failed'); }); }, uploadAudio(audioFile) { var formData = new FormData(); formData.append("audio", audioFile, this.audioName); axios.post('/api/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' } }).then(response => { console.log(response.data); if (response.status == 200) { console.log("上传成功"); } else { console.log("上传失败"); } }).catch(() => { console.log("网络异常"); }) } }
Among them,audioRecorder
is the recording object, andonComplete
indicates that after the recording is completed The callback function,startRecord
is the start recording function,stopRecord
is the stop recording function, and theuploadAudio
function is used to upload the recording file.
So far, we have completed the implementation of the recording function. Users can add a recording button in the Vue template, and call thestartRecord
function in the button's@click
event to start recording, and call thestopRecord
function when the recording ends. to end the recording and upload it.
3. Summary
Through the above steps, we can easily implement Vue’s APP recording function and upload it. By using the new features of HTML5 and the Vue framework, we can easily accomplish this task. The key to implementation is to use thevue-audio-recorder
library andaxios
dependencies to make the whole process simple and enjoyable.
The above is the detailed content of How to use Vue to implement APP recording function and upload function. For more information, please follow other related articles on the PHP Chinese website!