ソースを参照

增加录音权限判定

zjs_project 3 週間 前
コミット
5f810460b0
1 ファイル変更44 行追加0 行削除
  1. 44 0
      src/components/newQCCom/viewlayout/viewlayout.vue

+ 44 - 0
src/components/newQCCom/viewlayout/viewlayout.vue

@@ -52,6 +52,7 @@
                 speechText: '', //语音识别的结果-文字
                 recording: false, //语音识别是否开始
                 touching:false,
+                audioAuth:false, //录音权限
 			}
 		},
 		props: {
@@ -118,6 +119,7 @@
 			},
 		},
 		async mounted() {
+            this.getUserMedia();
 			// this.getRecordAuth();//获取录音权限
 			// this.initRecord();
 			// var currPage = getCurrentPages()[getCurrentPages().length - 1] ? getCurrentPages()[getCurrentPages().length - 1].$vm : null;
@@ -273,6 +275,42 @@
 			goRoam(){
 				this.$parent.$refs.viewCareful.goRoam();
 			},
+            getUserMedia(){
+                let self = this;
+                if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
+                    //最新的标准API
+                    navigator.mediaDevices.getUserMedia({
+                        'audio':true,
+                        'video':false//调用前置摄像头,后置摄像头使用video: { facingMode: { exact: "environment" } }
+                    })
+                        .then(success)
+                        .catch(error)
+                } else if (navigator.webkitGetUserMedia) {
+                    //webkit核心浏览器
+                    navigator.webkitGetUserMedia(constraints, success, error)
+                } else if (navigator.mozGetUserMedia) {
+                    //firfox浏览器
+                    navigator.mozGetUserMedia(constraints, success, error);
+                } else if (navigator.getUserMedia) {
+                    //旧版API
+                    navigator.getUserMedia(constraints, success, error);
+                }
+                function success(stream) {
+                    //兼容webkit核心浏览器
+                    let CompatibleURL = window.URL || window.webkitURL;
+                    //将视频流设置为video元素的源
+                    console.log(stream);
+                    self.audioAuth = true;
+                }
+                function error(error) {
+                    console.log(`访问用户媒体设备失败${error.name}, ${error.message}`);
+                    self.audioAuth = false;
+                    Toast({
+                        message: '已拒绝麦克风授权,无法使用录音功能',
+                    });
+                }
+                console.warn("****getUserMedia****")
+            },
             //求最长公共子集
             findSubStr(str1, str2) {
                 if (str1.length > str2.length) {
@@ -397,6 +435,12 @@
             },
             //语音识别
             speechRecognizer() {
+                if(this.audioAuth==false){
+                    Toast({
+                        message: '已拒绝麦克风授权,无法使用录音功能',
+                    });
+                    return false;
+                }
                 this.touching = true;//用户开始触摸语音按钮
                 console.warn('speechRecognizer', this.recording)
                 // if (!webAudioSpeechRecognizer) {