public class AsrClient extends Object
For example, you use these methods to initialize the ASR engine service, start speech recognition, stop speech recognition, and recognize PCM streams.
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
cancel() |
Cancels speech recognition.
|
static Optional<AsrClient> |
createAsrClient(Context context) |
Creates an
AsrClient instance. |
void |
destroy() |
Destroys the ASR service by canceling all ongoing ASR tasks.
|
void |
init(AsrIntent intent,
AsrListener asrListener) |
Initializes the ASR engine service.
|
void |
startListening(AsrIntent asrIntent) |
Starts reading and recognizing speech data.
|
void |
stopListening() |
Stops speech recognition.
|
void |
writePcm(byte[] bytes,
int length) |
Writes the PCM audio stream into a byte array and performs speech recognition.
|
public static Optional<AsrClient> createAsrClient(Context context)
AsrClient instance.
The created object is used for invoking the ASR engine capability.
context - Indicates the application context. This parameter cannot be null.AsrClient object.public void init(AsrIntent intent, AsrListener asrListener)
An ASR listener is passed to this method for observing the process and result of an ASR method call, and
initialization parameters are passed for initializing the ASR engine. You must use this method and obtain the
result of AsrListener#onInit(PacMap)} before calling the ASR methods such as
startListening(ohos.ai.asr.AsrIntent) and @link #stopListening()}.
Before re-initializing the ASR service, use destroy() to release the ASR engine.
intent - Indicates ASR initialization parameters, which are defined in AsrIntent. This parameter
cannot be null. Generally, you need to set the audio source type during initialization, otherwise the
default audio source type AsrIntent.AsrAudioSrcType#ASR_SRC_TYPE_FILE is used.asrListener - Indicates the listener to observe the process and result of ASR method calls. This parameter
cannot be null.public void startListening(AsrIntent asrIntent)
If an audio file is detected, this method reads the file and performs speech recognition with the file path
specified using AsrIntent#setFilePath(String); if PCM streams are detected, this method works with
writePcm(byte[],int) to perform speech recognition.
Before using this method, you need to call init(ohos.ai.asr.AsrIntent,ohos.ai.asr.AsrListener) to initialize the ASR engine
service.
asrIntent - Indicates ASR parameters. You can set the pre-VAD threshold, post-VAD threshold, recognition
timeout threshold, and file path (only for audio files). This parameter cannot be null. Unless otherwise
specified, the default parameter values are used. For details about the parameters and their default values,
see AsrIntent.public void stopListening()
After you call this method, the obtained speech data will be recognized, and new data will not. Generally, you do not need to call this method because the ASR engine server automatically stops speech recognition upon detecting that speech recognition is complete. Alternatively, you can use this method to manually stop speech recognition at a specific moment.
Before using this method, you need to call init(ohos.ai.asr.AsrIntent,ohos.ai.asr.AsrListener) to initialize the ASR engine
service.
public void writePcm(byte[] bytes,
int length)
Before using this method, you need to call init(ohos.ai.asr.AsrIntent,ohos.ai.asr.AsrListener) to initialize the ASR engine
service and call startListening(ohos.ai.asr.AsrIntent) to obtain the PCM stream, which will be passed
to the ASR engine. The size of the PCM stream cannot exceed 800 KB. In addition, the duration of the PCM-encoded
audio to play cannot exceed 20s.
bytes - Indicates the PCM stream. This parameter cannot be null. Its length must be greater than 0, and
its size must be less than or equal to 800 KB.length - Indicates the valid length of the PCM audio stream, either 1280 or 640 bytes.public void cancel()
Once speech recognition is canceled, the obtained speech data will no longer be recognized.
Before using this method, you need to call init(ohos.ai.asr.AsrIntent,ohos.ai.asr.AsrListener) to initialize the ASR engine
service.
public void destroy()
The destroyed ASR service can no longer be used. To use the ASR service again, you need to call
createAsrClient(ohos.app.Context) to create
another AsrClient instance.