import 'dart:typed_data'; import 'package:convert/convert.dart'; import 'package:flutter_sound/flutter_sound.dart'; import 'package:get/get.dart'; import 'package:star_lock/appRouters.dart'; // import 'package:just_audio/just_audio.dart'; import 'package:star_lock/main/lockDetail/monitoring/monitoring/lockMonitoring_page.dart'; import 'package:star_lock/talk/call/g711Decoder.dart'; import 'package:star_lock/talk/call/iFrameInfo.dart'; class CallTalk { static int POS_iframe_index = 63; static int POS_alen = 65; static int POS_blen = 73; static int POS_bag_index = 71; static int POS_bag_num = 69; static int POS_data = 77; static int ABUF_NUM = 100; static int FIRSTINDEX = 1; int status = 0; // 假设有这个成员变量 IframeInfo iframe = IframeInfo(); // 假设有这个成员变量 LockMonitoringPage callOut = const LockMonitoringPage(); FlutterSoundPlayer _audioPlayer = FlutterSoundPlayer(); Future getAVData(Uint8List bb, int len) async { //视频数据 // String hexData = // '5858584349449601075439415f396333613730323264346364a5a5a5a5c0a809a3503138363832313530323337000000000000000030303030420e000002000000b33300001f000100b301b301ffd8ffe000114a4649460001010000'; // Uint8List bb = Uint8List.fromList(hex.decode(hexData)); //音频数据 String hexData = '5858584349449601075439415f396333613730323264346364a5a5a5a5c0a809a3503138363832313530323337000000000000000030303030e02900000100050080020000010001004001b30145444444444444444445454444444444444444444444434343434443434344444444444544454545444444444444454545454545454545454445444545454545454545444444444343434344444443434344444444444343434343434343434443434343434343424343434343434242424242424242424243434343434242424242424343434342424343434242424242434243434343434344454344444444444444444444444444454545454544444444444444444445454545454545454545454545454546454545454545454545454545454545454646464646464645454545464646454545454545454545454545454544444444444444444443434343434343434343434343434343434343434242424242424242424242424242424343434242434343434343434343434343434343434343434444444343434343430000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024001d044000000000001d080001000000001d18c03000000000'; Uint8List bb = Uint8List.fromList(hex.decode(hexData)); // 音频数据 if (bb[61] == 1) { // 如果是音频数据且状态符合条件 _initializeAudioPlayer(); // 用你的711音频数据替换这里的Uint8List Uint8List rawData = G711Decoder().decodeG711uLaw(bb); _playRawData(rawData); } // 视频数据 else { int bagLen = getShortFromByte(bb, POS_blen + 2); int getIframeIndex = getShortFromByte(bb, POS_iframe_index); int alen = getShortFromByte(bb, POS_alen); int blen = getShortFromByte(bb, POS_blen); int getBagIndex = bb[POS_bag_index] & 0xff; int getBagNum = bb[POS_bag_num] & 0xff; if (getIframeIndex != iframe.iframeIndex) { iframe = IframeInfo(); iframe.iframeIndex = getIframeIndex; iframe.bagNum = getBagNum; iframe.cur_len = alen; iframe.bb = Uint8List(alen); } iframe.bagReceive++; int start = bagLen * (getBagIndex - FIRSTINDEX); int end = start + blen; // 确保 iframe!.bb 不为 null,并且足够长 if (iframe.bb == null || iframe.bb!.length < end) { iframe.bb = Uint8List(end); } copyBytes(iframe!.bb!, start, bb, POS_data, blen); // 打印输出看看字节数组的内容 print('Copied Bytes: ${iframe.bb}'); Uint8List data = iframe.bb!.sublist(0, iframe.cur_len); Get.toNamed(Routers.lockMonitoringPage, arguments: {"lockId": widget.lockListInfoItemEntity.lockId}); // 打印拷贝后的目标数据 print(iframe.bb); } } int getShortFromByte(Uint8List bb, int pos) { ByteData byteData = ByteData.sublistView(bb, pos, pos + 2); return byteData.getInt16(0, Endian.little); } void copyBytes(Uint8List destination, int destStart, Uint8List source, int sourceStart, int length) { if (destination.length < destStart + length || source.length < sourceStart + length) { // 处理越界的情况,这里你可以抛出异常或者进行其他处理 print('Error: Index out of range'); return; } for (int i = 0; i < length; i++) { destination[destStart + i] = source[sourceStart + i]; } } Future _initializeAudioPlayer() async { await _audioPlayer.openPlayer(); } Future _playRawData(Uint8List rawData) async { await _audioPlayer.startPlayer( fromDataBuffer: rawData, codec: Codec.pcm16, whenFinished: () { // 播放完成时的回调 print("Playback finished"); }, ); } }