fix:调整音频结束时继续发送为0的音频包,而不是立即中断

This commit is contained in:
liyi 2025-08-26 15:28:18 +08:00
parent d0b8f76303
commit 22887310c0
3 changed files with 52 additions and 50 deletions

View File

@ -602,12 +602,23 @@ class ImageTransmissionLogic extends BaseGetXController {
} on PlatformException catch (ex) { } on PlatformException catch (ex) {
// state.errorMessage.value = 'Failed to stop recorder: $ex'; // state.errorMessage.value = 'Failed to stop recorder: $ex';
} finally { } finally {
//
if (_startProcessingAudioTimer != null) {
// 53200
for (int i = 0; i < 5; i++) {
_bufferedAudioFrames.addAll(List.filled(chunkSize, 0));
}
Future.delayed(const Duration(milliseconds: 300), () {
_startProcessingAudioTimer?.cancel();
_startProcessingAudioTimer = null;
_bufferedAudioFrames.clear();
});
} else {
_bufferedAudioFrames.clear();
}
final bool? isRecording = await state.voiceProcessor?.isRecording(); final bool? isRecording = await state.voiceProcessor?.isRecording();
state.isRecordingAudio.value = isRecording!; state.isRecordingAudio.value = isRecording!;
} }
_startProcessingAudioTimer?.cancel();
_startProcessingAudioTimer = null;
_bufferedAudioFrames.clear();
} }
static const int chunkSize = 320; // 32010ms G.711 static const int chunkSize = 320; // 32010ms G.711
@ -656,27 +667,11 @@ class ImageTransmissionLogic extends BaseGetXController {
AppLog.log(error.message!); AppLog.log(error.message!);
} }
//
List<int> _applyGain(List<int> pcmData, double gainFactor) { List<int> _applyGain(List<int> pcmData, double gainFactor) {
List<int> result = List<int>.filled(pcmData.length, 0); return pcmData.map((sample) {
//
for (int i = 0; i < pcmData.length; i++) { int amplified = (sample * gainFactor).round();
// PCM数据通常是有符号的16位整数 return amplified.clamp(-32768, 32767);
int sample = pcmData[i]; }).toList();
//
double amplified = sample * gainFactor;
//
if (amplified > 32767) {
amplified = 32767;
} else if (amplified < -32768) {
amplified = -32768;
}
result[i] = amplified.toInt();
}
return result;
} }
} }

View File

@ -782,12 +782,23 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
} on PlatformException catch (ex) { } on PlatformException catch (ex) {
// state.errorMessage.value = 'Failed to stop recorder: $ex'; // state.errorMessage.value = 'Failed to stop recorder: $ex';
} finally { } finally {
//
if (_startProcessingAudioTimer != null) {
// 53200
for (int i = 0; i < 5; i++) {
_bufferedAudioFrames.addAll(List.filled(chunkSize, 0));
}
Future.delayed(const Duration(milliseconds: 300), () {
_startProcessingAudioTimer?.cancel();
_startProcessingAudioTimer = null;
_bufferedAudioFrames.clear();
});
} else {
_bufferedAudioFrames.clear();
}
final bool? isRecording = await state.voiceProcessor?.isRecording(); final bool? isRecording = await state.voiceProcessor?.isRecording();
state.isRecordingAudio.value = isRecording!; state.isRecordingAudio.value = isRecording!;
} }
_startProcessingAudioTimer?.cancel();
_startProcessingAudioTimer = null;
_bufferedAudioFrames.clear();
} }
// //

View File

@ -599,12 +599,23 @@ class TalkViewLogic extends BaseGetXController {
} on PlatformException catch (ex) { } on PlatformException catch (ex) {
// state.errorMessage.value = 'Failed to stop recorder: $ex'; // state.errorMessage.value = 'Failed to stop recorder: $ex';
} finally { } finally {
//
if (_startProcessingAudioTimer != null) {
// 53200
for (int i = 0; i < 5; i++) {
_bufferedAudioFrames.addAll(List.filled(chunkSize, 0));
}
Future.delayed(const Duration(milliseconds: 300), () {
_startProcessingAudioTimer?.cancel();
_startProcessingAudioTimer = null;
_bufferedAudioFrames.clear();
});
} else {
_bufferedAudioFrames.clear();
}
final bool? isRecording = await state.voiceProcessor?.isRecording(); final bool? isRecording = await state.voiceProcessor?.isRecording();
state.isRecordingAudio.value = isRecording!; state.isRecordingAudio.value = isRecording!;
} }
_startProcessingAudioTimer?.cancel();
_startProcessingAudioTimer = null;
_bufferedAudioFrames.clear();
} }
static const int chunkSize = 320; // 32010ms G.711 static const int chunkSize = 320; // 32010ms G.711
@ -656,25 +667,10 @@ class TalkViewLogic extends BaseGetXController {
// //
List<int> _applyGain(List<int> pcmData, double gainFactor) { List<int> _applyGain(List<int> pcmData, double gainFactor) {
List<int> result = List<int>.filled(pcmData.length, 0); return pcmData.map((sample) {
//
for (int i = 0; i < pcmData.length; i++) { int amplified = (sample * gainFactor).round();
// PCM数据通常是有符号的16位整数 return amplified.clamp(-32768, 32767);
int sample = pcmData[i]; }).toList();
//
double amplified = sample * gainFactor;
//
if (amplified > 32767) {
amplified = 32767;
} else if (amplified < -32768) {
amplified = -32768;
}
result[i] = amplified.toInt();
}
return result;
} }
} }