提交流格式播放代码
This commit is contained in:
parent
9848852f9f
commit
d830165402
@ -143,8 +143,8 @@ class CallTalk {
|
|||||||
if (audioData.isNotEmpty) {
|
if (audioData.isNotEmpty) {
|
||||||
// 在这里处理你的音频数据
|
// 在这里处理你的音频数据
|
||||||
print('转换pcmBytes数据长度为:${audioData.length}');
|
print('转换pcmBytes数据长度为:${audioData.length}');
|
||||||
_playRawData(audioData);
|
// _playRawData(audioData);
|
||||||
// _playAudioStream(audioData);
|
playAudioStream(audioData);
|
||||||
} else {
|
} else {
|
||||||
print('Failed to read audio data.');
|
print('Failed to read audio data.');
|
||||||
}
|
}
|
||||||
@ -162,32 +162,30 @@ class CallTalk {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _playAudioStream(List<int> audioData) async {
|
Future<void> playAudioStream(List<int> audioData) async {
|
||||||
|
await _audioPlayer
|
||||||
|
.setSubscriptionDuration(const Duration(milliseconds: 10));
|
||||||
|
|
||||||
|
// 启动播放器
|
||||||
await _audioPlayer.startPlayer(
|
await _audioPlayer.startPlayer(
|
||||||
codec: Codec.pcm16,
|
codec: Codec.pcm16,
|
||||||
sampleRate: 8000,
|
sampleRate: 8000,
|
||||||
numChannels: 1,
|
numChannels: 1,
|
||||||
);
|
);
|
||||||
|
|
||||||
int totalLength = audioData.length;
|
// 将音频数据逐步添加到缓冲区
|
||||||
int offset = 0;
|
for (int i = 0; i < audioData.length; i += 320) {
|
||||||
|
int chunkSize = 320;
|
||||||
while (offset < totalLength) {
|
if (i + chunkSize > audioData.length) {
|
||||||
int chunkSize = _bufferSize;
|
chunkSize = audioData.length - i;
|
||||||
if (offset + chunkSize > totalLength) {
|
|
||||||
chunkSize = totalLength - offset;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
List<int> chunk = audioData.sublist(offset, offset + chunkSize);
|
List<int> chunk = audioData.sublist(i, i + chunkSize);
|
||||||
await _audioPlayer.feedFromStream(Uint8List.fromList(chunk));
|
await _audioPlayer
|
||||||
|
.feedFromStream(G711().convertToInt8ListLittleEndian(chunk));
|
||||||
offset += chunkSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await _audioPlayer.stopPlayer();
|
// 停止播放器
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> stopAudioPlayer() async {
|
|
||||||
await _audioPlayer.stopPlayer();
|
await _audioPlayer.stopPlayer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user