Skip to content

Commit

Permalink
feat: implememts all modules for android platform
Browse files Browse the repository at this point in the history
  • Loading branch information
narol1024 committed May 8, 2024
1 parent d2377b5 commit 4d4e436
Show file tree
Hide file tree
Showing 17 changed files with 585 additions and 214 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ RealTimeRecognizerModuleEmitter.addListener(

- onSliceRecognize
- onSegmentSuccessRecognize
- didFinish
- didError
- didStartRecord
- didStopRecord
- didUpdateVolume
- didSaveAudioDataAsFile
- onSliceSuccessRecognize
- onErrorRecognize
- onStartRecord
- onStopRecord
- onUpdateVolume
- onSilentDetectTimeOut(仅支持Android)
- onSaveAudioDataAsFile
- onError

### 一句话识别
Expand Down Expand Up @@ -164,10 +165,10 @@ OneSentenceRecognizerModuleEmitter.addListener(

详细事件列表,请查看SDK文档, https://cloud.tencent.com/document/product/1093/36502

- didRecognize
- didStartRecord
- didEndRecord
- didUpdateVolume
- onRecognize
- onStartRecord
- onStopRecord
- onUpdateVolume
- onError

### 录音文件识别极速版
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ android {

lintOptions {
disable "GradleCompatible"
disable "LongLogTag"
}

compileOptions {
Expand All @@ -84,6 +83,7 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:4.2.2'
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
}

2 changes: 2 additions & 0 deletions android/src/main/java/com/tencentasr/TencentAsrPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.facebook.react.uimanager.ViewManager;
import com.tencentasr.module.FlashFileRecognizerModule;
import com.tencentasr.module.OneSentenceRecognizerModule;
import com.tencentasr.module.RealTimeRecognizerModule;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -19,6 +20,7 @@ public class TencentAsrPackage implements ReactPackage {
List<NativeModule> modules = new ArrayList<>();
modules.add(new FlashFileRecognizerModule(reactContext));
modules.add(new OneSentenceRecognizerModule(reactContext));
modules.add(new RealTimeRecognizerModule(reactContext));
return modules;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void sendEvent(ReactContext reactContext, String eventName,
WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
.emit(ModuleName + "." + eventName, params);
}

@ReactMethod
Expand All @@ -56,7 +56,9 @@ public void addListener(String eventName) {}
@ReactMethod
public void removeListeners(Integer count) {}

// 模块名称
@Override
@NonNull
public String getName() {
return ModuleName;
}
Expand Down Expand Up @@ -126,15 +128,15 @@ public void recognize(ReadableMap configParams, Promise promise) {
if (audioFilePath == null) {
sendErrorEvent(FlashFileRecognizerModuleErrorTypes.PARAMETER_MISSING,
"audioFilePath参数缺失");
promise.reject(new RuntimeException("audioFilePath参数缺失"));
promise.reject("audioFilePath参数缺失");
return;
}

File audioFile = new File(audioFilePath);
if (!audioFile.exists()) {
sendErrorEvent(FlashFileRecognizerModuleErrorTypes.FILE_DOES_NOT_EXIST,
"音频文件不存在");
promise.reject(new RuntimeException("音频文件不存在"));
promise.reject("音频文件不存在");
return;
}
try {
Expand All @@ -149,12 +151,12 @@ public void recognize(ReadableMap configParams, Promise promise) {
} catch (Exception e) {
sendErrorEvent(FlashFileRecognizerModuleErrorTypes.RECOGNIZE_FAILED,
e.getMessage());
promise.reject(new RuntimeException(e.getMessage()));
promise.reject(e.getMessage());
}
} catch (Exception e) {
sendErrorEvent(FlashFileRecognizerModuleErrorTypes.FILE_READ_FAILED,
"读取音频文件失败");
promise.reject(new RuntimeException("读取音频文件失败"));
promise.reject("读取音频文件失败", e);
}
}

Expand All @@ -167,8 +169,7 @@ public void recognizeResult(QCloudFlashRecognizer recognizer, String result,
if (resultJson.getInt("code") == 0) {
_promise.resolve(ReactNativeJsonUtils.convertJsonToMap(resultJson));
} else {
_promise.reject(
new RuntimeException(resultJson.getString("message")));
_promise.reject(resultJson.getString("message"));
}
} catch (Exception e) {
sendErrorEvent(FlashFileRecognizerModuleErrorTypes.RECOGNIZE_FAILED,
Expand All @@ -177,8 +178,8 @@ public void recognizeResult(QCloudFlashRecognizer recognizer, String result,

} else {
sendErrorEvent(FlashFileRecognizerModuleErrorTypes.RECOGNIZE_FAILED,
exception.getLocalizedMessage());
_promise.reject(new RuntimeException(exception.getLocalizedMessage()));
exception.getMessage());
_promise.reject(exception.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.tencentasr.module;
import android.app.Activity;
import android.util.Log;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
Expand Down Expand Up @@ -56,15 +57,17 @@ private void sendEvent(ReactContext reactContext, String eventName,
WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
.emit(ModuleName + "." + eventName, params);
}
@ReactMethod
public void addListener(String eventName) {}

@ReactMethod
public void removeListeners(Integer count) {}

// 模块名称
@Override
@NonNull
public String getName() {
return ModuleName;
}
Expand Down Expand Up @@ -203,13 +206,12 @@ public void recognizeResult(QCloudOneSentenceRecognizer recognizer,
try {
JSONObject resultJson = new JSONObject(result);
JSONObject response = resultJson.getJSONObject("Response");
WritableMap resultBody = Arguments.createMap();
if (response.has("Error")) {
JSONObject errorObject = response.getJSONObject("Error");
sendErrorEvent(errorObject.getString("Code"),
errorObject.getString("Message"));
} else {
sendEvent(_reactContext, "didRecognize",
sendEvent(_reactContext, "onRecognize",
ReactNativeJsonUtils.convertJsonToMap(response));
}
} catch (Exception e) {
Expand All @@ -223,6 +225,20 @@ public void recognizeResult(QCloudOneSentenceRecognizer recognizer,
}
}

public void didStartRecord() { Log.i("一句话识别模块", "didStartRecord"); }
public void didStopRecord() { Log.d("一句话识别模块", "didStopRecord"); }
public void didStartRecord() {
Log.i(ModuleName, "回调didStartRecord");
sendEvent(_reactContext, "onStartRecord", Arguments.createMap());
}

public void didStopRecord() {
Log.i(ModuleName, "回调didStopRecord");
sendEvent(_reactContext, "onStopRecord", Arguments.createMap());
}

public void didUpdateVolume(int volumn) {
Log.i(ModuleName, "回调didUpdateVolume");
WritableMap resultBody = Arguments.createMap();
resultBody.putInt("volumn", volumn);
sendEvent(_reactContext, "onUpdateVolume", resultBody);
}
}
Loading

0 comments on commit 4d4e436

Please sign in to comment.