HMS ASR无法开始录制

HMS ASR cannot start recording

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

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

我尝试过的事情

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

代码:

speechRecognizer = MLAsrRecognizer.createAsrRecognizer(activity)
speechIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    .putExtra(
        MLAsrCaptureConstants.LANGUAGE,
        "en-US"
    )
    .putExtra(MLAsrConstants.FEATURE, MLAsrConstants.FEATURE_ALLINONE)
speechRecognizer?.setAsrListener(listener)
speechRecognizer?.startRecognizing(speechIntent)
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 上仍然面临同样的问题。

根据您提供的日志,没有检测到用户的声音。 日志和状态码的含义如下:

解决方案

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

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

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


查看您的日志后,发现以下错误:

此错误的原因is:The语音识别的语言代码超过 10。

您可以在此处查看代码:

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


11203 ,subError code: 3002,errorMessage: Service unavailable

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

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

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

详情见下文Docs


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

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

你可以将上面的方法添加到监听器的class:

2.If语音识别开启成功但未获取语音识别结果:

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

某些 phone 型号和平板电脑在使用 ML ASR 时可能会出现资源限制问题。问题现象是点击microphone按钮后phone/tablet没有反应,或者提示设备没有安装语音识别服务。不仅是 HMS ASR,我还尝试使用本机 Android SpeechRecognizer 来实现语音识别,但在有限的硬件资源 phone 模拟器上单击按钮后示例应用程序挂起。

为了解决您的问题,我建议将使用 HMS ML Kit ASR 切换为使用 HMS ML Kit 实时转录 (RTT)。 RTT 提供与 ASR 类似的功能,用于语音识别并将语音转换为文本。有关详细信息,请参阅位于 ML Kit-Real-Time Transcription (huawei.com) 的 HMS ML Kit RTT 文档。

RTT的代码类似于ASR,需要提供SpeechRecognitionListenerclass或匿名class来实现MLSpeechRealTimeTranscriptionListener。文档 link 中也有示例代码。我在我的华为 phone Mate 30 Pro 上测试了示例代码,它工作正常。