From 9848852f9f546cb0315e8ad9b13af2d32b34fbd9 Mon Sep 17 00:00:00 2001 From: Daisy <> Date: Wed, 27 Dec 2023 13:59:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=92=AD=E6=94=BE=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- star_lock/lib/talk/call/callTalk.dart | 85 +++++++++++++++++---------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/star_lock/lib/talk/call/callTalk.dart b/star_lock/lib/talk/call/callTalk.dart index ab040e77..93455b70 100644 --- a/star_lock/lib/talk/call/callTalk.dart +++ b/star_lock/lib/talk/call/callTalk.dart @@ -1,8 +1,10 @@ import 'dart:typed_data'; +import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; import 'package:flutter_sound/flutter_sound.dart'; import 'package:star_lock/talk/call/g711.dart'; import 'package:star_lock/talk/call/iFrameInfo.dart'; +import 'package:star_lock/talk/udp/udp_talkClass.dart'; import '../../tools/eventBusEventManage.dart'; class CallTalk { @@ -21,7 +23,8 @@ class CallTalk { var growableList; bool getFirstFrame = false; //是否得到了第一帧 final FlutterSoundPlayer _audioPlayer = FlutterSoundPlayer(); - List? allPcmData = []; + List allPcmData = []; + int _bufferSize = 400; // 设置缓冲区大小,根据需求调整 CallTalk._init() { iframe = IframeInfo(); @@ -40,35 +43,16 @@ class CallTalk { if (bb[61] == 1) { Uint8List g711Data = bb.sublist(77, bb.length); - // // 解码为 PCM 数据 - // Uint8List pcmData = G711Decoder().g711Decode(g711Data, G711Type.uLaw); - // // 现在你可以使用 pcmData 进行播放或其他处理 - // print('得到的pcmData:$pcmData'); - // allPcmData!.addAll(pcmData); - // await Future.delayed(const Duration(seconds: 3)); - // _playRawData(allPcmData!); - - print('dinglingling bb.length:${g711Data.length} 音频数据来:$g711Data '); - - List pcmBytes = Uint8List.fromList([]); + List pcmBytes; try { // 将 ALaw 转为 Linear pcmBytes = G711().convertList(g711Data); + allPcmData.addAll(pcmBytes); + // await Future.delayed(const Duration(seconds: 1)); + _initializeAudioPlayer(allPcmData); } catch (e) { print('Error decoding G.711 to PCM: $e'); } - - allPcmData!.addAll(pcmBytes!); - await Future.delayed(const Duration(seconds: 1)); - - print('PCM decoded data: $pcmBytes'); - - if (pcmBytes != null) { - _initializeAudioPlayer(); - _playRawData(Uint8List.fromList(allPcmData!)); - } else { - print('Error decoding G.711 to PCM'); - } } // 视频数据 else { @@ -150,23 +134,60 @@ class CallTalk { } //音频相关处理 - Future _initializeAudioPlayer() async { - await _audioPlayer.openPlayer(); + Future _initializeAudioPlayer(List audioData) async { + // _audioPlayer.closePlayer(); + _audioPlayer.openPlayer(); + + // await _audioPlayer + // .setSubscriptionDuration(const Duration(milliseconds: 10)); + if (audioData.isNotEmpty) { + // 在这里处理你的音频数据 + print('转换pcmBytes数据长度为:${audioData.length}'); + _playRawData(audioData); + // _playAudioStream(audioData); + } else { + print('Failed to read audio data.'); + } } Future _playRawData(List rawData) async { + print( + '啊啦啦啦啦啦数据长度为:${G711().convertToInt8ListLittleEndian(rawData).length}, 数据为 ${G711().convertToInt8ListLittleEndian(rawData)}'); + await _audioPlayer.startPlayer( - fromDataBuffer: Uint8List.fromList(rawData), + fromDataBuffer: G711().convertToInt8ListLittleEndian(rawData), codec: Codec.pcm16, - numChannels: 1, sampleRate: 8000, - whenFinished: () { - print('Finished playing'); - }, + numChannels: 1, ); } - Future stopLocalAudio() async { + Future _playAudioStream(List audioData) async { + await _audioPlayer.startPlayer( + codec: Codec.pcm16, + sampleRate: 8000, + numChannels: 1, + ); + + int totalLength = audioData.length; + int offset = 0; + + while (offset < totalLength) { + int chunkSize = _bufferSize; + if (offset + chunkSize > totalLength) { + chunkSize = totalLength - offset; + } + + List chunk = audioData.sublist(offset, offset + chunkSize); + await _audioPlayer.feedFromStream(Uint8List.fromList(chunk)); + + offset += chunkSize; + } + + await _audioPlayer.stopPlayer(); + } + + Future stopAudioPlayer() async { await _audioPlayer.stopPlayer(); } }