删除无用代码,main函数测试播放711音频数据成功暂存
This commit is contained in:
parent
2aa56b65de
commit
1806c15c2f
@ -135,7 +135,8 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
|||||||
// CallTalk talkClass = CallTalk();
|
// CallTalk talkClass = CallTalk();
|
||||||
// talkClass.getAVData(uint8List, 90);
|
// 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 {
|
Future<void> _initializeAudioPlayer() async {
|
||||||
await _audioPlayer.closePlayer();
|
await _audioPlayer.closePlayer();
|
||||||
@ -201,11 +189,11 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
|||||||
|
|
||||||
Future<void> _playRawData(List<int> rawData) async {
|
Future<void> _playRawData(List<int> rawData) async {
|
||||||
print(
|
print(
|
||||||
'啊啦啦啦啦啦数据长度为:${convertToInt8ListLittleEndian(rawData).length}, 数据为 ${convertToInt8ListLittleEndian(rawData)}');
|
'啊啦啦啦啦啦数据长度为:${G711().convertToInt8ListLittleEndian(rawData).length}, 数据为 ${G711().convertToInt8ListLittleEndian(rawData)}');
|
||||||
|
|
||||||
await _audioPlayer.startPlayer(
|
await _audioPlayer.startPlayer(
|
||||||
fromURI: null,
|
fromURI: null,
|
||||||
fromDataBuffer: convertToInt8ListLittleEndian(rawData),
|
fromDataBuffer: G711().convertToInt8ListLittleEndian(rawData),
|
||||||
codec: Codec.pcm16, //_codec,
|
codec: Codec.pcm16, //_codec,
|
||||||
sampleRate: 8000, //tSAMPLERATE,
|
sampleRate: 8000, //tSAMPLERATE,
|
||||||
numChannels: 1,
|
numChannels: 1,
|
||||||
|
|||||||
@ -1,12 +1,5 @@
|
|||||||
import 'dart:async';
|
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:flutter/services.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
|
||||||
|
|
||||||
class G711 {
|
class G711 {
|
||||||
Future<List<int>> readAssetFile(String assetPath) async {
|
Future<List<int>> readAssetFile(String assetPath) async {
|
||||||
@ -27,85 +20,24 @@ class G711 {
|
|||||||
return (aVal & 0x80) != 0 ? 0x84 - t : t - 0x84;
|
return (aVal & 0x80) != 0 ? 0x84 - t : t - 0x84;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//711解码为pcm数据
|
||||||
List<int> convertList(List<int> aLawList) {
|
List<int> convertList(List<int> aLawList) {
|
||||||
// 将 ALawToLinear 函数应用于 List<int>
|
// 将 ALawToLinear 函数应用于 List<int>
|
||||||
List<int> linearList = aLawList.map(ALawToLinear).toList();
|
List<int> linearList = aLawList.map(ALawToLinear).toList();
|
||||||
return linearList;
|
return linearList;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Uint8List?> decodeG711ToPCM(List<int> g711Data) async {
|
//List<int>转为Uint8List
|
||||||
// FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg();
|
Uint8List convertToInt8ListLittleEndian(List<int> intList) {
|
||||||
|
List<int> int8List = [];
|
||||||
|
|
||||||
// Save G.711 data to a temporary file
|
for (int intValue in intList) {
|
||||||
Directory appDocDir = await getApplicationDocumentsDirectory();
|
intValue = intValue * 2;
|
||||||
String g711FilePath = '${appDocDir.path}/input.g711';
|
// 将 int 拆分为两个字节,采用小端序
|
||||||
await File(g711FilePath).writeAsBytes(g711Data);
|
int8List.add(intValue & 0xFF); // 低 8 位
|
||||||
|
int8List.add((intValue & 0xFF00) >> 8); // 高 8 位
|
||||||
// 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');
|
|
||||||
// 处理异常情况
|
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user