11<template >
2- <view >
3- <page-head :title =" title" ></page-head >
4- <view class =" uni-padding-wrap uni-common-mt" >
5- <view class =" uni-textarea" >
6- <textarea :value =" value" placeholder =" 语音识别内容展示区域" disabled />
7- </view >
2+ <view >
3+ <page-head :title =" title" ></page-head >
4+ <view class =" uni-padding-wrap uni-common-mt" >
5+ <view class =" uni-textarea" >
6+ <textarea :value =" value" placeholder =" 语音识别内容展示区域" disabled />
7+ </view >
88 <view class =" uni-common-mt uni-btn-v" >
99 <button type =" primary" @tap =" startRecognize" >开始语音识别</button >
1010 <button type =" primary" @tap =" startRecognizeEnglish" >开始语音识别(英语)</button >
1313 </view >
1414</template >
1515<script >
16+ import permision from " @/common/permission.js"
1617 export default {
1718 data () {
1819 return {
2425 this .value = " "
2526 },
2627 methods: {
27- startRecognize : function () {
28+ async startRecognize () {
29+ // #ifdef APP-PLUS
30+ let status = await this .checkPermission ();
31+ if (status !== 1 ) {
32+ return ;
33+ }
34+ // #endif
35+
36+ // TODO ios 在没有请求过权限之前无法得知是否有相关权限,这种状态下需要直接调用语音,会弹出正在识别的toast
2837 var options = {};
2938 var that = this ;
3039 options .engine = ' baidu' ;
3645 console .log (" 语音识别失败:" + e .message );
3746 });
3847 },
39- startRecognizeEnglish : function () {
48+ async startRecognizeEnglish () {
49+ // #ifdef APP-PLUS
50+ let status = await this .checkPermission ();
51+ if (status !== 1 ) {
52+ return ;
53+ }
54+ // #endif
55+
56+ // TODO ios 在没有请求过权限之前无法得知是否有相关权限,这种状态下需要直接调用语音,会弹出正在识别的toast
4057 var options = {};
4158 var that = this ;
4259 options .engine = ' baidu' ;
4966 console .log (" 语音识别失败:" + e .message );
5067 });
5168 }
69+ // #ifdef APP-PLUS
70+ ,
71+ async checkPermission () {
72+ let status = permision .isIOS ? await permision .requestIOS (' record' ) :
73+ await permision .requestAndroid (' android.permission.RECORD_AUDIO' );
74+
75+ if (status === null || status === 1 ) {
76+ status = 1 ;
77+ } else if (status === 2 ) {
78+ uni .showModal ({
79+ content: " 系统麦克风已关闭" ,
80+ confirmText: " 设置" ,
81+ success : function (res ) {
82+ if (res .confirm ) {
83+ permision .gotoiOSSetting ();
84+ }
85+ }
86+ })
87+ } else {
88+ uni .showModal ({
89+ content: " 需要麦克风权限" ,
90+ confirmText: " 设置" ,
91+ success : function (res ) {
92+ if (res .confirm ) {
93+ permision .gotoAppSetting ();
94+ }
95+ }
96+ })
97+ }
98+ return status;
99+ }
100+ // #endif
52101 }
53102 }
54103 </script >
55104
56105<style >
57-
58- </style >
106+
107+ </style >
0 commit comments