梳理对讲录音功能相关逻辑并优化

解决录音只有第一次生效,后面不能正常发送录音功能问题
新增对讲过程中的截屏功能
This commit is contained in:
Daisy 2024-04-02 14:07:47 +08:00
parent 4c9ff85e4a
commit 266f68e5a6
3 changed files with 197 additions and 111 deletions

View File

@ -163,48 +163,77 @@ class LockMonitoringLogic extends BaseGetXController {
Get.back();
}
//
Future<void> startProcessing() async {
state.sendEnd.value = false;
frameListener(List<int> frame) async {
List<int> pcmBytes = listLinearToULaw(frame);
await Future.delayed(const Duration(milliseconds: 10));
sendRecordData({
"bytes": pcmBytes,
// "udpSendDataFrameNumber": 0,
"lockID": UDPManage().lockId,
"lockIP": UDPManage().host,
"userMobile": await state.userUid,
"userMobileIP": await state.userMobileIP,
});
}
errorListener(VoiceProcessorException error) {
print("VoiceProcessorException: $error");
}
state.voiceProcessor?.addFrameListener(frameListener);
state.voiceProcessor?.addErrorListener(errorListener);
state.isButtonDisabled.value = true;
state.voiceProcessor?.addFrameListener(_onFrame);
state.voiceProcessor?.addErrorListener(_onError);
try {
if (await state.voiceProcessor?.hasRecordAudioPermission() ?? false) {
await state.voiceProcessor?.start(320, 8000);
await state.voiceProcessor?.start(state.frameLength, state.sampleRate);
bool? isRecording = await state.voiceProcessor?.isRecording();
} else {}
state.isProcessing.value = isRecording!;
} else {
state.errorMessage.value = "Recording permission not granted";
}
} on PlatformException catch (ex) {
Get.log("PlatformException: $ex");
} finally {}
state.errorMessage.value = "Failed to start recorder: $ex";
} finally {
state.isButtonDisabled.value = false;
}
}
double _calculateVolumeLevel(List<int> frame) {
double rms = 0.0;
for (int sample in frame) {
rms += pow(sample, 2);
}
rms = sqrt(rms / frame.length);
double dbfs = 20 * log(rms / 32767.0) / log(10);
double normalizedValue = (dbfs + state.dbOffset) / state.dbOffset;
return normalizedValue.clamp(0.0, 1.0);
}
Future<void> _onFrame(List<int> frame) async {
double volumeLevel = _calculateVolumeLevel(frame);
if (state.volumeHistory.value.length == state.volumeHistoryCapacity) {
state.volumeHistory.value.removeAt(0);
}
state.volumeHistory.value.add(volumeLevel);
state.smoothedVolumeValue.value =
state.volumeHistory.value.reduce((a, b) => a + b) /
state.volumeHistory.value.length;
List<int> pcmBytes = listLinearToULaw(frame);
await Future.delayed(const Duration(milliseconds: 100));
sendRecordData({
"bytes": pcmBytes,
// "udpSendDataFrameNumber": 0,
"lockID": UDPManage().lockId,
"lockIP": UDPManage().host,
"userMobile": await state.userUid,
"userMobileIP": await state.userMobileIP,
});
}
void _onError(VoiceProcessorException error) {
state.errorMessage.value = error.message!;
}
Future<void> stopProcessing() async {
state.isButtonDisabled.value = true;
try {
await state.voiceProcessor?.stop();
} on PlatformException catch (ex) {
Get.log("PlatformException: $ex");
} finally {}
}
void onError(Object e) {
print(e);
state.errorMessage.value = "Failed to stop recorder: $ex";
} finally {
bool? isRecording = await state.voiceProcessor?.isRecording();
state.isProcessing.value = isRecording!;
state.isButtonDisabled.value = false;
}
}
sendRecordData(Map<String, dynamic> args) async {

View File

@ -1,9 +1,14 @@
import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path_provider/path_provider.dart';
import 'package:star_lock/talk/call/callTalk.dart';
import '../../../../app_settings/app_colors.dart';
@ -36,85 +41,88 @@ class _LockMonitoringPageState extends State<LockMonitoringPage> {
Widget build(BuildContext context) {
return PopScope(
canPop: false,
child: Container(
width: 1.sw,
height: 1.sh,
color: Colors.white,
child: Stack(
children: [
Obx(() {
if (state.listPhotoData.value.isEmpty ||
state.listPhotoData.value.length < 10) {
return Container(color: Colors.transparent);
} else {
return Image.memory(
state.listPhotoData.value,
gaplessPlayback: true,
width: 1.sw,
height: 1.sh,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Container(color: Colors.transparent);
},
);
}
}),
Positioned(
top: ScreenUtil().statusBarHeight + 30.h,
width: 1.sw,
child: Obx(() {
var sec = (state.oneMinuteTime.value % 60)
.toString()
.padLeft(2, '0');
var min = (state.oneMinuteTime.value ~/ 60)
.toString()
.padLeft(2, '0');
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("$min:$sec",
style: TextStyle(
fontSize: 26.sp, color: Colors.white)),
// SizedBox(width: 30.w),
// GestureDetector(
// onTap: () {
// Get.back();
// },
// child: Container(
// decoration: BoxDecoration(
// color: Colors.white,
// borderRadius: BorderRadius.circular(25.h)),
// padding: EdgeInsets.all(10.w),
// child: Image(
// width: 40.w,
// height: 40.w,
// image: const AssetImage("images/icon_left_black.png"),
// ),
// ),
// ),
]);
child: RepaintBoundary(
key: state.globalKey,
child: Container(
width: 1.sw,
height: 1.sh,
color: Colors.white,
child: Stack(
children: [
Obx(() {
if (state.listPhotoData.value.isEmpty ||
state.listPhotoData.value.length < 10) {
return Container(color: Colors.transparent);
} else {
return Image.memory(
state.listPhotoData.value,
gaplessPlayback: true,
width: 1.sw,
height: 1.sh,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Container(color: Colors.transparent);
},
);
}
}),
),
Positioned(
bottom: 10.w,
child: Container(
width: 1.sw - 30.w * 2,
// height: 300.h,
margin: EdgeInsets.all(30.w),
decoration: BoxDecoration(
color: const Color(0xC83C3F41),
borderRadius: BorderRadius.circular(20.h)),
child: Column(
children: [
SizedBox(height: 20.h),
bottomTopBtnWidget(),
SizedBox(height: 20.h),
bottomBottomBtnWidget(),
SizedBox(height: 20.h),
],
),
))
],
Positioned(
top: ScreenUtil().statusBarHeight + 30.h,
width: 1.sw,
child: Obx(() {
var sec = (state.oneMinuteTime.value % 60)
.toString()
.padLeft(2, '0');
var min = (state.oneMinuteTime.value ~/ 60)
.toString()
.padLeft(2, '0');
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("$min:$sec",
style: TextStyle(
fontSize: 26.sp, color: Colors.white)),
// SizedBox(width: 30.w),
// GestureDetector(
// onTap: () {
// Get.back();
// },
// child: Container(
// decoration: BoxDecoration(
// color: Colors.white,
// borderRadius: BorderRadius.circular(25.h)),
// padding: EdgeInsets.all(10.w),
// child: Image(
// width: 40.w,
// height: 40.w,
// image: const AssetImage("images/icon_left_black.png"),
// ),
// ),
// ),
]);
}),
),
Positioned(
bottom: 10.w,
child: Container(
width: 1.sw - 30.w * 2,
// height: 300.h,
margin: EdgeInsets.all(30.w),
decoration: BoxDecoration(
color: const Color(0xC83C3F41),
borderRadius: BorderRadius.circular(20.h)),
child: Column(
children: [
SizedBox(height: 20.h),
bottomTopBtnWidget(),
SizedBox(height: 20.h),
bottomBottomBtnWidget(),
SizedBox(height: 20.h),
],
),
))
],
),
),
));
}
@ -144,6 +152,7 @@ class _LockMonitoringPageState extends State<LockMonitoringPage> {
//
GestureDetector(
onTap: () {
captureAndSavePng();
// Get.toNamed(Routers.monitoringRealTimeScreenPage);
},
child: Container(
@ -198,13 +207,16 @@ class _LockMonitoringPageState extends State<LockMonitoringPage> {
state.udpStatus.value = 9;
}
// logic.readG711Data();
logic.startProcessing();
if (state.isProcessing.value == false) {
logic.startProcessing();
}
}, longPressUp: () async {
//
print("onLongPressUp");
if (state.udpStatus.value == 9) {
state.udpStatus.value = 8;
}
logic.stopProcessing();
})),
bottomBtnItemWidget(
"images/main/icon_lockDetail_hangUp.png", "挂断", Colors.red, () async {
@ -337,6 +349,41 @@ class _LockMonitoringPageState extends State<LockMonitoringPage> {
});
}
Future<void> captureAndSavePng() async {
try {
if (state.globalKey.currentContext == null) {
print('截图失败: 未找到当前上下文');
return;
}
RenderRepaintBoundary boundary = state.globalKey.currentContext!
.findRenderObject() as RenderRepaintBoundary;
ui.Image image = await boundary.toImage();
ByteData? byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
if (byteData == null) {
print('截图失败: 图像数据为空');
return;
}
Uint8List pngBytes = byteData.buffer.asUint8List();
//
final directory = await getApplicationDocumentsDirectory();
final imagePath = '${directory.path}/screenshot.png';
//
File imgFile = File(imagePath);
await imgFile.writeAsBytes(pngBytes);
//
await ImageGallerySaver.saveFile(imagePath);
print('截图保存路径: $imagePath');
logic.showToast('截图已保存到相册');
} catch (e) {
print('截图失败: $e');
}
}
@override
void dispose() {
super.dispose();

View File

@ -23,9 +23,19 @@ class LockMonitoringState {
var listPhotoData = Uint8List(0).obs; //
var listAudioData = <int>[].obs; //
//
late final VoiceProcessor? voiceProcessor;
var sendEnd = false.obs; //
late StreamSubscription<List<int>> frameSubscription;
var isProcessing = false.obs; //
var isButtonDisabled = false.obs; //
final int frameLength = 320; //320
final int sampleRate = 8000; //8000
final int volumeHistoryCapacity = 5; //
final double dbOffset = 50.0; //
var volumeHistory = <double>[].obs; //
var smoothedVolumeValue = 0.0.obs; //
var errorMessage = ''.obs;
GlobalKey globalKey = GlobalKey();
late Timer oneMinuteTimeTimer =
Timer(const Duration(seconds: 1), () {}); // 60