[bugfix] asr unfinished
This commit is contained in:
@@ -32,7 +32,8 @@ public class MainActivity extends FlutterActivity implements INativeNuiCallback
|
||||
private static final String ACCESS_KEY = "LTAI5t71JHxXRvt2mGuEVz9X";
|
||||
private static final String ACCESS_KEY_SECRET = "WQOUWvngxmCg4CxIG0qkSlkcH5hrVT";
|
||||
private static final String URL = "wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1";
|
||||
|
||||
private static final String chineseVoice = "zhitian_emo";
|
||||
private static final String englishVoice = "abby";
|
||||
private final NativeNui streamInputTtsInstance = new NativeNui(Constants.ModeType.MODE_STREAM_INPUT_TTS);
|
||||
private final NativeNui asrInstance = new NativeNui();
|
||||
|
||||
@@ -337,11 +338,11 @@ public class MainActivity extends FlutterActivity implements INativeNuiCallback
|
||||
String str = "";
|
||||
try {
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("voice", isChinese ? "zhimiao_emo" : "beth");
|
||||
object.put("voice", isChinese ? chineseVoice : englishVoice);
|
||||
object.put("format", "pcm");
|
||||
object.put("sample_rate", 16000);
|
||||
object.put("volume", 100);
|
||||
object.put("speech_rate", 0);
|
||||
object.put("speech_rate", 50);
|
||||
object.put("pitch_rate", 0);
|
||||
str = object.toString();
|
||||
} catch (JSONException e) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:ai_chat_assistant/utils/common_util.dart';
|
||||
import 'package:ai_chat_assistant/utils/tts_util.dart';
|
||||
import 'package:basic_intl/intl.dart';
|
||||
@@ -19,14 +21,16 @@ import 'command_service.dart';
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
|
||||
class MessageService extends ChangeNotifier {
|
||||
static const MethodChannel _channel = MethodChannel('com.example.ai_chat_assistant/asr');
|
||||
static const MethodChannel _asrChannel = MethodChannel('com.example.ai_chat_assistant/asr');
|
||||
|
||||
static final MessageService _instance = MessageService._internal();
|
||||
|
||||
factory MessageService() => _instance;
|
||||
|
||||
Completer<String>? _asrCompleter;
|
||||
|
||||
MessageService._internal() {
|
||||
_channel.setMethodCallHandler((call) async {
|
||||
_asrChannel.setMethodCallHandler((call) async {
|
||||
switch (call.method) {
|
||||
case "onAsrResult":
|
||||
replaceMessage(id: _latestUserMessageId!, text: call.arguments);
|
||||
@@ -34,6 +38,10 @@ class MessageService extends ChangeNotifier {
|
||||
case "onAsrStop":
|
||||
replaceMessage(
|
||||
id: _latestUserMessageId!, status: MessageStatus.normal);
|
||||
if (_asrCompleter != null && !_asrCompleter!.isCompleted) {
|
||||
_asrCompleter!.complete(messages.last.text);
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -92,7 +100,7 @@ class MessageService extends ChangeNotifier {
|
||||
changeState(MessageServiceState.recording);
|
||||
addMessage("", true, MessageStatus.listening);
|
||||
_latestUserMessageId = messages.last.id;
|
||||
_channel.invokeMethod("start");
|
||||
_asrChannel.invokeMethod("start");
|
||||
} catch (e) {
|
||||
print('录音开始出错: $e');
|
||||
}
|
||||
@@ -120,8 +128,9 @@ class MessageService extends ChangeNotifier {
|
||||
}
|
||||
try {
|
||||
changeState(MessageServiceState.recognizing);
|
||||
_channel.invokeMethod("stop");
|
||||
final recognizedText = messages.last.text;
|
||||
_asrChannel.invokeMethod("stop");
|
||||
_asrCompleter = Completer<String>();
|
||||
final recognizedText = await _asrCompleter!.future;
|
||||
// final audioData = await _audioService.stopRecording();
|
||||
// replaceMessage(
|
||||
// id: _latestUserMessageId!,
|
||||
@@ -144,9 +153,10 @@ class MessageService extends ChangeNotifier {
|
||||
changeState(MessageServiceState.replying);
|
||||
await reply(recognizedText);
|
||||
} catch (e) {
|
||||
// todo
|
||||
print(e);
|
||||
} finally {
|
||||
changeState(MessageServiceState.idle);
|
||||
_asrCompleter = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user