Merge branch 'talk_flutter' of https://gitee.com/starlock-cn/app-starlock into talk_flutter
This commit is contained in:
commit
4e2dfece37
@ -1,8 +1,10 @@
|
|||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
import 'package:audioplayers/audioplayers.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_sound/flutter_sound.dart';
|
import 'package:flutter_sound/flutter_sound.dart';
|
||||||
import 'package:star_lock/talk/call/g711.dart';
|
import 'package:star_lock/talk/call/g711.dart';
|
||||||
import 'package:star_lock/talk/call/iFrameInfo.dart';
|
import 'package:star_lock/talk/call/iFrameInfo.dart';
|
||||||
|
import 'package:star_lock/talk/udp/udp_talkClass.dart';
|
||||||
import '../../tools/eventBusEventManage.dart';
|
import '../../tools/eventBusEventManage.dart';
|
||||||
|
|
||||||
class CallTalk {
|
class CallTalk {
|
||||||
@ -21,7 +23,8 @@ class CallTalk {
|
|||||||
var growableList;
|
var growableList;
|
||||||
bool getFirstFrame = false; //是否得到了第一帧
|
bool getFirstFrame = false; //是否得到了第一帧
|
||||||
final FlutterSoundPlayer _audioPlayer = FlutterSoundPlayer();
|
final FlutterSoundPlayer _audioPlayer = FlutterSoundPlayer();
|
||||||
List<int>? allPcmData = [];
|
List<int> allPcmData = [];
|
||||||
|
int _bufferSize = 400; // 设置缓冲区大小,根据需求调整
|
||||||
|
|
||||||
CallTalk._init() {
|
CallTalk._init() {
|
||||||
iframe = IframeInfo();
|
iframe = IframeInfo();
|
||||||
@ -40,35 +43,16 @@ class CallTalk {
|
|||||||
if (bb[61] == 1) {
|
if (bb[61] == 1) {
|
||||||
Uint8List g711Data = bb.sublist(77, bb.length);
|
Uint8List g711Data = bb.sublist(77, bb.length);
|
||||||
|
|
||||||
// // 解码为 PCM 数据
|
List<int> pcmBytes;
|
||||||
// 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<int> pcmBytes = Uint8List.fromList([]);
|
|
||||||
try {
|
try {
|
||||||
// 将 ALaw 转为 Linear
|
// 将 ALaw 转为 Linear
|
||||||
pcmBytes = G711().convertList(g711Data);
|
pcmBytes = G711().convertList(g711Data);
|
||||||
|
allPcmData.addAll(pcmBytes);
|
||||||
|
// await Future.delayed(const Duration(seconds: 1));
|
||||||
|
_initializeAudioPlayer(allPcmData);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Error decoding G.711 to PCM: $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 {
|
else {
|
||||||
@ -150,23 +134,58 @@ class CallTalk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//音频相关处理
|
//音频相关处理
|
||||||
Future<void> _initializeAudioPlayer() async {
|
Future<void> _initializeAudioPlayer(List<int> audioData) async {
|
||||||
await _audioPlayer.openPlayer();
|
// _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<void> _playRawData(List<int> rawData) async {
|
Future<void> _playRawData(List<int> rawData) async {
|
||||||
|
print(
|
||||||
|
'啊啦啦啦啦啦数据长度为:${G711().convertToInt8ListLittleEndian(rawData).length}, 数据为 ${G711().convertToInt8ListLittleEndian(rawData)}');
|
||||||
|
|
||||||
await _audioPlayer.startPlayer(
|
await _audioPlayer.startPlayer(
|
||||||
fromDataBuffer: Uint8List.fromList(rawData),
|
fromDataBuffer: G711().convertToInt8ListLittleEndian(rawData),
|
||||||
codec: Codec.pcm16,
|
codec: Codec.pcm16,
|
||||||
numChannels: 1,
|
|
||||||
sampleRate: 8000,
|
sampleRate: 8000,
|
||||||
whenFinished: () {
|
numChannels: 1,
|
||||||
print('Finished playing');
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> stopLocalAudio() async {
|
Future<void> playAudioStream(List<int> audioData) async {
|
||||||
|
await _audioPlayer
|
||||||
|
.setSubscriptionDuration(const Duration(milliseconds: 10));
|
||||||
|
|
||||||
|
// 启动播放器
|
||||||
|
await _audioPlayer.startPlayer(
|
||||||
|
codec: Codec.pcm16,
|
||||||
|
sampleRate: 8000,
|
||||||
|
numChannels: 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
// 将音频数据逐步添加到缓冲区
|
||||||
|
for (int i = 0; i < audioData.length; i += 320) {
|
||||||
|
int chunkSize = 320;
|
||||||
|
if (i + chunkSize > audioData.length) {
|
||||||
|
chunkSize = audioData.length - i;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<int> chunk = audioData.sublist(i, i + chunkSize);
|
||||||
|
await _audioPlayer
|
||||||
|
.feedFromStream(G711().convertToInt8ListLittleEndian(chunk));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止播放器
|
||||||
await _audioPlayer.stopPlayer();
|
await _audioPlayer.stopPlayer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user