音频播放测试通过
This commit is contained in:
parent
b8a2c78836
commit
0c5e71f231
@ -4,6 +4,7 @@ import 'package:audioplayers/audioplayers.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_pcm_sound/flutter_pcm_sound.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:flutter_sound/flutter_sound.dart';
|
||||
import 'package:get/get.dart';
|
||||
@ -56,6 +57,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
||||
var _deviceId = "";
|
||||
// final audioPlayer = AudioPlayer();
|
||||
final FlutterSoundPlayer _audioPlayer = FlutterSoundPlayer();
|
||||
late List<int> allDataBytes;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -167,9 +169,9 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
||||
|
||||
//音频相关处理
|
||||
Future<void> _initializeAudioPlayer() async {
|
||||
await _audioPlayer.closePlayer();
|
||||
await _audioPlayer.openPlayer();
|
||||
await _audioPlayer.setSubscriptionDuration(Duration(milliseconds: 10));
|
||||
// await _audioPlayer.closePlayer();
|
||||
// await _audioPlayer.openPlayer();
|
||||
// await _audioPlayer.setSubscriptionDuration(Duration(milliseconds: 10));
|
||||
|
||||
List<int> pcmBytes;
|
||||
|
||||
@ -180,6 +182,8 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
||||
if (audioData.isNotEmpty) {
|
||||
// 在这里处理你的音频数据
|
||||
pcmBytes = G711().convertList(audioData);
|
||||
allDataBytes = pcmBytes;
|
||||
// allDataBytes = pcmBytes.sublist(0, 640);
|
||||
print('转换pcmBytes数据长度为:${pcmBytes.length}');
|
||||
_playRawData(pcmBytes);
|
||||
} else {
|
||||
@ -188,16 +192,39 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
||||
}
|
||||
|
||||
Future<void> _playRawData(List<int> rawData) async {
|
||||
print(
|
||||
'啊啦啦啦啦啦数据长度为:${G711().convertToInt8ListLittleEndian(rawData).length}, 数据为 ${G711().convertToInt8ListLittleEndian(rawData)}');
|
||||
// print(
|
||||
// '啊啦啦啦啦啦数据长度为:${G711().convertToInt8ListLittleEndian(rawData).length}, 数据为 ${G711().convertToInt8ListLittleEndian(rawData)}');
|
||||
|
||||
await _audioPlayer.startPlayer(
|
||||
fromURI: null,
|
||||
fromDataBuffer: G711().convertToInt8ListLittleEndian(rawData),
|
||||
codec: Codec.pcm16, //_codec,
|
||||
sampleRate: 8000, //tSAMPLERATE,
|
||||
numChannels: 1,
|
||||
);
|
||||
// await _audioPlayer.startPlayer(
|
||||
// fromURI: null,
|
||||
// fromDataBuffer: G711().convertToInt8ListLittleEndian(rawData),
|
||||
// codec: Codec.pcm16, //_codec,
|
||||
// sampleRate: 8000, //tSAMPLERATE,
|
||||
// numChannels: 1,
|
||||
// );
|
||||
|
||||
FlutterPcmSound.setLogLevel(LogLevel.error);
|
||||
FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
|
||||
FlutterPcmSound.setFeedThreshold(8000 ~/ 2);
|
||||
FlutterPcmSound.setFeedCallback(onFeed);
|
||||
FlutterPcmSound.play();
|
||||
}
|
||||
|
||||
void onFeed(int remainingFrames) async {
|
||||
int framesToFeed = 320;
|
||||
|
||||
if (allDataBytes.length >= framesToFeed) {
|
||||
List<int> frames = allDataBytes.sublist(0, framesToFeed);
|
||||
allDataBytes.removeRange(0, framesToFeed);
|
||||
|
||||
// 将数据传递给 FlutterPcmSound
|
||||
PcmArrayInt16 fromList = PcmArrayInt16.fromList(frames);
|
||||
await FlutterPcmSound.feed(fromList);
|
||||
FlutterPcmSound.play();
|
||||
} else {
|
||||
// 处理长度不足的情况,可以等待更多数据或者采取其他措施
|
||||
print("Not enough data in allPcmData.");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@ -22,12 +22,12 @@ class CallTalk {
|
||||
IframeInfo? iframe; // 假设有这个成员变量
|
||||
var growableList;
|
||||
bool getFirstFrame = false; //是否得到了第一帧
|
||||
List<int> allPcmData = [];
|
||||
int _remainingFrames = 0;
|
||||
bool getAudio = false;
|
||||
|
||||
CallTalk._init() {
|
||||
iframe = IframeInfo();
|
||||
FlutterPcmSound.setLogLevel(LogLevel.error);
|
||||
FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
|
||||
FlutterPcmSound.setFeedThreshold(8000 ~/ 2);
|
||||
}
|
||||
|
||||
static CallTalk _share() {
|
||||
@ -47,14 +47,6 @@ class CallTalk {
|
||||
try {
|
||||
// 将 ALaw 转为 Linear
|
||||
pcmBytes = G711().convertList(g711Data);
|
||||
allPcmData.addAll(pcmBytes);
|
||||
// if (_remainingFrames == 0) {
|
||||
// _remainingFrames = 1;
|
||||
// print('延迟了么1');
|
||||
|
||||
// await Future.delayed(const Duration(milliseconds: 50));
|
||||
// }
|
||||
|
||||
_initializeAudioPlayer(pcmBytes);
|
||||
} catch (e) {
|
||||
print('Error decoding G.711 to PCM: $e');
|
||||
@ -126,30 +118,9 @@ class CallTalk {
|
||||
|
||||
//音频相关处理
|
||||
Future<void> _initializeAudioPlayer(List<int> audioData) async {
|
||||
FlutterPcmSound.setLogLevel(LogLevel.error);
|
||||
FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
|
||||
FlutterPcmSound.setFeedThreshold(8000 * 10);
|
||||
// FlutterPcmSound.setFeedCallback(onFeed);
|
||||
PcmArrayInt16 fromList = PcmArrayInt16.fromList(audioData);
|
||||
await FlutterPcmSound.feed(fromList);
|
||||
|
||||
FlutterPcmSound.play();
|
||||
}
|
||||
|
||||
void onFeed(int remainingFrames) async {
|
||||
int framesToFeed = 320;
|
||||
|
||||
if (allPcmData.length >= framesToFeed) {
|
||||
List<int> frames = allPcmData.sublist(0, framesToFeed);
|
||||
allPcmData.removeRange(0, framesToFeed);
|
||||
|
||||
// 将数据传递给 FlutterPcmSound
|
||||
PcmArrayInt16 fromList = PcmArrayInt16.fromList(frames);
|
||||
await FlutterPcmSound.feed(fromList);
|
||||
FlutterPcmSound.play();
|
||||
} else {
|
||||
// 处理长度不足的情况,可以等待更多数据或者采取其他措施
|
||||
print("Not enough data in allPcmData.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user