uniapp 安卓/ios 录音授权,录制音频,录音文件上传
生活随笔
收集整理的這篇文章主要介紹了
uniapp 安卓/ios 录音授权,录制音频,录音文件上传
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下載依賴插件
官方的app端要權插件:App權限判斷和提示
第三方錄音組件:錄音播放-語音錄制voice-sound-recording
引入依賴
import permision from "@/js_sdk/wa-permission/permission.js" import soundRecording from '@/components/sound-recording/sound-recording.vue'組件聲明
components: {soundRecording,},組件使用
//組件 show為控制是否顯示。<u-button @click="record" type="primary">錄制音頻'</u-button><view class="popup-bottom" v-if="show"><view class="popup-bg" @click="show = false"></view><view class="popup-content"><sound-recording:maximum="60"@cancel="show = false"@confirm="onUpload"></sound-recording></view></view>// 組件樣式.popup-bottom {position: fixed;bottom: 0;left: 0;width: 100%;height: 100%;z-index: 10;display: flex;flex-direction: column;.popup-bg {position: fixed;left: 0;top: 0;width: 100%;height: 100%;background: rgba(0, 0, 0, 0.5);}.popup-content {height: 40%;margin-top: auto;background-color: #fff;position: relative;z-index: 11;}}錄音權限
async record() {let env = uni.getSystemInfoSync().platformif (env === 'android') {permision.requestAndroidPermission('android.permission.RECORD_AUDIO').then((e)=>{if(e===-1){uni.showToast({title:'您已經永久拒絕錄音權限,請在應用設置中手動打開',icon:'none',})}else if(e===0){uni.showToast({title:'您拒絕了錄音授權',icon:'none',})}else if(e===1){this.show=true}else {uni.showToast({title:'授權返回值錯誤',icon:'none',})}}).catch((err)=>{uni.showToast({title:'拉起錄音授權失敗',icon:'none',})})} else if (env === 'ios') {if(permision.judgeIosPermission("record"))this.show=trueelseuni.showToast({title:'您拒絕了錄音授權,請在應用設置中手動打開',icon:'none',})}},錄音文件上傳
//音頻上傳onUpload(tempFilePath) {uni.showLoading();uni.uploadFile({url: host + apiUrl.UploadController.upload,filePath: tempFilePath, //錄音結束后返回的臨時路徑name: 'file', // 文件對應的 key值對象名稱header: {'content-type': 'multipart/form-data','Authorization': uni.getStorageSync('Authorization') //token信息},success: (res) => {const result=JSON.parse(res.data)console.log('result',result)},fail: (res) => {uni.showToast({title:'失敗',icon:'none',})}})},總結
以上是生活随笔為你收集整理的uniapp 安卓/ios 录音授权,录制音频,录音文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。