From 1806c15c2f4b98ffc3b2647dce6cf84326fd23cc Mon Sep 17 00:00:00 2001 From: Daisy <> Date: Tue, 26 Dec 2023 16:34:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8Cmain=E5=87=BD=E6=95=B0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=92=AD=E6=94=BE711=E9=9F=B3=E9=A2=91=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- star_lock/lib/main.dart | 20 ++----- star_lock/lib/talk/call/g711.dart | 90 ++++--------------------------- 2 files changed, 15 insertions(+), 95 deletions(-) diff --git a/star_lock/lib/main.dart b/star_lock/lib/main.dart index 0912cfb7..5dc9a493 100644 --- a/star_lock/lib/main.dart +++ b/star_lock/lib/main.dart @@ -135,7 +135,8 @@ class _MyAppState extends State with WidgetsBindingObserver, BaseWidget { // CallTalk talkClass = CallTalk(); // talkClass.getAVData(uint8List, 90); - _initializeAudioPlayer(); + //测试播放711音频数据 + // _initializeAudioPlayer(); } //初始化阿里云推送 @@ -164,19 +165,6 @@ class _MyAppState extends State with WidgetsBindingObserver, BaseWidget { // }); } - Uint8List convertToInt8ListLittleEndian(List intList) { - List int8List = []; - - for (int intValue in intList) { - intValue = intValue * 2; - // 将 int 拆分为两个字节,采用小端序 - int8List.add(intValue & 0xFF); // 低 8 位 - int8List.add((intValue & 0xFF00) >> 8); // 高 8 位 - } - - return Uint8List.fromList(int8List); - } - //音频相关处理 Future _initializeAudioPlayer() async { await _audioPlayer.closePlayer(); @@ -201,11 +189,11 @@ class _MyAppState extends State with WidgetsBindingObserver, BaseWidget { Future _playRawData(List rawData) async { print( - '啊啦啦啦啦啦数据长度为:${convertToInt8ListLittleEndian(rawData).length}, 数据为 ${convertToInt8ListLittleEndian(rawData)}'); + '啊啦啦啦啦啦数据长度为:${G711().convertToInt8ListLittleEndian(rawData).length}, 数据为 ${G711().convertToInt8ListLittleEndian(rawData)}'); await _audioPlayer.startPlayer( fromURI: null, - fromDataBuffer: convertToInt8ListLittleEndian(rawData), + fromDataBuffer: G711().convertToInt8ListLittleEndian(rawData), codec: Codec.pcm16, //_codec, sampleRate: 8000, //tSAMPLERATE, numChannels: 1, diff --git a/star_lock/lib/talk/call/g711.dart b/star_lock/lib/talk/call/g711.dart index 0d9aca8f..aaf3afcc 100644 --- a/star_lock/lib/talk/call/g711.dart +++ b/star_lock/lib/talk/call/g711.dart @@ -1,12 +1,5 @@ import 'dart:async'; -import 'dart:ffi' as ffi; -import 'dart:io'; -import 'dart:typed_data'; - -import 'package:ffmpeg_kit_flutter/ffmpeg_kit.dart'; -import 'package:ffmpeg_kit_flutter/return_code.dart'; import 'package:flutter/services.dart'; -import 'package:path_provider/path_provider.dart'; class G711 { Future> readAssetFile(String assetPath) async { @@ -27,85 +20,24 @@ class G711 { return (aVal & 0x80) != 0 ? 0x84 - t : t - 0x84; } +//711解码为pcm数据 List convertList(List aLawList) { // 将 ALawToLinear 函数应用于 List List linearList = aLawList.map(ALawToLinear).toList(); return linearList; } - Future decodeG711ToPCM(List g711Data) async { - // FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg(); +//List转为Uint8List + Uint8List convertToInt8ListLittleEndian(List intList) { + List int8List = []; - // Save G.711 data to a temporary file - Directory appDocDir = await getApplicationDocumentsDirectory(); - String g711FilePath = '${appDocDir.path}/input.g711'; - await File(g711FilePath).writeAsBytes(g711Data); - - // Replace with the desired output PCM file path - String pcmFilePath = '${appDocDir.path}/output.pcm'; - - // Run FFmpeg command to decode G.711 to PCM - // String command = - // '-y -f u8 -ar 8000 -ac 1 -i $g711FilePath -f s16le -ar 8000 -ac 1 $pcmFilePath'; - try { - final session = await FFmpegKit.execute( - '-y -f u8 -ar 8000 -ac 1 -i $g711FilePath -f s16le -ar 8000 -ac 1 $pcmFilePath', - ); - - final returnCode = await session.getReturnCode(); - - if (ReturnCode.isSuccess(returnCode)) { - Uint8List pcmBytes = await File(pcmFilePath).readAsBytes(); - return pcmBytes; - } else { - print('FFmpeg execution failed with rc=$returnCode'); - // 处理执行失败的情况 - } - } catch (e) { - print('Error executing FFmpeg command: $e'); - // 处理异常情况 + for (int intValue in intList) { + intValue = intValue * 2; + // 将 int 拆分为两个字节,采用小端序 + int8List.add(intValue & 0xFF); // 低 8 位 + int8List.add((intValue & 0xFF00) >> 8); // 高 8 位 } - return null; + + return Uint8List.fromList(int8List); } - - // Future encodePCMToG711(Uint8List pcmData) async { - // try { - // // FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg(); - - // // Save PCM data to a temporary file - // Directory appDocDir = await getApplicationDocumentsDirectory(); - // String pcmFilePath = '${appDocDir.path}/input.pcm'; - // await File(pcmFilePath).writeAsBytes(pcmData); - - // // Replace with the desired output G.711 file path - // String g711FilePath = '${appDocDir.path}/output.g711'; - - // // Run FFmpeg command to encode PCM to G.711 - // String command = - // '-y -f s16le -ar 8000 -ac 1 -i $pcmFilePath -f g711 -ar 8000 -ac 1 $g711FilePath'; - // int result = await flutterFFmpeg.execute(command); - - // if (result == 0) { - // print( - // 'PCM encoding to G.711 successful! G.711 file saved at: $g711FilePath'); - - // // Read G.711 data from file - // Uint8List g711Bytes = await File(g711FilePath).readAsBytes(); - - // // Delete the temporary PCM file - // await File(pcmFilePath).delete(); - - // // Delete the temporary G.711 file - // await File(g711FilePath).delete(); - - // return g711Bytes; - // } else { - // print('Error during PCM encoding to G.711: $result'); - // return null; - // } - // } catch (e) { - // print('Error: $e'); - // return null; - // } - // } }