這篇文章主要介紹uniapp如何實現錄音上傳功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
uni-app 是一個使用 Vue.js 開發跨平臺應用的前端框架。
開發者通過編寫 Vue.js 代碼,uni-app 將其編譯到iOS、Android、微信小程序等多個平臺,保證其正確運行并達到優秀體驗。
我是寫了個錄音的圖片
點擊之后彈出一個彈出層(仿了下qq的樣式)

樣式怎么寫我就不贅述了大家都會

這是重點敲黑板!!!

為了全局都好獲取到,可以隨時開始錄音,隨時停止錄音,我把他扔進全局了
const recorderManager = uni.getRecorderManager();//創建一個錄音實例
const innerAudioContext = uni.createInnerAudioContext();//用來播放的實例
// 為了防止蘋果手機靜音無法播放
uni.setInnerAudioOption({
obeyMuteSwitch: false
})
innerAudioContext.autoplay = true;
export default {this.timecount = '00:00:00';//初始化錄音時間
this.hour = 0;
this.minute = 0;
this.second = 0;
this.getTimeIntervalplus();//封裝的一個計時器,調用開始計時
const options = {//參數
duration: 600000,
sampleRate: 44100,
numberOfChannels: 1,
encodeBitRate: 192000,
format: 'mp3',
frameSize: 50
}
recorderManager.start(options);
需要限制最短時長的可以做下判斷,我這邊沒寫
recorderManager.stop();//結束錄音 clearInterval(this.timer);//結束計時
innerAudioContext.src = this.voicePath;//播放的地址(上面錄的本地地址) innerAudioContext.play();//播放
innerAudioContext.pause();//暫停播放 clearInterval(this.timer);//清除定時器
//結束錄音提交錄音
submitrecord: function() {
this.count += 1;//這是定義了一個全局的變量來防止多次提交
if (this.count == 1){
console.log(this.count);
var https = getApp().globalData.https;
if (this.recordednum == 2) {
this.recordednum = 3;
recorderManager.stop();
clearInterval(this.timer);
}
if (this.voicePath != '') {
console.log(this.voicePath);
uni.uploadFile({
url: https + 'Uploads/uploadVoiceVideo',
filePath: this.voicePath,
name: 'file',
success: (res) => {
this.count = 0;
//初始化
this.initialize()//我封裝了一個初始化定時器的函數
this.timer = this.timecount;
this.show_recording = false;
var data = JSON.parse(res.data);
if (this.current == 0) {//判斷是哪個列里面錄的音插回去
this.firsvideo.push(data.address);
} else if (this.current == 1) {
this.secovideo.push(data.address);
console.log(this.secovideo);
} else if (this.current == 2) {
this.thrivideo.push(data.address);
}
uni.showToast({
title: '提交成功!',
icon: 'none',
duration: 1200
})
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
tilte: '上傳失敗~',
icon: 'none',
duration: 1200
})
return
}
});
} else {
console.log("錄音失敗")
uni.showToast({
tilte: '錄音失敗',
icon: 'none',
duration: 1200
})
uni.hideLoading();
this.show_recording = false;
this.checkPermission()
this.rerecord()
}
} else {
uni.showToast({
title: '請勿重復提交',
icon: 'none',
duration: 2000
})
this.count=0;
}
},//重新錄制
rerecord: function() {
//初始化定時器
this.initialize()
this.getTimeIntervalplus();//開始計時
const options = {
duration: 600000,
sampleRate: 44100,
numberOfChannels: 1,
encodeBitRate: 192000,
format: 'mp3',
frameSize: 50
}
recorderManager.start(options);//開始錄音
},onLoad(option) {
var appointment_message = option.appointment_message;
appointment_message = JSON.parse(appointment_message);
this.appointment_message = appointment_message;
let that = this;
recorderManager.onStop(function(res) {
console.log('recorder stop' + JSON.stringify(res));
that.voiceDuration = res.duration;
that.voicePath = res.tempFilePath;
console.log(res);
});
},// 計時器增
getTimeIntervalplus() {
clearInterval(this.timer);
this.timer = setInterval(() => {
this.second += 1;
if (this.second >= 60) {
this.minute += 1;
this.second = 0;
}
if (this.minute >= 10) {
this.recordednum = 3;
recorderManager.stop();
clearInterval(this.timer);
}
this.second2 = this.second;
this.minute2 = this.minute;
this.timecount = this.showNum(this.hour) + ":" + this.showNum(this.minute) + ":" + this
.showNum(this.second);
- console.log("this.timecount", this.timecount)
}, 1000);
},data() {
return {
action: 'https://yl.letter-song.top/api/Uploads/uploadPicture', //上傳圖片地址
textareavalue: '', //文字描述值
fileList2: [], //返回圖片路徑2
fileList3: [], //返回圖片路徑3
fileList: [], //返回圖片路徑1
firsvideo: [], //錄音路徑1
secovideo: [], //錄音路徑2
thrivideo: [], //錄音路徑3
appointment_message: '', //預約信息上個頁面傳參過來的
list: [{ //標簽表
name: '過往癥狀'
}, {
name: '近期癥狀'
}, {
name: '本次癥狀',
}],
current: 0, //選中項
sty: { //滑塊樣式
height: '4px',
borderRadius: '2rpx',
borderTopLeftRadius: '10px',
backgroundColor: '#3ECEB5'
},
activeItem: { //選中項樣式
color: '#333333',
fontWeight: '600',
fontSize: '36rpx',
},
show_recording: false, //調起錄音彈窗
recordednum: 1, //1:初始狀態2.錄音狀態3結束
voicePath: '', //本次音頻錄音路徑
voiceDuration: '',
timecount: '00:00:00',
timecount2: "",
hour: 0,
minute: 0,
second: 0,
hour2: 0,
minute2: 0,
second2: 0,
isZant: false,
timer: '',
yuming: '這是域名',
permiss: 0, //0為開啟錄音權限1已開啟錄音權限,
count: 0
}
},以上是“uniapp如何實現錄音上傳功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。