音频播放测试通过
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/material.dart';
|
||||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||||
import 'package:flutter_localizations/flutter_localizations.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_screenutil/flutter_screenutil.dart';
|
||||||
import 'package:flutter_sound/flutter_sound.dart';
|
import 'package:flutter_sound/flutter_sound.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
@ -56,6 +57,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
|||||||
var _deviceId = "";
|
var _deviceId = "";
|
||||||
// final audioPlayer = AudioPlayer();
|
// final audioPlayer = AudioPlayer();
|
||||||
final FlutterSoundPlayer _audioPlayer = FlutterSoundPlayer();
|
final FlutterSoundPlayer _audioPlayer = FlutterSoundPlayer();
|
||||||
|
late List<int> allDataBytes;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -167,9 +169,9 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
|||||||
|
|
||||||
//音频相关处理
|
//音频相关处理
|
||||||
Future<void> _initializeAudioPlayer() async {
|
Future<void> _initializeAudioPlayer() async {
|
||||||
await _audioPlayer.closePlayer();
|
// await _audioPlayer.closePlayer();
|
||||||
await _audioPlayer.openPlayer();
|
// await _audioPlayer.openPlayer();
|
||||||
await _audioPlayer.setSubscriptionDuration(Duration(milliseconds: 10));
|
// await _audioPlayer.setSubscriptionDuration(Duration(milliseconds: 10));
|
||||||
|
|
||||||
List<int> pcmBytes;
|
List<int> pcmBytes;
|
||||||
|
|
||||||
@ -180,6 +182,8 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
|||||||
if (audioData.isNotEmpty) {
|
if (audioData.isNotEmpty) {
|
||||||
// 在这里处理你的音频数据
|
// 在这里处理你的音频数据
|
||||||
pcmBytes = G711().convertList(audioData);
|
pcmBytes = G711().convertList(audioData);
|
||||||
|
allDataBytes = pcmBytes;
|
||||||
|
// allDataBytes = pcmBytes.sublist(0, 640);
|
||||||
print('转换pcmBytes数据长度为:${pcmBytes.length}');
|
print('转换pcmBytes数据长度为:${pcmBytes.length}');
|
||||||
_playRawData(pcmBytes);
|
_playRawData(pcmBytes);
|
||||||
} else {
|
} else {
|
||||||
@ -188,16 +192,39 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _playRawData(List<int> rawData) async {
|
Future<void> _playRawData(List<int> rawData) async {
|
||||||
print(
|
// print(
|
||||||
'啊啦啦啦啦啦数据长度为:${G711().convertToInt8ListLittleEndian(rawData).length}, 数据为 ${G711().convertToInt8ListLittleEndian(rawData)}');
|
// '啊啦啦啦啦啦数据长度为:${G711().convertToInt8ListLittleEndian(rawData).length}, 数据为 ${G711().convertToInt8ListLittleEndian(rawData)}');
|
||||||
|
|
||||||
await _audioPlayer.startPlayer(
|
// await _audioPlayer.startPlayer(
|
||||||
fromURI: null,
|
// fromURI: null,
|
||||||
fromDataBuffer: G711().convertToInt8ListLittleEndian(rawData),
|
// fromDataBuffer: G711().convertToInt8ListLittleEndian(rawData),
|
||||||
codec: Codec.pcm16, //_codec,
|
// codec: Codec.pcm16, //_codec,
|
||||||
sampleRate: 8000, //tSAMPLERATE,
|
// sampleRate: 8000, //tSAMPLERATE,
|
||||||
numChannels: 1,
|
// 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
|
@override
|
||||||
|
|||||||
@ -22,12 +22,12 @@ class CallTalk {
|
|||||||
IframeInfo? iframe; // 假设有这个成员变量
|
IframeInfo? iframe; // 假设有这个成员变量
|
||||||
var growableList;
|
var growableList;
|
||||||
bool getFirstFrame = false; //是否得到了第一帧
|
bool getFirstFrame = false; //是否得到了第一帧
|
||||||
List<int> allPcmData = [];
|
|
||||||
int _remainingFrames = 0;
|
|
||||||
bool getAudio = false;
|
|
||||||
|
|
||||||
CallTalk._init() {
|
CallTalk._init() {
|
||||||
iframe = IframeInfo();
|
iframe = IframeInfo();
|
||||||
|
FlutterPcmSound.setLogLevel(LogLevel.error);
|
||||||
|
FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
|
||||||
|
FlutterPcmSound.setFeedThreshold(8000 ~/ 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static CallTalk _share() {
|
static CallTalk _share() {
|
||||||
@ -47,14 +47,6 @@ class CallTalk {
|
|||||||
try {
|
try {
|
||||||
// 将 ALaw 转为 Linear
|
// 将 ALaw 转为 Linear
|
||||||
pcmBytes = G711().convertList(g711Data);
|
pcmBytes = G711().convertList(g711Data);
|
||||||
allPcmData.addAll(pcmBytes);
|
|
||||||
// if (_remainingFrames == 0) {
|
|
||||||
// _remainingFrames = 1;
|
|
||||||
// print('延迟了么1');
|
|
||||||
|
|
||||||
// await Future.delayed(const Duration(milliseconds: 50));
|
|
||||||
// }
|
|
||||||
|
|
||||||
_initializeAudioPlayer(pcmBytes);
|
_initializeAudioPlayer(pcmBytes);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Error decoding G.711 to PCM: $e');
|
print('Error decoding G.711 to PCM: $e');
|
||||||
@ -126,30 +118,9 @@ class CallTalk {
|
|||||||
|
|
||||||
//音频相关处理
|
//音频相关处理
|
||||||
Future<void> _initializeAudioPlayer(List<int> audioData) async {
|
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);
|
PcmArrayInt16 fromList = PcmArrayInt16.fromList(audioData);
|
||||||
await FlutterPcmSound.feed(fromList);
|
await FlutterPcmSound.feed(fromList);
|
||||||
|
|
||||||
FlutterPcmSound.play();
|
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