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

View File

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

View File

@@ -57,6 +57,8 @@ class CommonUtil {
cleanedText = cleanedText.replaceAllMapped( cleanedText = cleanedText.replaceAllMapped(
RegExp(r'UNYX', caseSensitive: false), (m) => 'Unix'); 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; return cleanedText;

View File

@@ -17,7 +17,11 @@ class TtsUtil {
await execute('send', {'text': text}); 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'); await execute('stop');
} }
} }