|
|
@@ -6,6 +6,7 @@ import 'package:electronic_assistant/dialog/loading_dialog.dart';
|
|
|
import 'package:electronic_assistant/module/record/record_task.dart';
|
|
|
import 'package:electronic_assistant/resource/colors.gen.dart';
|
|
|
import 'package:electronic_assistant/router/app_pages.dart';
|
|
|
+import 'package:electronic_assistant/utils/expand.dart';
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
import 'package:flutter_sound/flutter_sound.dart';
|
|
|
@@ -63,6 +64,10 @@ class RecordHandler {
|
|
|
String get lastRecordId => _lastRecordId ?? '';
|
|
|
StreamSubscription? _currentDurationListener;
|
|
|
StreamSubscription? _recordActionListener;
|
|
|
+ final int maxRecordDuration = 60 * 60 * 5 - 60; //最大录音时长5小时,减去1分钟以防止录音时长超过5小时
|
|
|
+ final int maxRecordSize =
|
|
|
+ 1024 * 1024 * 1024 - 1024 * 1024; //最大录音文件1G,减去1M以防止录音文件超过1G
|
|
|
+ int currentRecordSize = 0;
|
|
|
|
|
|
void init() {
|
|
|
if (currentStatus.value != RecordStatus.recording) {
|
|
|
@@ -134,8 +139,10 @@ class RecordHandler {
|
|
|
double time = _getPcmDuration(
|
|
|
fileLength, recordConfig.sampleRate, 16, recordConfig.numChannels);
|
|
|
currentDuration.value = time;
|
|
|
+ currentRecordSize = fileLength;
|
|
|
} else {
|
|
|
currentDuration.value = 0;
|
|
|
+ currentRecordSize = 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -268,10 +275,24 @@ class RecordHandler {
|
|
|
if (data.isEmpty) {
|
|
|
return;
|
|
|
}
|
|
|
+ if (currentDuration.value >= maxRecordDuration) {
|
|
|
+ ToastUtil.showToast("录音时长已达上限");
|
|
|
+ stopRecord();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (currentRecordSize >= maxRecordSize) {
|
|
|
+ ToastUtil.showToast("录音文件已达上限");
|
|
|
+ stopRecord();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
targetFile.writeAsBytesSync(data, mode: FileMode.append);
|
|
|
+ currentRecordSize += data.length;
|
|
|
currentDuration.value = currentDuration.value +
|
|
|
_getPcmDuration(data.length, recordConfig.sampleRate, 16,
|
|
|
recordConfig.numChannels);
|
|
|
+ debugPrint(
|
|
|
+ "currentDuration: ${formatDuration(currentDuration.value)} , currentSize: ${currentRecordSize.toReadableSize()}");
|
|
|
}, onDone: () {
|
|
|
_changeRecordStatus(RecordStatus.paused);
|
|
|
}, onError: (error) {
|
|
|
@@ -291,6 +312,13 @@ class RecordHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ String formatDuration(double value) {
|
|
|
+ int hour = (value / 3600).floor();
|
|
|
+ int minute = ((value - hour * 3600) / 60).floor();
|
|
|
+ int second = (value - hour * 3600 - minute * 60).floor();
|
|
|
+ return '${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}:${second.toString().padLeft(2, '0')}';
|
|
|
+ }
|
|
|
+
|
|
|
Future<void> _requestForegroundTaskPermission() async {
|
|
|
final NotificationPermission notificationPermission =
|
|
|
await FlutterForegroundTask.checkNotificationPermission();
|
|
|
@@ -315,6 +343,7 @@ class RecordHandler {
|
|
|
KVUtil.putString(keyLastRecordId, "");
|
|
|
_changeRecordStatus(RecordStatus.pending);
|
|
|
currentDuration.value = 0;
|
|
|
+ currentRecordSize = 0;
|
|
|
}
|
|
|
|
|
|
Future<void> _startForegroundService() async {
|