Blame view

src/platforms/app-plus/speech/speech.vue 3.35 KB
289f85d9e   Adam   提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  <template>
      <view>
          <page-head :title="title"></page-head>
          <view class="uni-padding-wrap uni-common-mt">
              <view class="uni-textarea">
                  <textarea :value="value" placeholder="语音识别内容展示区域" disabled />
                  </view>
  			<view class="uni-common-mt uni-btn-v">
  				<button type="primary" @tap="startRecognize">开始语音识别</button>
  				<button type="primary" @tap="startRecognizeEnglish">开始语音识别(英语)</button>
  			</view>
  		</view>
  	</view>
  </template>
  <script>
      import permision from "@/common/permission.js"
  	export default {
  		data() {
  			return {
  				title: 'speech',
  				value: ''
  			}
  		},
  		onUnload(){
  			this.value = ""
  		},
  		methods: {
  			async startRecognize () {
                  // #ifdef APP-PLUS
                  let status = await this.checkPermission();
                  if (status !== 1) {
                      return;
                  }
                  // #endif
  
                  // TODO ios 在没有请求过权限之前无法得知是否有相关权限,这种状态下需要直接调用语音,会弹出正在识别的toast
  				var options = {};
  				var that = this;
  				options.engine = 'baidu';
  				that.value = "";
  				plus.speech.startRecognize(options, function (s) {
  					console.log(s);
  					that.value += s;
  				}, function (e) {
  					console.log("语音识别失败:" + e.message);
  				});
  			},
  			async startRecognizeEnglish () {
                  // #ifdef APP-PLUS
                  let status = await this.checkPermission();
                  if (status !== 1) {
                      return;
                  }
                  // #endif
  
                  // TODO ios 在没有请求过权限之前无法得知是否有相关权限,这种状态下需要直接调用语音,会弹出正在识别的toast
  				var options = {};
  				var that = this;
  				options.engine = 'baidu';
  				options.lang = 'en-us';
  				that.value = "";
  				plus.speech.startRecognize(options, function (s) {
  					console.log(s);
  					that.value += s;
  				}, function (e) {
  					console.log("语音识别失败:" + e.message);
  				});
  			}
              // #ifdef APP-PLUS
              ,
              async checkPermission() {
                  let status = permision.isIOS ? await permision.requestIOS('record') :
                      await permision.requestAndroid('android.permission.RECORD_AUDIO');
  
                  if (status === null || status === 1) {
                      status = 1;
                  } else if (status === 2) {
                      uni.showModal({
                          content: "系统麦克风已关闭",
                          confirmText: "确定",
                          showCancel: false,
                          success: function(res) {
                          }
                      })
                  } else {
                      uni.showModal({
                          content: "需要麦克风权限",
                          confirmText: "设置",
                          success: function(res) {
                              if (res.confirm) {
                                  permision.gotoAppSetting();
                              }
                          }
                      })
                  }
                  return status;
              }
              // #endif
  		}
  	}
  </script>
  
  <style>
  
  </style>