DEV Community

HarmonyOS
HarmonyOS

Posted on

Error occurred during initialization of the speech recognition engine

Read the original article:Error occurred during initialization of the speech recognition engine

Problem Description

When developing the speech recognition feature, developers often encounter the following errors when creating and initializing the SpeechRecognitionEngine instance. How can this be resolved?

  • Asr CreateEngineParams is wrong.
  • Error message: Cannot read property 'createEngine' of undefined.
  • errCode: 1002200001, errMessage: create param is error, language only support zh-CN now.
  • errCode: 1002200001, errMessage: asr engine init failed with other process has initialized.

Background Knowledge

Speech recognition technology is widely applied, for example, to provide audio-to-text capabilities for hearing-impaired users or situations where it is inconvenient to listen to audio, even when the device is offline. HarmonyOS provides the speechRecognizer capability, which can convert a piece of Chinese audio information (Chinese, English in a Chinese context; short audio mode no longer than 60s, long audio mode no longer than 8 hours) into text. The audio can be in PCM audio file format or real-time speech. For more details, refer to the official website. The corresponding API to create and initialize the engine is speechRecognizer.createEngine.

Troubleshooting Process

  • Asr CreateEngineParams is wrong. There are multiple SpeechRecognitionEngine instances on the testing device.
  • Error message: Cannot read property 'createEngine' of undefined. The error occurs because the current debugging device may not support the feature.

By checking the error message and error codes, you can try to refer to the Core Speech Kit error codes to locate the issue.

  • Error code 1002200001:
    • errMessage: create param is error, language only support zh-CN now. This indicates the creation parameters are wrong, as the language currently only supports "zh-CN".
    • errMessage: asr engine init failed with other process has initialized. The ASR engine initialization failed because another process has already initialized it.

Analysis Conclusion

  • Asr CreateEngineParams is wrong. There are multiple SpeechRecognitionEngine instances on the testing device, possibly because resources were not released promptly after creating the instance, resulting in an error when trying to create it again.
  • Error message: Cannot read property 'createEngine' of undefined. This error occurs when using the emulator for speech recognition debugging. The official documentation specifies that this feature currently does not support the emulator. Refer to the speech recognition constraints and limitations.
  • Error code 1002200001:
    • errMessage: create param is error, language only support zh-CN now. The parameters for speechRecognizer.createEngine CreateEngineParams in the official documentation specify that the language currently only supports "zh-CN" (Chinese).
    • errMessage: asr engine init failed with other process has initialized. This error means that another instance or process is using the same resources or configurations to initialize the ASR engine at the same time or in the same environment, leading to a failure in engine creation.

Solution

  • For the "Asr CreateEngineParams is wrong" error, to ensure there are no multiple instances on the current device, it is important to release resources promptly after use. Below is a suggested code implementation:
  // Define the engine object
  let asrEngine: speechRecognizer.SpeechRecognitionEngine;
  // Define the listener callback object
  let listener: speechRecognizer.RecognitionListener = {
    onStart(sessionId: string, eventMessage: string) {},
    onEvent(sessionId: string, eventCode: number, eventMessage: string) {},
    onResult(sessionId: string, result: speechRecognizer.SpeechRecognitionResult) {},
    onComplete(sessionId: string, eventMessage: string) {
      asrEngine.shutdown();   // Release resources after recognition is complete to ensure the engine can be created again next time
      console.info(`onComplete, sessionId: ${sessionId} eventMessage: ${eventMessage}`);
    },
    onError(sessionId: string, errorCode: number, errorMessage: string) {},
  }

  // Create the speech recognition instance
  speechRecognizer.createEngine(initParamsInfo).then((speechRecognitionEngine: speechRecognizer.SpeechRecognitionEngine) => {
    // Receive the engine instance
    asrEngine = speechRecognitionEngine;
    console.info(`Succeeded in creating engine, result: ${JSON.stringify(asrEngine)}.`);

    // Set the listener
    asrEngine.setListener(listener);

    // Start listening
    asrEngine.startListening({
      sessionId: 'xxx',   // The sessionId must be shorter than 64 characters and cannot contain special characters like '.'
      audioInfo: {
        audioType: 'pcm',
        sampleRate: 16000,
        soundChannel: 1,
        sampleBit: 16,
      }
    });
  }).catch((err: BusinessError) => {
    console.error(`Failed to create engine. Code: ${err.code}, message: ${err.message}.`);
  });
Enter fullscreen mode Exit fullscreen mode
  • The speechRecognizer capability does not support emulator debugging, so switch to physical device debugging.

  • The language parameter in the CreateEngineParams when calling speechRecognizer.createEngine should be changed to "zh-CN".

When implementing speech recognition capabilities, if errors occur while creating and initializing the engine through speechRecognizer.createEngine, check the CreateEngineParams, the current debugging device, and whether there are multiple instances running to resolve the issue.

Written by Bunyamin Akcay

Top comments (0)