【问题标题】:webkitSpeechRecognition not Working in WKWebViewwebkitSpeechRecognition 在 WKWebView 中不起作用
【发布时间】:2023-02-17 05:31:44
【问题描述】:

我试图让语音识别在 WKWebView 中工作,但我收到“语音识别服务权限检查失败”错误。

它会在模拟器上提示权限,但不会在实际设备上提示。

我认为它在 iOS 14.3 及更高版本上受支持。该应用程序确实允许麦克风权限。

这是我要使用的

var speechRecognition = new webkitSpeechRecognition(),
                response = {onChange: function() {}},
                allowEnd;
            speechRecognition.interimResults = true; 

            response.stop = function() {
                allowEnd = true;
                speechRecognition.stop();
            }
            response.cancel = function() {
                allowEnd = true;
                speechRecognition.abort();
            }

            speechRecognition.addEventListener('speechstart', function() {
                scope.$apply(function() {
                    response.speaking = true;
                });
            });

            speechRecognition.addEventListener('error', function(event) {
                _this.browserSupport.errorMessage = 'Something went wrong.  Try again later.';

                allowEnd = true;

                if (event) {
                    switch(event.error) {
                        case 'language-not-supported':
                            scope.$apply(function () {
                                _this.browserSupport.isSupported = false;
                            });
                            response.cancel();
                            break;
                        case 'not-allowed':
                            response.permissionDenied = true;
                            break;
                        case 'aborted':
                            break;
                        default:
                            console.error('Speech Recognition Error', event);
                    }
                }
            });


            speechRecognition.addEventListener('end', function(event) {
                if (!allowEnd && !scope.$$destroyed) {
                    speechRecognition.start();
                } else {
                    scope.$apply(function() {
                        response.listening = false;

                        if (response.onStop) {
                            response.onStop()
                        }
                    });
                }
            });

            speechRecognition.addEventListener('start', function() {
                scope.$apply(function() {
                    response.listening = true;
                });
            });

            speechRecognition.start();

【问题讨论】:

  • 我知道这个问题是针对 JavaScript 语言的,但是有一个类似的(尚未回答的)问题是针对 Swift here 提出的。由于主题在我看来是 WKWebView,我认为它可能是一个有用的交叉引用
  • 您是否成功解决了这个@plato522?
  • 我确实取得了一些成功。我确实发现我必须进行另一个权限设置。我还必须设置 NSSpeechRecognitionUsageDescription。然后我会提示要求用户能够进行语音识别。我本来以为那个麦克风就足够了。

标签: ios wkwebview mobile-webkit


【解决方案1】:

@plato522 感谢,在 info.plist 文件中添加权限声明来解决问题:

通过属性/密钥

键:NSMicrophoneUsageDescription

键:NSCameraUsageDescription

键:NSSpeechRecognitionUsageDescription

或按来源

<key>NSMicrophoneUsageDescription</key>
<string>your string</string>
<key>NSCameraUsageDescription</key>
<string>your string</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>your string</string>

【讨论】:

    猜你喜欢
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多