知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 小程序相關(guān) >
uni-app小程序錄音上傳解決方案
發(fā)表時(shí)間:2021-1-5
發(fā)布人:葵宇科技
瀏覽次數(shù):83
能力依賴
RecorderManager 全局唯一的錄音管理器
錄音功能的要求與限制
- 與當(dāng)前頁(yè)面其他 音頻播放/錄音 功能互斥
- 是否在錄音中狀態(tài)顯示
-
結(jié)束/不需要錄音時(shí),回收
RecorderManager
對(duì)象
材料
可以/結(jié)束 錄音
錄音中Codeing(結(jié)果代碼直接看最后)
構(gòu)造一個(gè)簡(jiǎn)單的 DOM
結(jié)構(gòu)
@click="recordAction" :src=http://www.wxapp-union.com/"recordImg" class="record"/>
先實(shí)現(xiàn)小程序的錄音功能
import iconRecord from '../../static/images/icon_record.png'
import iconRecording from '../../static/images/icon_recording.png'
// ...
data() {
recordImg: iconRecord, // 錄音按鈕的圖標(biāo)
rm: null, // 錄音管理器
},
// ...
mounted() {
if (this.rm === null) { // 錄音管理器如果沒有初始化就先初始化
this.rm = uni.getRecorderManager()
}
// 綁定回調(diào)方法
this.rm.onStart((e) => this.onStart(e))
this.rm.onPause((e) => this.onPause(e))
this.rm.onResume((e) => this.onResume(e))
this.rm.onInterruptionBegin((e) => this.onInterruptionBegin(e))
this.rm.onInterruptionEnd((e) => this.onInterruptionEnd(e))
this.rm.onError((e) => this.onError(e))
},
// ...
methods: {
// ...
recordAction() {
if (this.recordImg === iconRecord) {
// 設(shè)置格式為MP3,最長(zhǎng)60S,采樣率22050
this.rm.start({
duration: 600000,
format: 'mp3',
sampleRate: 22050,
})
// 開始錄音后綁定停止錄音的回調(diào)方法
this.rm.onStop((e) => this.onStop(e))
} else if (this.recordImg === iconRecording) {
this.rm.stop()
},
},
onStart(e) {
console.log('開始錄音', this.question, this.subQuesIndex)
this.recordImg = iconRecording
console.log(e)
},
onPause(e) {
console.log(e)
afterAudioRecord()
},
onResume(e) {
console.log(e)
},
onStop(e) {
console.log(e)
this.recordImg = iconRecord
// 結(jié)束錄音之后上傳錄音
this.uploadMp3Action(e)
},
onInterruptionBegin(e) {
console.log(e)
},
onInterruptionEnd(e) {
console.log(e)
},
onError(e) {
console.log(e)
},
uploadMp3Action(e) {
// TODO uploadMp3
},
},
只能同時(shí)有一個(gè)錄音,與音頻播放互斥
-
globalData
中增加兩個(gè)屬性audioPlaying
,audioRecording
// src/App.vue
export default {
globalData: {
// 保證全局只有一個(gè)音頻處于播放狀態(tài)且錄音與播放操作互斥
audioPlaying: false,
audioRecording: false,
},
// ...
},
-
Util
中增加判斷方法
// src/lib/Util.js
// 結(jié)束錄音之后釋放錄音能力
export function afterAudioRecord() {
getApp().globalData.audioRecording = false
}
// 結(jié)束音頻播放之后釋放音頻播放能力
export function afterAudioPlay() {
getApp().globalData.audioPlaying = false
}
/**
* 判斷是否可以錄音或者播放
* @param {string} type play | record
*/
export function beforeAudioRecordOrPlay(type) {
const audioPlaying = getApp().globalData.audioPlaying
const audioRecording = getApp().globalData.audioRecording
if (audioPlaying ||audioRecording) {
uni.showToast({
title: audioPlaying ? '請(qǐng)先暫停其他音頻播放' : '請(qǐng)先結(jié)束其他錄音',
icon: 'none'
})
return false
} else {
if (type === 'play') {
getApp().globalData.audioPlaying = true
} else if (type === 'record') {
getApp().globalData.audioRecording = true
} else {
throw new Error('type Error', type)
}
return true
}
}
-
改造原有
recordAction
方法
import { beforeAudioRecordOrPlay, afterAudioRecord} from '../../lib/Utils';
// ...
recordAction() {
- if (this.recordImg === iconRecord) {
+ if (this.recordImg === iconRecord && beforeAudioRecordOrPlay('record')) {
// 設(shè)置格式為MP3,最長(zhǎng)60S,采樣率22050
this.rm.start({
duration: 600000,
format: 'mp3',
sampleRate: 22050,
})
// 開始錄音后綁定停止錄音的回調(diào)方法
this.rm.onStop((e) => this.onStop(e))
} else if (this.recordImg === iconRecording) {
this.rm.stop()
+ afterAudioRecord()
},
},
這樣就避免了多次錄音
小程序錄音上傳
補(bǔ)全我們的 uploadMp3Action
方法,我們使用 uni-app
的 uni.uploadFile()
方法來上傳錄音文件
uploadMp3Action(e) {
const filePath = e.tempFilePath
const option = {
url: 'xxx',
filePath,
header,
formData: {
filePath
},
name: 'audio',
}
uni.showLoading({
title: '錄音上傳中...'
})
return await uni.uploadFile(option)
uni.hideloading()
}
最后在頁(yè)面卸載的時(shí)候回收 RecorderManager
對(duì)象
beforeDestroy() {
this.rm = null
}
相關(guān)案例查看更多
相關(guān)閱讀
- 汽車拆解管理系統(tǒng)
- 昆明小程序定制開發(fā)
- 網(wǎng)絡(luò)公司
- 曲靖小程序開發(fā)
- 云南小程序設(shè)計(jì)
- 百度小程序
- 云南網(wǎng)站建設(shè)
- python開發(fā)小程序
- 報(bào)廢車回收管理系統(tǒng)
- 網(wǎng)站建設(shè)電話
- 云南etc微信小程序
- 開發(fā)微信小程序
- 云南旅游網(wǎng)站建設(shè)
- 網(wǎng)頁(yè)制作
- 云南小程序開發(fā)課程
- 分銷系統(tǒng)
- 網(wǎng)站建設(shè)高手
- 怎么做網(wǎng)站
- asp網(wǎng)站
- 手機(jī)網(wǎng)站建設(shè)
- 小程序制作
- 汽車回收系統(tǒng)
- 云南網(wǎng)站制作
- 云南省建設(shè)廳網(wǎng)站官網(wǎng)
- 汽車報(bào)廢管理
- 定制小程序開發(fā)
- 小程序模板開發(fā)公司
- 買小程序被騙
- 網(wǎng)絡(luò)公司電話
- 云南省建設(shè)廳官方網(wǎng)站