1

我正在尝试将 HMS 自动语音识别 (ASR) 添加到我的应用程序中。我已经SpeechRecognizer实现了,但它需要 GMS 才能工作。

当前的 HMS 实现适用于安装了 HMS 核心的非华为设备,但不适用于我的华为 Mediapad T5。

我尝试过的事情

这些方法是从不同的线程(主线程和图形线程)调用的,所以我尝试将锁上的方法或posting a同步Runnable到活动处理程序,而没有太大的区别。IE,将函数包装在synchronized(lock)or中activity.post

代码:

  • fun init(activity: Activity)
speechRecognizer = MLAsrRecognizer.createAsrRecognizer(activity)
speechIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    .putExtra(
        MLAsrCaptureConstants.LANGUAGE,
        "en-US"
    )
    .putExtra(MLAsrConstants.FEATURE, MLAsrConstants.FEATURE_ALLINONE)
speechRecognizer?.setAsrListener(listener)
  • fun startListening()
speechRecognizer?.startRecognizing(speechIntent)
  • fun destroy()
speechRecognizer?.destroy()

日志

4945-4945 W/HmsSpeechRecognitionHolder@c1cafe: init on Thread[main,5,main]
4945-4945 W/InputMethodManager: startInputReason = 1
4945-4945 W/HmsSpeechRecognitionHolder@c1cafe: startListening on Thread[main,5,main]
4945-4945 E/HaLogProvider: forbiddenHiLog openHa = false
4945-4945 E/HaLogProvider: forbiddenHiLog.getVenderCountry=ca
4945-4945 E/HaLogProvider: forbiddenHiLog openHa = false
4945-4945 E/MLASR_HaAdapter_MLKitAsr: mEventsToBeReported: has no response event isInfoGatherStart:falsemsg: 0
4945-4945 E/HwCustAudioRecordImpl: isOpenEC : false
634-985 E/HuaweiProcessing: ProcessingLib_Create: the algo have already been created.
634-985 W/EffectsFactory: EffectCreate() library huawei_processing: could not create fx Huawei Audio Preprocessing Effect, error -22
634-985 E/EffectFactoryHAL: Error creating effect e707d040-6b79-11e2-b16a-0002a5d5c51b: Invalid argument
721-8060 E/AudioEffect: set(): AudioFlinger could not create effect, status: -19
721-8060 W/AudioPolicyEffects: addInputEffects(): failed to create Fx huawei_pre_processing on source 1
634-8049 E/baidu_asr_interface: asr_baidu_set_parameters_data-not baidu asr
634-7993 W/DeviceHAL: Device 0x78c2d00000 get_mic_mute: Function not implemented
634-987 W/DeviceHAL: Device 0x78c2dc4680 get_mic_mute: Function not implemented
721-8009 W/HuaweiAudioFlinger: soundtrigger is now disable or not support, pls enable it first from setting
721-8009 W/APM_AudioPolicyManager: startInput(78) failed: other input 70 already started
721-8009 E/AudioFlinger: RecordThread::start error,setCallingAppName -1
4945-4945 E/AudioRecord: start() status -38
4945-4945 E/MLASR_A: getVendorCountry=ca
500-8480 W/libc: Set property "hw.wifi.dns_stat" to "99,14,14044,1,34759"
1472-1472 W/HwKeyguardDragHelper: AnimationBlocked
4945-5079 W/libEGL: EGLNativeWindowType 0x79e0317010 disconnect failed
1140-2290 E/WindowManager: win=Window{d80c651 u0 ProjectActivity} destroySurfaces: appStopped=true win.mWindowRemovalAllowed=false win.mRemoveOnExit=false
767-767 E/wificond: Failed to get NL80211_RATE_INFO_NOISE
767-767 E/wificond: Failed to get NL80211_RATE_INFO_SNR
767-767 E/wificond: Failed to get NL80211_STA_INFO_CNAHLOAD
1140-1316 E/WificondControl: Noise: 0, Snr: -1, Chload: -1
767-767 E/wificond: Failed to get NL80211_RATE_INFO_SNR
767-767 E/wificond: Failed to get NL80211_STA_INFO_CNAHLOAD
767-767 E/wificond: Failed to get NL80211_RATE_INFO_NOISE
767-767 E/wificond: Failed to get NL80211_RATE_INFO_SNR
767-767 E/wificond: Failed to get NL80211_STA_INFO_CNAHLOAD
1140-1316 E/WificondControl: Noise: 0, Snr: -1, Chload: -1
761-8466 W/ACodec: forcing OMX state to Idle when received shutdown in ExecutingState
769-8467 W/SimpleSoftOMXComponent: onChangeState mState= 3, mTargetState = 3, state = 2
769-8467 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions mState = 2, mTargetState = 1
721-8060 W/HuaweiAudioFlinger: soundtrigger is now disable or not support, pls enable it first from setting
1900-3437 E/HSM: BMNCaller:is not PermissionEnabled.
721-6695 W/AudioFlinger::EffectModule: EffectModule 0x7ba4f22a00 destructor called with unreleased interface
634-941 E/audio_hw_primary: in_remove_audio_effect error effect is null
634-941 W/StreamHAL: Error from HAL stream in function remove_audio_effect: Function not implemented
721-6695 E/AudioFlinger::EffectModule: Error when removing effect: -38
721-6695 W/AudioFlinger::EffectHandle: disconnect Effect handle 0x7ba4e45800 disconnected after thread destruction
1640-1796 W/AudioState: session release  and not found  sessionId: 81
4945-4945 W/HmsSpeechRecognitionHolder@c1cafe: destroy on Thread[main,5,main]
4945-8481 E/HwCustAudioRecordImpl: isOpenEC : false
4945-8481 E/HwCustAudioRecordImpl: isOpenEC : false

我在日志中发现可疑的事情

634-985 E/HuaweiProcessing: ProcessingLib_Create: the algo have already been created.
634-985 W/EffectsFactory: EffectCreate() library huawei_processing: could not create fx Huawei Audio Preprocessing Effect, error -22
634-985 E/EffectFactoryHAL: Error creating effect e707d040-6b79-11e2-b16a-0002a5d5c51b: Invalid argument
721-8060 E/AudioEffect: set(): AudioFlinger could not create effect, status: -19
721-8060 W/AudioPolicyEffects: addInputEffects(): failed to create Fx huawei_pre_processing on source 1
721-8009 W/APM_AudioPolicyManager: startInput(78) failed: other input 70 already started
721-8009 E/AudioFlinger: RecordThread::start error,setCallingAppName -1
4945-4945 E/AudioRecord: start() status -38

注意:我尝试过的 HMS 演示应用程序可以在我的 Mediapad T5 上正常运行。

更新:在@shirley 指出一些修复后,ASR 似乎在 P30Lite 上可靠地工作。但在较旧的 Mediapad T5 上仍然面临同样的问题。

4

2 回答 2

1

根据您提供的日志,没有检测到用户的声音。日志和状态码的含义如下: 在此处输入图像描述

在此处输入图像描述

解决方案

  1. 建议您在MLAsrListener监听器的回调方法中添加日志,查看语音识别过程。

在此处输入图像描述

  1. 建议您检查mSpeechRecognizer.destroy()。检查此方法是否被过早调用并且在它开始之前已经结束。

  2. 检查设备是否有故障或设备的麦克风是否无效。更换设备并执行测试。


查看您的日志后,发现以下错误: 在此处输入图像描述

这个错误的原因是:语音识别的Languagecode超过10。

你可以在这里查看代码: 在此处输入图像描述

确保语音识别 Languagecode 不超过 10。


11203 ,subError code: 3002,errorMessage: Service unavailable

这个错误的原因是app_id项目中没有找到该信息。

建议检查项目中是否存在agconnect-services.json文件,如下图

在此处输入图像描述

在此处输入图像描述

如果该文件不存在,则需要将其添加到项目中。如果文件存在,请确保它app_id是正确的。

有关详细信息,请参阅以下文档


  1. 检查自动语音识别是否开启失败。

    如果自动语音识别开启失败,可以通过onError(int error, String errorMessage)MLAsrListener类的方法获取原因,如下图所示。

在此处输入图像描述

您可以将上述方法添加到侦听器的类中: 在此处输入图像描述

2.如果语音识别开启成功,但没有得到语音识别结果:

MLAsrConstants.FEATURE参数设置FUNCTION_ALLINONE为。因此,需要在onResults(Bundle results)方法中获取语音识别结果,如下图所示。

在此处输入图像描述

于 2021-03-29T09:32:13.407 回答
1

某些型号的手机和平板电脑在使用 ML ASR 时可能会出现资源限制问题。问题的症状是点击麦克风按钮后手机/平板无响应,或提示设备未安装语音识别服务的错误信息。不仅是 HMS ASR,我还尝试使用原生 Android SpeechRecognizer 来实现语音识别,但在有限的硬件资源手机模拟器上单击按钮后示例应用程序挂起。

要解决您的问题,我建议将使用 HMS ML Kit ASR 切换为使用 HMS ML Kit Real-Time Transcription (RTT)。RTT 提供与 ASR 类似的功能,用于语音识别并将语音转换为文本。更多详情请参见ML Kit-Real-Time Transcription(huawei.com)的HMS ML Kit RTT文档。

RTT 的代码类似于 ASR,需要提供 SpeechRecognitionListener 类或匿名类来实现 MLSpeechRealTimeTranscriptionListener。文档链接上也有示例代码。我在我的华为手机 Mate 30 Pro 上测试了示例代码,它运行良好。

于 2021-03-31T17:49:35.727 回答