删除无用代码,main函数测试播放711音频数据成功暂存

This commit is contained in:
Daisy 2023-12-26 16:34:30 +08:00
parent 2aa56b65de
commit 1806c15c2f
2 changed files with 15 additions and 95 deletions

View File

@ -135,7 +135,8 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
// CallTalk talkClass = CallTalk();
// talkClass.getAVData(uint8List, 90);
_initializeAudioPlayer();
//711
// _initializeAudioPlayer();
}
//
@ -164,19 +165,6 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
// });
}
Uint8List convertToInt8ListLittleEndian(List<int> intList) {
List<int> 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<void> _initializeAudioPlayer() async {
await _audioPlayer.closePlayer();
@ -201,11 +189,11 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
Future<void> _playRawData(List<int> 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,

View File

@ -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<List<int>> readAssetFile(String assetPath) async {
@ -27,85 +20,24 @@ class G711 {
return (aVal & 0x80) != 0 ? 0x84 - t : t - 0x84;
}
//711pcm数据
List<int> convertList(List<int> aLawList) {
// ALawToLinear List<int>
List<int> linearList = aLawList.map(ALawToLinear).toList();
return linearList;
}
Future<Uint8List?> decodeG711ToPCM(List<int> g711Data) async {
// FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg();
//List<int>Uint8List
Uint8List convertToInt8ListLittleEndian(List<int> intList) {
List<int> 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<Uint8List?> 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;
// }
// }
}