[bugfix] can not read the last sentence

This commit is contained in:
2025-08-18 16:30:53 +08:00
parent 028471a2b7
commit e9ba8d03db
4 changed files with 26 additions and 18 deletions

View File

@@ -64,7 +64,6 @@ public class MainActivity extends FlutterActivity implements INativeNuiCallback
@Override
public void configureFlutterEngine(FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
System.out.println("??????????????????????????????????");
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), TTS_CHANNEL)
.setMethodCallHandler((call, result) -> {
Map<String, Object> args = (Map<String, Object>) call.arguments;
@@ -84,9 +83,11 @@ public class MainActivity extends FlutterActivity implements INativeNuiCallback
}
sendTts(textArg.toString());
break;
case "complete":
completeTts();
break;
case "stop":
stopTts();
result.success("已停止");
break;
default:
result.notImplemented();
@@ -129,6 +130,7 @@ public class MainActivity extends FlutterActivity implements INativeNuiCallback
protected void onStop() {
Log.i(TAG, "onStop");
super.onStop();
asrInstance.release();
}
@@ -189,6 +191,10 @@ public class MainActivity extends FlutterActivity implements INativeNuiCallback
streamInputTtsInstance.sendStreamInputTts(text);
}
private void completeTts() {
streamInputTtsInstance.stopStreamInputTts();
}
private void stopTts() {
streamInputTtsInstance.cancelStreamInputTts();
ttsAudioTrack.stop();
@@ -196,16 +202,6 @@ public class MainActivity extends FlutterActivity implements INativeNuiCallback
private void startAsr() {
asrText = "";
if (asrAudioRecorder == null) {
asrAudioRecorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
ASR_SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
ASR_WAVE_FRAM_SIZE * 4);
Log.d(TAG, "AudioRecorder new ...");
} else {
Log.w(TAG, "AudioRecord has been new ...");
}
asrHandler.post(() -> {
String setParamsString = genAsrParams();
Log.i(TAG, "nui set params " + setParamsString);
@@ -281,9 +277,12 @@ public class MainActivity extends FlutterActivity implements INativeNuiCallback
Log.i(TAG, "onNuiAudioStateChanged");
if (state == Constants.AudioState.STATE_OPEN) {
Log.i(TAG, "audio recorder start");
if (asrAudioRecorder != null) {
asrAudioRecorder.startRecording();
}
asrAudioRecorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
ASR_SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
ASR_WAVE_FRAM_SIZE * 4);
asrAudioRecorder.startRecording();
Log.i(TAG, "audio recorder start done");
} else if (state == Constants.AudioState.STATE_CLOSE) {
Log.i(TAG, "audio recorder close");

View File

@@ -62,6 +62,7 @@ class ChatSseService {
if (line.startsWith('data:')) {
final jsonStr = line.substring(5).trim();
if (jsonStr == '[DONE]' || jsonStr.contains('message_end')) {
TtsUtil.complete();
onStreamResponse(messageId, responseText, true);
break;
}
@@ -89,8 +90,8 @@ class ChatSseService {
await _startTtsFuture;
_isFirstData = false;
}
print("----------------------Send text: " + completeText);
completeText += isChinese ? '' : ',';
print("----------------------Send text: " + completeText);
TtsUtil.send(completeText);
}
tempText = tempText.substring(endIndex).trim();
@@ -108,6 +109,7 @@ class ChatSseService {
} catch (e) {
// todo
} finally {
print("------------------------------finally");
resetRequest();
}
}
@@ -123,7 +125,7 @@ class ChatSseService {
Future<void> abort() async {
_isAborted = true;
TtsUtil.stopTts();
TtsUtil.stop();
resetRequest();
}
@@ -136,6 +138,7 @@ class ChatSseService {
_currentResponse = null;
_isFirstData = true;
_isTtsStarted = false;
_startTtsFuture = null;
}
int _getCompleteTextEndIndex(String buffer) {

View File

@@ -57,6 +57,8 @@ class CommonUtil {
cleanedText = cleanedText.replaceAllMapped(
RegExp(r'UNYX', caseSensitive: false), (m) => 'Unix');
}
cleanedText = cleanedText.replaceAllMapped(
RegExp(r'400-023-4567', caseSensitive: false), (m) => '4 0 0 - 0 2 3 - 4 5 6 7');
}
return cleanedText;

View File

@@ -17,7 +17,11 @@ class TtsUtil {
await execute('send', {'text': text});
}
static Future<void> stopTts() async {
static Future<void> complete() async {
await execute('complete');
}
static Future<void> stop() async {
await execute('stop');
}
}