implementation(name: 'speech_release', ext: 'aar')
implementation 'com.squareup.okhttp3:okhttp:4.2.2'implementation 'com.squareup.okio:okio:1.11.0'implementation 'com.google.code.gson:gson:2.8.5'implementation 'org.slf4j:slf4j-api:1.7.25'
< uses-permission android:name="android.permission.RECORD_AUDIO"/>< uses-permission android:name="android.permission.INTERNET"/>< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
int appid = XXX;int projectid = XXX;String secretId = "XXX";// For user convenience, the SDK provides a local signature. For the security of the secretKey, please generate the signature on a third-party server in a production environment.AbsCredentialProvider credentialProvider = new LocalCredentialProvider("your secretKey");final AAIClient aaiClient;try {// 1. Initialize the AAIClient object.aaiClient = new AAIClient(this, appid, projectid, secretId, credentialProvider);/** You can also use temporary certificate authentication* * 1. Obtain temporary certificates through sts, this step should be implemented on your server side* 2. Call the interface using temporary credentials* **/// aaiClient = new AAIClient(MainActivity.this, appid, projectId, "temporary secretId", "temporary secretKey", "corresponding token", credentialProvider);// 2. Initialize ASR request.final AudioRecognizeRequest audioRecognizeRequest = new AudioRecognizeRequest.Builder().pcmAudioDataSource(new AudioRecordDataSource()) // Set the voice source to microphone input.build();// 3. Initialize ASR result listener.final AudioRecognizeResultListener audioRecognizeResultListener = new AudioRecognizeResultListener() {@Overridepublic void onSliceSuccess(AudioRecognizeRequest audioRecognizeRequest, AudioRecognizeResult audioRecognizeResult, int i) {// Return the recognition result of the voice segment}@Overridepublic void onSegmentSuccess(AudioRecognizeRequest audioRecognizeRequest, AudioRecognizeResult audioRecognizeResult, int i) {// Return the recognition result of the voice stream}@Overridepublic void onSuccess(AudioRecognizeRequest audioRecognizeRequest, String s) {// Return all recognition results}@Overridepublic void onFailure(AudioRecognizeRequest audioRecognizeRequest, ClientException e, ServerException e1) {// Recognition failed}};// 4. Start ASRnew Thread(new Runnable() {@Overridepublic void run() {if (aaiClient!=null) {aaiClient.startAudioRecognize(audioRecognizeRequest, audioRecognizeResultListener);}}}).start();} catch (ClientException e) {e.printStackTrace();}
// 1, Get the request IDfinal int requestId = audioRecognizeRequest.getRequestId();// 2, Call the stop methodnew Thread(new Runnable() {@Overridepublic void run() {if (aaiClient!=null){// Stop ASR, wait for the current task to endaaiClient.stopAudioRecognize(requestId);}}}).start();
// 1, Get the request IDfinal int requestId = audioRecognizeRequest.getRequestId();// 2, Call the cancel methodnew Thread(new Runnable() {@Overridepublic void run() {if (aaiClient!=null){//Cancel ASR and discard the current taskaaiClient.cancelAudioRecognize(requestId);}}}).start();
/*** Signature function: encrypt the original string, the specific encryption algorithm is described below.* @param source Original string* @return Encrypted ciphertext*/String getAudioRecognizeSign(String source);
public AAIClient(Context context, int appid, int projectId, String secreteId, AbsCredentialProvider credentialProvider) throws ClientException
Parameter | Type | Required | Description |
context | Context | Yes | Context |
appid | Int | Yes | AppID registered with Tencent Cloud |
projectId | Int | No | Your projectId |
secreteId | String | Yes | Your SecreteId |
credentialProvider | AbsCredentialProvider | Yes | Authentication class |
try {AaiClient aaiClient = new AAIClient(context, appid, projectId, secretId, credentialProvider);} catch (ClientException e) {e.printStackTrace();}
aaiClient.release();
Method | Description | Default Value | Valid Range |
setMaxAudioRecognizeConcurrentNumber | Maximum number of concurrent speech recognition requests | 2 | 1 - 5 |
setMaxRecognizeSliceConcurrentNumber | Maximum number of concurrent segments for speech recognition | 5 | 1 - 5 |
setAudioRecognizeSliceTimeout | HTTP read timeout period | 5000ms | 500 - 10000ms |
setAudioRecognizeConnectTimeout | HTTP connection timeout period | 5000ms | 500 - 10000ms |
setAudioRecognizeWriteTimeout | HTTP write timeout period | 5000ms | 500 - 10000ms |
ClientConfiguration.setMaxAudioRecognizeConcurrentNumber(2)ClientConfiguration.setMaxRecognizeSliceConcurrentNumber(5)ClientConfiguration.setAudioRecognizeSliceTimeout(2000)ClientConfiguration.setAudioRecognizeConnectTimeout(2000)ClientConfiguration.setAudioRecognizeWriteTimeout(2000)
void onSliceSuccess(AudioRecognizeRequest request, AudioRecognizeResult result, int order);
Parameters | Type | Description |
request | AudioRecognizeRequest | Speech recognition request |
result | AudioRecognizeResult | Speech recognition result of the audio segment |
order | Int | Sequence of the audio stream of the audio segment |
void onSegmentSuccess(AudioRecognizeRequest request, AudioRecognizeResult result, int order);
Parameters | Type | Description |
request | AudioRecognizeRequest | Speech recognition request |
result | AudioRecognizeResult | Speech recognition result of the audio segment |
order | Int | Sequence of the audio stream |
void onSuccess(AudioRecognizeRequest request, String result);
Parameters | Type | Description |
request | AudioRecognizeRequest | Speech recognition request |
result | String | All recognition results |
void onFailure(AudioRecognizeRequest request, final ClientException clientException, final ServerException serverException,String response);
Parameters | Type | Description |
request | AudioRecognizeRequest | Speech recognition request |
clientException | ClientException | Client exception |
serverException | ServerException | Server exception |
response | String | JSON string returned by the server |
Parameter | Type | Required | Description | Default Value |
setSilentDetectTimeOut | Boolean | No | Specifies whether to enable silence detection. After it is enabled, the silence part before the actual speech starts will not be recognized | true |
audioFlowSilenceTimeOut | Int | No | Specifies whether to enable speech start detection timeout. After it is enabled, recording will automatically stop after the timeout period elapses | 5000ms |
minAudioFlowSilenceTime | Int | No | Minimum period for segmenting two audio streams | 2000ms |
minVolumeCallbackTime | Int | No | Volume callback time | 80ms |
AudioRecognizeConfiguration audioRecognizeConfiguration = new AudioRecognizeConfiguration.Builder().setSilentDetectTimeOut(true)// Enable silence detection. false means the silent part will not be checked.audioFlowSilenceTimeOut(5000) // Silent detection timeout to stop recording.minAudioFlowSilenceTime(2000) // Interval time during voice stream recognition.minVolumeCallbackTime(80) // Volume callback time.build();// Start ASRnew Thread(new Runnable() {@Overridepublic void run() {if (aaiClient!=null) {aaiClient.startAudioRecognize(audioRecognizeRequest, audioRecognizeResultListener, audioRecognizeConfiguration);}}}).start();
Method | Description |
onStartRecord | Start of recording |
onStopRecord | Stop of recording |
onVoiceFlowStart | Start of audio stream |
onVoiceFlowStartRecognize | Start of audio stream recognition |
onVoiceFlowFinishRecognize | End of audio stream recognition |
onVoiceVolume | Volume |
onNextAudioData | Return of the audio stream to the host layer for recording caching. It will take effect when true is passed in for new AudioRecordDataSource(true) |
Method | Description |
onFirstVoiceFlowTimeout | Detects the timeout of the first audio stream |
onNextVoiceFlowTimeout | Detects the timeout of the next audio stream |
AudioRecognizeStateListener audioRecognizeStateListener = new AudioRecognizeStateListener() {@Overridepublic void onStartRecord(AudioRecognizeRequest audioRecognizeRequest) {// Start recording}@Overridepublic void onStopRecord(AudioRecognizeRequest audioRecognizeRequest) {// End recording}@Overridepublic void onVoiceFlowStart(AudioRecognizeRequest audioRecognizeRequest, int i) {// Voice stream starts}@Overridepublic void onVoiceFlowFinish(AudioRecognizeRequest audioRecognizeRequest, int i) {// Voice stream ends}@Overridepublic void onVoiceFlowStartRecognize(AudioRecognizeRequest audioRecognizeRequest, int i) {// Voice stream starts recognition}@Overridepublic void onVoiceFlowFinishRecognize(AudioRecognizeRequest audioRecognizeRequest, int i) {// Voice stream ends recognition}@Overridepublic void onVoiceVolume(AudioRecognizeRequest audioRecognizeRequest, int i) {// Volume callback}};/*** Return Audio Stream,* Used to return to the host layer for the recording cache service.* Since the method runs on the SDK thread, it is mostly used for file operations. The host needs to create a new thread specifically for implementing business logic* new AudioRecordDataSource(true) is valid, otherwise, this function will not be called back* @param audioDatas*/@Overridepublic void onNextAudioData(final short[] audioDatas, final int readBufferLength){}
Parameter | Type | Required | Description | Default Value |
pcmAudioDataSource | PcmAudioDataSource | Yes | Audio Data Source | No |
templateName | String | No | Template name set in the console | No |
customTemplate | AudioRecognizeTemplate | No | Custom template | ("16k_zh", 1) |
Parameter | Type | Description |
code | Int | Recognition status code |
message | String | Recognition prompt message |
text | String | Recognition result |
seq | Int | Sequence number of the audio segment |
voiceId | String | ID of the audio stream of the audio segment |
cookie | String | Cookie value |
Parameter | Type | Required | Description |
engineModelType | String | Yes | Engine model type |
resType | Int | Yes | Result return method |
AudioRecognizeTemplate audioRecognizeTemplate = new AudioRecognizeTemplate("16k_zh",1);
int read(short[] audioPcmData, int length);
void start() throws AudioRecognizerException;
void stop();
void savePcmFileCallBack(String filePath);
void saveWaveFileCallBack(String filePath);
int maxLengthOnceRead();
public static void disableDebug();public static void disableInfo();public static void disableWarn();public static void disableError();public static void enableDebug();public static void enableInfo();public static void enableWarn();public static void enableError();
isSaveAudioRecordFiles
to true during the initialization of new AudioRecordDataSource(isSaveAudioRecordFiles)
.AudioRecognizeStateListener
.onStopRecord callback function and optionally save PCM files as WAV files.AudioRecognizeStateListener
.onNextAudioData callback function.MainActivity
class in the demo project.
Was this page helpful?