pcm音频播放插件提交
This commit is contained in:
parent
4e2dfece37
commit
286fce3c09
@ -48,6 +48,8 @@ PODS:
|
||||
- Flutter (1.0.0)
|
||||
- flutter_native_contact_picker (0.0.1):
|
||||
- Flutter
|
||||
- flutter_pcm_sound (0.0.1):
|
||||
- Flutter
|
||||
- flutter_sound (9.2.13):
|
||||
- Flutter
|
||||
- flutter_sound_core (= 9.2.13)
|
||||
@ -117,6 +119,7 @@ DEPENDENCIES:
|
||||
- ffmpeg_kit_flutter (from `.symlinks/plugins/ffmpeg_kit_flutter/ios`)
|
||||
- Flutter (from `Flutter`)
|
||||
- flutter_native_contact_picker (from `.symlinks/plugins/flutter_native_contact_picker/ios`)
|
||||
- flutter_pcm_sound (from `.symlinks/plugins/flutter_pcm_sound/ios`)
|
||||
- flutter_sound (from `.symlinks/plugins/flutter_sound/ios`)
|
||||
- fluttertoast (from `.symlinks/plugins/fluttertoast/ios`)
|
||||
- g711_flutter (from `.symlinks/plugins/g711_flutter/ios`)
|
||||
@ -180,6 +183,8 @@ EXTERNAL SOURCES:
|
||||
:path: Flutter
|
||||
flutter_native_contact_picker:
|
||||
:path: ".symlinks/plugins/flutter_native_contact_picker/ios"
|
||||
flutter_pcm_sound:
|
||||
:path: ".symlinks/plugins/flutter_pcm_sound/ios"
|
||||
flutter_sound:
|
||||
:path: ".symlinks/plugins/flutter_sound/ios"
|
||||
fluttertoast:
|
||||
@ -238,6 +243,7 @@ SPEC CHECKSUMS:
|
||||
ffmpeg_kit_flutter: fb5bee3a6038231463ee99e30f97a5763e0ae40f
|
||||
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
|
||||
flutter_native_contact_picker: bd430ba0fbf82768bb50c2c52a69a65759a8f907
|
||||
flutter_pcm_sound: de0572ca4f99091cc2abfcc31601b8a4ddd33c0e
|
||||
flutter_sound: c60effa2a350fb977885f0db2fbc4c1ad5160900
|
||||
flutter_sound_core: 26c10e5832e76aaacfae252d8925232281c486ae
|
||||
fluttertoast: fafc4fa4d01a6a9e4f772ecd190ffa525e9e2d9c
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import 'package:audioplayers/audioplayers.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_sound/flutter_sound.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:star_lock/talk/call/g711.dart';
|
||||
import 'package:star_lock/talk/call/iFrameInfo.dart';
|
||||
import 'package:star_lock/talk/udp/udp_talkClass.dart';
|
||||
import '../../tools/eventBusEventManage.dart';
|
||||
import 'package:flutter_pcm_sound/flutter_pcm_sound.dart';
|
||||
import 'package:flutter_sound/flutter_sound.dart';
|
||||
|
||||
class CallTalk {
|
||||
static CallTalk? _manager;
|
||||
@ -22,9 +22,9 @@ class CallTalk {
|
||||
IframeInfo? iframe; // 假设有这个成员变量
|
||||
var growableList;
|
||||
bool getFirstFrame = false; //是否得到了第一帧
|
||||
final FlutterSoundPlayer _audioPlayer = FlutterSoundPlayer();
|
||||
List<int> allPcmData = [];
|
||||
int _bufferSize = 400; // 设置缓冲区大小,根据需求调整
|
||||
int _remainingFrames = 0;
|
||||
bool getAudio = false;
|
||||
|
||||
CallTalk._init() {
|
||||
iframe = IframeInfo();
|
||||
@ -48,8 +48,14 @@ class CallTalk {
|
||||
// 将 ALaw 转为 Linear
|
||||
pcmBytes = G711().convertList(g711Data);
|
||||
allPcmData.addAll(pcmBytes);
|
||||
// await Future.delayed(const Duration(seconds: 1));
|
||||
_initializeAudioPlayer(allPcmData);
|
||||
// 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');
|
||||
}
|
||||
@ -118,74 +124,32 @@ class CallTalk {
|
||||
}
|
||||
}
|
||||
|
||||
//视频相关处理
|
||||
void printByteArray(String printTitle, Uint8List byteArray) {
|
||||
final int chunkSize = byteArray.length; // 每行显示的字节数
|
||||
int length = byteArray.length;
|
||||
|
||||
for (int i = 0; i < length; i += chunkSize) {
|
||||
int end = (i + chunkSize < length) ? i + chunkSize : length;
|
||||
Uint8List chunk = byteArray.sublist(i, end);
|
||||
|
||||
String hexString =
|
||||
chunk.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join(' ');
|
||||
debugPrint(hexString);
|
||||
}
|
||||
}
|
||||
|
||||
//音频相关处理
|
||||
Future<void> _initializeAudioPlayer(List<int> audioData) async {
|
||||
// _audioPlayer.closePlayer();
|
||||
_audioPlayer.openPlayer();
|
||||
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);
|
||||
|
||||
// await _audioPlayer
|
||||
// .setSubscriptionDuration(const Duration(milliseconds: 10));
|
||||
if (audioData.isNotEmpty) {
|
||||
// 在这里处理你的音频数据
|
||||
print('转换pcmBytes数据长度为:${audioData.length}');
|
||||
// _playRawData(audioData);
|
||||
playAudioStream(audioData);
|
||||
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('Failed to read audio data.');
|
||||
// 处理长度不足的情况,可以等待更多数据或者采取其他措施
|
||||
print("Not enough data in allPcmData.");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _playRawData(List<int> rawData) async {
|
||||
print(
|
||||
'啊啦啦啦啦啦数据长度为:${G711().convertToInt8ListLittleEndian(rawData).length}, 数据为 ${G711().convertToInt8ListLittleEndian(rawData)}');
|
||||
|
||||
await _audioPlayer.startPlayer(
|
||||
fromDataBuffer: G711().convertToInt8ListLittleEndian(rawData),
|
||||
codec: Codec.pcm16,
|
||||
sampleRate: 8000,
|
||||
numChannels: 1,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> playAudioStream(List<int> audioData) async {
|
||||
await _audioPlayer
|
||||
.setSubscriptionDuration(const Duration(milliseconds: 10));
|
||||
|
||||
// 启动播放器
|
||||
await _audioPlayer.startPlayer(
|
||||
codec: Codec.pcm16,
|
||||
sampleRate: 8000,
|
||||
numChannels: 1,
|
||||
);
|
||||
|
||||
// 将音频数据逐步添加到缓冲区
|
||||
for (int i = 0; i < audioData.length; i += 320) {
|
||||
int chunkSize = 320;
|
||||
if (i + chunkSize > audioData.length) {
|
||||
chunkSize = audioData.length - i;
|
||||
}
|
||||
|
||||
List<int> chunk = audioData.sublist(i, i + chunkSize);
|
||||
await _audioPlayer
|
||||
.feedFromStream(G711().convertToInt8ListLittleEndian(chunk));
|
||||
}
|
||||
|
||||
// 停止播放器
|
||||
await _audioPlayer.stopPlayer();
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@ import audioplayers_darwin
|
||||
import device_info_plus
|
||||
import ffmpeg_kit_flutter
|
||||
import file_selector_macos
|
||||
import flutter_pcm_sound
|
||||
import just_audio
|
||||
import network_info_plus
|
||||
import package_info_plus
|
||||
@ -26,6 +27,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
|
||||
FFmpegKitFlutterPlugin.register(with: registry.registrar(forPlugin: "FFmpegKitFlutterPlugin"))
|
||||
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
|
||||
FlutterPcmSoundPlugin.register(with: registry.registrar(forPlugin: "FlutterPcmSoundPlugin"))
|
||||
JustAudioPlugin.register(with: registry.registrar(forPlugin: "JustAudioPlugin"))
|
||||
NetworkInfoPlusPlugin.register(with: registry.registrar(forPlugin: "NetworkInfoPlusPlugin"))
|
||||
FLTPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlusPlugin"))
|
||||
|
||||
@ -128,6 +128,7 @@ dependencies:
|
||||
ffmpeg_kit_flutter: 5.1.0-LTS
|
||||
fast_gbk: ^1.0.0
|
||||
flutter_audio_capture: ^1.1.6
|
||||
flutter_pcm_sound: ^1.1.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user