金坛河长通 v1.1.6版本的 MD5 值为:20e883288031684b3415ea19cd21af4d
以下内容为反编译后的 NlsClient.java 源代码,内容仅作参考
package com.alibaba.idst.nls;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.alibaba.idst.nls.NlsListener;
import com.alibaba.idst.nls.internal.protocol.AsrResponse;
import com.alibaba.idst.nls.internal.protocol.NlsRequest;
import com.alibaba.idst.nls.internal.utils.DefaultRecorder;
import com.alibaba.idst.nls.internal.utils.JoyPrint;
import com.alibaba.idst.nls.internal.utils.L;
import com.alibaba.idst.nls.internal.utils.RecorderCallback;
import com.alibaba.idst.nls.nlsclientsdk.requests.NlsClientInternal;
import com.alibaba.idst.nls.nlsclientsdk.requests.asr.SpeechRecognizer;
import com.alibaba.idst.nls.nlsclientsdk.requests.asr.SpeechRecognizerListener;
import com.alibaba.idst.nls.nlsclientsdk.requests.asr.SpeechRecognizerResponse;
import com.alibaba.idst.nls.nlsclientsdk.requests.tts.SpeechSynthesizer;
import com.alibaba.idst.nls.nlsclientsdk.requests.tts.SpeechSynthesizerListener;
import com.alibaba.idst.nls.nlsclientsdk.requests.tts.SpeechSynthesizerResponse;
import com.amap.api.col.n3.a;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
public class NlsClient implements RecorderCallback {
private static final String TAG = "NlsClient";
private static AsrResponse asrOut;
private static NlsListener.RecognizedResult recognizedResult;
private NlsListener mRecognizeListener;
private StageListener mStageListener;
private DefaultRecorder mVoiceRecorder;
private NlsRequest nlsRequest;
private Handler ttsHandler;
private static String mAddress = "wss://nls-gateway-inner.aliyuncs.com:443/ws/v1";
private static String mAppKey = "";
private static String accessToken = "";
private NlsClientInternal nlsClientInternal = null;
private SpeechSynthesizer synthesizer = null;
private SpeechRecognizer recognizer = null;
private boolean isStarted = false;
private boolean onRequesting = false;
private int webSocketTimeout = 0;
public static class ErrorCode {
public static final int CONNECT_ERROR = 530;
public static final int ERROR_AUTH_FAILD = 403;
public static final int ERROR_CLICK_TOOMUCH = 570;
public static final int ERROR_FORMAT = 400;
public static final int ERROR_NEED_DATA_PLUS_AUTH = 401;
public static final int ERROR_OVER_CONNECTION_LIMITED = 429;
public static final int ERROR_REQUEST_TIMEOUT = 408;
public static final int NOTHING = 4;
public static final int NO_RECORDING_PERMISSION = 504;
public static final int RECOGNIZE_ERROR = 1;
public static final int RECORDING_ERROR = 2;
public static final int SERVER_HANDLING_ERROR = 500;
public static final int SERVICE_NOT_AVAILABLE = 503;
public static final int SUCCESS = 0;
public static final int TTS_BEGIN = 6;
public static final int TTS_OVER = 8;
public static final int TTS_TRANSFERRING = 7;
public static final int USER_CANCEL = 3;
}
public static void configure(Context context, String str, String str2) {
mAppKey = str2;
mAddress = str;
}
public static void configure(Context context) {
}
public static NlsClient newInstance(Context context, NlsListener nlsListener, StageListener stageListener, NlsRequest nlsRequest) {
return new NlsClient(context, nlsListener, stageListener, nlsRequest);
}
private NlsClient(Context context, NlsListener nlsListener, StageListener stageListener, NlsRequest nlsRequest) {
this.mStageListener = null;
this.mRecognizeListener = null;
this.nlsRequest = null;
this.mVoiceRecorder = null;
this.mStageListener = stageListener;
this.mRecognizeListener = nlsListener;
this.nlsRequest = nlsRequest;
this.mVoiceRecorder = new DefaultRecorder(this);
initTTSHandler();
}
public NlsClient setHost(String str) {
if (str.contains("443")) {
mAddress = "wss://" + str;
} else {
mAddress = "ws://" + str;
}
return this;
}
public NlsClient setAccessToken(String str) {
accessToken = str;
return this;
}
public void setWebSocketClientTimeout(int i) {
this.webSocketTimeout = i;
}
public static void openLog(boolean z) {
if (z) {
JoyPrint.openPrint();
} else {
JoyPrint.closePrint();
}
}
public static void setAutoLoadLibs(boolean z) {
}
public static boolean isSerivceAvailable() {
return true;
}
public NlsClient setMinVoiceValueInterval(int i) {
return this;
}
public NlsClient setNoneEffectiveRecordTime(int i) {
return this;
}
public NlsClient setMinRecordTime(int i) {
return this;
}
public NlsClient setMaxRecordTime(int i) {
return this;
}
public NlsClient setMaxStallTime(int i) {
return this;
}
public NlsClient setMinimalSpeechLength(int i) {
return this;
}
public NlsClient setMinMuteValue(int i) {
return this;
}
public NlsClient setRecordAutoStop(boolean z) {
return this;
}
public NlsClient setConnectorEnabled(boolean z) {
return this;
}
public boolean isConnectorEnabled() {
return true;
}
public void checkService() {
}
public void destory() {
L.i(TAG, "Destory!!!");
this.ttsHandler.removeCallbacksAndMessages(null);
this.ttsHandler.getLooper().quitSafely();
if (this.nlsClientInternal != null) {
this.nlsClientInternal.shutdown();
}
}
public boolean start() {
if (this.isStarted && !this.onRequesting) {
this.mRecognizeListener.onRecognizingResult(ErrorCode.ERROR_CLICK_TOOMUCH, null);
return false;
}
this.nlsClientInternal = new NlsClientInternal(mAddress, accessToken);
this.recognizer = this.nlsClientInternal.buildSpeechRecognizer(getRecognizerListener());
if (!this.mVoiceRecorder.start()) {
L.e(TAG, "voice recorder start failed!");
stop();
return false;
}
recognizedResult = new NlsListener.RecognizedResult();
asrOut = new AsrResponse();
this.isStarted = true;
return true;
}
public void stop() {
if (this.synthesizer != null) {
L.i(TAG, "stop tts!");
this.synthesizer.close();
this.onRequesting = false;
} else {
if (!this.isStarted && this.onRequesting) {
L.e(TAG, "Too many stop request, request already on stopping");
return;
}
this.isStarted = false;
this.mVoiceRecorder.stop();
if (this.recognizer != null) {
try {
if (this.onRequesting) {
this.recognizer.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
this.recognizer.close();
}
}
}
public boolean isStarted() {
return this.isStarted;
}
private void close() {
}
public void cancel() {
this.isStarted = false;
this.mVoiceRecorder.stop();
if (this.recognizer != null) {
try {
this.recognizer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (this.synthesizer != null) {
L.i(TAG, "stop tts!");
this.synthesizer.close();
this.onRequesting = false;
}
}
public boolean PostTtsRequest(String str, String str2) {
if (this.onRequesting) {
L.e("Still on tts requesting");
return false;
}
this.onRequesting = true;
Message message = new Message();
message.arg1 = Integer.parseInt(str2);
message.obj = str;
if (this.ttsHandler != null && this.ttsHandler.sendMessage(message)) {
return true;
}
this.onRequesting = false;
return false;
}
void initTTSHandler() {
new Thread(new Runnable() {
@Override
@SuppressLint({"HandlerLeak"})
public void run() {
Looper.prepare();
NlsClient.this.ttsHandler = new Handler() {
@Override
public void handleMessage(Message message) {
try {
NlsClient.this.onRequesting = true;
NlsClient.this.nlsClientInternal = new NlsClientInternal(NlsClient.mAddress, NlsClient.accessToken);
if (NlsClient.this.webSocketTimeout > 0) {
NlsClient.this.nlsClientInternal.setJwebSocketClientTimeout(NlsClient.this.webSocketTimeout);
}
for (int i = 0; i < 3; i++) {
NlsClient.this.synthesizer = NlsClient.this.nlsClientInternal.buildSpeechSynthesizer(NlsClient.this.initSynthesizerListener());
if (NlsClient.this.synthesizer != null) {
break;
}
L.e(NlsClient.TAG, "build synthesizer request failed, start retry connect for " + (i + 1) + " times!");
}
if (NlsClient.this.synthesizer != null) {
if (NlsClient.this.nlsRequest != null) {
if (NlsClient.this.nlsRequest.requests.tts_in.getEncode_type() != null && !NlsClient.this.nlsRequest.requests.tts_in.getEncode_type().equals("")) {
NlsClient.this.synthesizer.setFormat(NlsClient.this.nlsRequest.requests.tts_in.getEncode_type());
}
NlsClient.this.synthesizer.setSampleRate(message.arg1);
NlsClient.this.synthesizer.setPitchRate(NlsClient.this.nlsRequest.requests.tts_in.getPitch_rate());
NlsClient.this.synthesizer.setSpeechRate(NlsClient.this.nlsRequest.requests.tts_in.getPitch_rate());
NlsClient.this.synthesizer.setText((String) message.obj);
NlsClient.this.synthesizer.setVoice(NlsClient.this.nlsRequest.requests.tts_in.getVoice());
NlsClient.this.synthesizer.setVoiceVolume(NlsClient.this.nlsRequest.requests.tts_in.getVolume());
NlsClient.this.synthesizer.setAppKey(NlsClient.this.nlsRequest.getApp_key());
NlsClient.this.synthesizer.start();
return;
}
return;
}
L.e(NlsClient.TAG, "synthesizer request build failed for 3 times!");
NlsClient.this.onRequesting = false;
if (NlsClient.this.mRecognizeListener != null) {
NlsClient.this.mRecognizeListener.onTtsResult(ErrorCode.CONNECT_ERROR, null);
}
} catch (Exception e) {
NlsClient.this.onRequesting = false;
e.printStackTrace();
}
}
};
Looper.loop();
}
}).start();
}
public boolean PostTtsRequest(String str) {
return PostTtsRequest(str, "16000");
}
public SpeechSynthesizerListener initSynthesizerListener() {
return new SpeechSynthesizerListener() {
boolean first = false;
@Override
public void onComplete(SpeechSynthesizerResponse speechSynthesizerResponse) {
NlsClient.this.synthesizer.close();
NlsClient.this.onRequesting = false;
NlsClient.this.synthesizer = null;
NlsClient.this.mRecognizeListener.onTtsResult(8, null);
if (NlsClient.this.mStageListener != null) {
NlsClient.this.mStageListener.onStopRecognizing(NlsClient.this);
}
}
@Override
public void onOpen() {
this.first = true;
if (NlsClient.this.mStageListener != null) {
NlsClient.this.mStageListener.onStartRecognizing(NlsClient.this);
}
}
@Override
public void onClose(int i, String str) {
String str2 = "connection is closed due to {" + str + "},code:{" + i + "}";
NlsClient.this.onRequesting = false;
}
@Override
public void onFail(int i, String str) {
NlsClient.this.onRequesting = false;
String str2 = "fail status:{},reason:{}" + i + str;
NlsClient.this.mRecognizeListener.onTtsResult(500, null, str);
}
@Override
public void onMessage(ByteBuffer byteBuffer) {
byte[] bArr = new byte[byteBuffer.remaining()];
byteBuffer.get(bArr, 0, bArr.length);
L.i(NlsClient.TAG, "Received Byte Data length : " + bArr.length);
if (!this.first) {
if (NlsClient.this.mRecognizeListener != null) {
NlsClient.this.mRecognizeListener.onTtsResult(7, bArr);
}
} else {
this.first = false;
if (NlsClient.this.mRecognizeListener != null) {
NlsClient.this.mRecognizeListener.onTtsResult(6, bArr);
}
}
}
@Override
public void onError(Exception exc) {
if (!(exc instanceof UnknownHostException)) {
String str = "error occurred :" + exc;
exc.printStackTrace();
NlsClient.this.onRequesting = false;
NlsClient.this.nlsClientInternal.shutdown();
if (NlsClient.this.mRecognizeListener != null) {
NlsClient.this.mRecognizeListener.onTtsResult(ErrorCode.CONNECT_ERROR, null);
}
}
}
};
}
private SpeechRecognizerListener getRecognizerListener() {
return new SpeechRecognizerListener() {
@Override
public void onFail(int i, String str) {
NlsClient.this.isStarted = false;
NlsClient.this.onRequesting = false;
String str2 = "Server fail with status:{" + i + "},reasone:{" + str + "}";
NlsClient.this.mRecognizeListener.onRecognizingResult(500, null);
}
@Override
public void onOpen() {
NlsClient.this.onRequesting = true;
NlsClient.this.mStageListener.onStartRecognizing(NlsClient.this);
}
@Override
public void onStopRecognizing(int i, String str) {
NlsClient.this.isStarted = false;
if (i == 403 || i == 302) {
NlsClient.this.stop();
}
NlsClient.this.onRequesting = false;
NlsClient.this.mStageListener.onStopRecognizing(NlsClient.this);
}
@Override
public void onError(Exception exc) {
NlsClient.this.isStarted = false;
if (NlsClient.this.recognizer != null) {
NlsClient.this.recognizer.markFail();
L.e(NlsClient.TAG, "Network error, call recognizer.markFail()");
}
L.e(NlsClient.TAG, "Client error occurred with" + exc.getMessage());
NlsClient.this.mRecognizeListener.onRecognizingResult(ErrorCode.CONNECT_ERROR, null);
NlsClient.this.stop();
if (!NlsClient.this.onRequesting) {
NlsClient.this.mStageListener.onStopRecognizing(NlsClient.this);
}
NlsClient.this.onRequesting = false;
}
@Override
public void onSpeechRecResult(SpeechRecognizerResponse speechRecognizerResponse) {
L.d(NlsClient.TAG, "******asr result:" + speechRecognizerResponse.getRecognizedText() + " task id is:" + speechRecognizerResponse.getTaskId());
NlsClient.asrOut.setFinish(0);
NlsClient.asrOut.setUid(speechRecognizerResponse.getTaskId());
NlsClient.asrOut.setResult(speechRecognizerResponse.getRecognizedText());
String a = a.a(NlsClient.asrOut);
NlsClient.recognizedResult.asr_out = a;
NlsClient.recognizedResult.out = a;
NlsClient.this.mRecognizeListener.onRecognizingResult(0, NlsClient.recognizedResult);
}
@Override
public void onSpeechRecComplete(SpeechRecognizerResponse speechRecognizerResponse) {
if (NlsClient.this.recognizer.getEnableVoiceDetection()) {
L.i(NlsClient.TAG, "Auto stop when use Cloud VAD");
NlsClient.this.stop();
}
L.d(NlsClient.TAG, "******asr complete result:" + speechRecognizerResponse.getRecognizedText() + " task id is:" + speechRecognizerResponse.getTaskId());
NlsClient.asrOut.setFinish(1);
NlsClient.asrOut.setUid(speechRecognizerResponse.getTaskId());
NlsClient.asrOut.setResult(speechRecognizerResponse.getRecognizedText());
String a = a.a(NlsClient.asrOut);
NlsClient.recognizedResult.finish = true;
NlsClient.recognizedResult.asr_out = a;
NlsClient.recognizedResult.out = a;
NlsClient.this.mRecognizeListener.onRecognizingResult(0, NlsClient.recognizedResult);
}
};
}
@Override
public void onStop() {
this.mStageListener.onStopRecording(this);
}
@Override
public void onVoiceData(byte[] bArr, int i) {
this.recognizer.send(bArr);
}
@Override
public void onVoiceVolume(int i) {
this.mStageListener.onVoiceVolume(i);
}
@Override
public void onPre() {
if (this.nlsRequest != null && this.recognizer != null) {
this.recognizer.setAppKey(this.nlsRequest.getApp_key());
this.recognizer.setFormat(this.nlsRequest.getAsrFormat());
this.recognizer.setCustomizationId(this.nlsRequest.requests.asr_in.customization_id);
if (this.nlsRequest.requests.asr_in.response_mode.equals("0")) {
this.recognizer.enableIntermediateResult(true);
} else if (this.nlsRequest.requests.asr_in.response_mode.equals("1")) {
this.recognizer.enableIntermediateResult(false);
}
if (this.nlsRequest.requests.asr_in.max_end_silence > 0) {
this.recognizer.setEnableVoiceDetection(true);
this.recognizer.setMaxStartSilence(this.nlsRequest.requests.asr_in.max_start_silence);
this.recognizer.setMaxEndSilence(this.nlsRequest.requests.asr_in.max_end_silence);
}
if (this.nlsRequest.requests.asr_in.model != null) {
this.recognizer.payload.put("model", this.nlsRequest.requests.asr_in.model);
}
this.recognizer.setVocabularyId(this.nlsRequest.requests.asr_in.vocabulary_id);
} else {
L.e(TAG, "start error because recognizer is null");
}
this.mStageListener.onStartRecording(this);
try {
this.recognizer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onPost() {
}
@Override
public void onFailed(int i) {
this.mRecognizeListener.onRecognizingResult(ErrorCode.NO_RECORDING_PERMISSION, null);
}
private void combineResult(String str, boolean z) {
}
}