|
|
@@ -1,53 +1,32 @@
|
|
|
import 'dart:io';
|
|
|
-import 'dart:typed_data';
|
|
|
-
|
|
|
import 'package:electronic_assistant/base/base_controller.dart';
|
|
|
import 'package:electronic_assistant/data/bean/talks.dart';
|
|
|
-import 'package:electronic_assistant/data/consts/error_code.dart';
|
|
|
import 'package:electronic_assistant/data/consts/event_report_id.dart';
|
|
|
-import 'package:electronic_assistant/data/repositories/talk_repository.dart';
|
|
|
-import 'package:electronic_assistant/dialog/alert_dialog.dart';
|
|
|
import 'package:electronic_assistant/handler/event_handler.dart';
|
|
|
import 'package:electronic_assistant/module/record/constants.dart';
|
|
|
-import 'package:electronic_assistant/module/record/record_task.dart';
|
|
|
+import 'package:electronic_assistant/module/record/record_handler.dart';
|
|
|
import 'package:electronic_assistant/module/talk/view.dart';
|
|
|
-import 'package:electronic_assistant/resource/string.gen.dart';
|
|
|
-import 'package:electronic_assistant/router/app_pages.dart';
|
|
|
import 'package:electronic_assistant/utils/desktop_shortcut_utils.dart';
|
|
|
-import 'package:electronic_assistant/utils/http_handler.dart';
|
|
|
-import 'package:electronic_assistant/utils/mmkv_util.dart';
|
|
|
import 'package:electronic_assistant/utils/toast_util.dart';
|
|
|
-import 'package:flutter/cupertino.dart';
|
|
|
-import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
|
|
import 'package:get/get.dart';
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
-import 'package:record/record.dart';
|
|
|
-import 'package:uuid/uuid.dart';
|
|
|
-import 'package:wakelock_plus/wakelock_plus.dart';
|
|
|
-import '../../utils/pcm_wav_converter.dart';
|
|
|
+
|
|
|
+import '../../data/consts/error_code.dart';
|
|
|
+import '../../data/repositories/talk_repository.dart';
|
|
|
+import '../../dialog/alert_dialog.dart';
|
|
|
+import '../../router/app_pages.dart';
|
|
|
+import '../../utils/http_handler.dart';
|
|
|
|
|
|
class RecordController extends BaseController {
|
|
|
- static const String keyLastRecordId = "last_record_id";
|
|
|
static const int minRecordDuration = 3;
|
|
|
- final String isRequestSuccessAudio = 'isRequestSuccessAudio';
|
|
|
|
|
|
- final Rx<RecordStatus> currentStatus = RecordStatus.pending.obs;
|
|
|
- final RxDouble currentDuration = 0.0.obs;
|
|
|
- final AudioRecorder _record = AudioRecorder();
|
|
|
- final RecordConfig _recordConfig = RecordConfig(
|
|
|
- encoder: AudioEncoder.pcm16bits,
|
|
|
- bitRate: 16000,
|
|
|
- sampleRate: SampleRate.rate32k.value,
|
|
|
- numChannels: Channel.mono.value,
|
|
|
- );
|
|
|
- late final String _lastRecordId;
|
|
|
+ Rx<RecordStatus> currentStatus = recordHandler.currentStatus;
|
|
|
+ RxDouble currentDuration = recordHandler.currentDuration;
|
|
|
|
|
|
@override
|
|
|
void onInit() {
|
|
|
super.onInit();
|
|
|
- _initLastRecordId();
|
|
|
- _initLastRecordStatus();
|
|
|
- _initForegroundService();
|
|
|
+ recordHandler.init();
|
|
|
}
|
|
|
|
|
|
@override
|
|
|
@@ -55,8 +34,7 @@ class RecordController extends BaseController {
|
|
|
super.onReady();
|
|
|
EventHandler.report(EventId.event_100010,
|
|
|
params: {EventId.id: EventId.id_001});
|
|
|
- // if (KVUtil.getBool(isRequestSuccessAudio, false) &&
|
|
|
- if (!await hasUnUploadRecord()) {
|
|
|
+ if (!await RecordHandler.hasUnUploadRecord()) {
|
|
|
_startOrContinueRecord();
|
|
|
}
|
|
|
}
|
|
|
@@ -64,52 +42,7 @@ class RecordController extends BaseController {
|
|
|
@override
|
|
|
void onClose() {
|
|
|
super.onClose();
|
|
|
- _record.dispose();
|
|
|
- }
|
|
|
-
|
|
|
- void _initLastRecordId() {
|
|
|
- String? lastRecordId = KVUtil.getString(keyLastRecordId, null);
|
|
|
- if (lastRecordId == null || lastRecordId.isEmpty) {
|
|
|
- _lastRecordId = const Uuid().v4();
|
|
|
- KVUtil.putString(keyLastRecordId, _lastRecordId);
|
|
|
- } else {
|
|
|
- _lastRecordId = lastRecordId;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Future<void> _initLastRecordStatus() async {
|
|
|
- var currentRecordFile = await _getCurrentRecordFile();
|
|
|
- var fileLength = currentRecordFile.lengthSync();
|
|
|
- if (currentRecordFile.existsSync() && fileLength > 0) {
|
|
|
- _changeRecordStatus(RecordStatus.paused);
|
|
|
- currentDuration.value = _getPcmDuration(
|
|
|
- fileLength, _recordConfig.sampleRate, 16, _recordConfig.numChannels);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- _initForegroundService() {
|
|
|
- WidgetsBinding.instance
|
|
|
- .addPostFrameCallback((_) => FlutterForegroundTask.init(
|
|
|
- androidNotificationOptions: AndroidNotificationOptions(
|
|
|
- channelId: StringName.recordNotificationChannelId.tr,
|
|
|
- channelName: StringName.recordNotificationChannelName.tr,
|
|
|
- channelDescription:
|
|
|
- StringName.recordNotificationChannelDescription.tr,
|
|
|
- channelImportance: NotificationChannelImportance.LOW,
|
|
|
- priority: NotificationPriority.LOW,
|
|
|
- ),
|
|
|
- iosNotificationOptions: const IOSNotificationOptions(
|
|
|
- showNotification: false,
|
|
|
- playSound: false,
|
|
|
- ),
|
|
|
- foregroundTaskOptions: ForegroundTaskOptions(
|
|
|
- eventAction: ForegroundTaskEventAction.once(),
|
|
|
- autoRunOnBoot: false,
|
|
|
- autoRunOnMyPackageReplaced: true,
|
|
|
- allowWakeLock: true,
|
|
|
- allowWifiLock: false,
|
|
|
- ),
|
|
|
- ));
|
|
|
+ recordHandler.onClose();
|
|
|
}
|
|
|
|
|
|
void addShortcut() {
|
|
|
@@ -117,24 +50,25 @@ class RecordController extends BaseController {
|
|
|
}
|
|
|
|
|
|
void onBackClick() {
|
|
|
- if (currentStatus.value == RecordStatus.pending ||
|
|
|
- currentStatus.value == RecordStatus.paused) {
|
|
|
- Get.back();
|
|
|
- } else {
|
|
|
- EAAlertDialog.show(
|
|
|
- title: "是否保存当前录音?",
|
|
|
- confirmText: "确定",
|
|
|
- cancelText: "取消",
|
|
|
- confirmOnTap: () {
|
|
|
- _saveCurrentRecord();
|
|
|
- EAAlertDialog.dismiss();
|
|
|
- },
|
|
|
- cancelOnTap: () {
|
|
|
- EAAlertDialog.dismiss();
|
|
|
- _stopRecord().then((_) => Get.back());
|
|
|
- },
|
|
|
- );
|
|
|
- }
|
|
|
+ Get.back();
|
|
|
+ // if (currentStatus.value == RecordStatus.pending ||
|
|
|
+ // currentStatus.value == RecordStatus.paused) {
|
|
|
+ // Get.back();
|
|
|
+ // } else {
|
|
|
+ // EAAlertDialog.show(
|
|
|
+ // title: "是否保存当前录音?",
|
|
|
+ // confirmText: "确定",
|
|
|
+ // cancelText: "取消",
|
|
|
+ // confirmOnTap: () {
|
|
|
+ // _saveCurrentRecord();
|
|
|
+ // EAAlertDialog.dismiss();
|
|
|
+ // },
|
|
|
+ // cancelOnTap: () {
|
|
|
+ // EAAlertDialog.dismiss();
|
|
|
+ // _stopRecord().then((_) => Get.back());
|
|
|
+ // },
|
|
|
+ // );
|
|
|
+ // }
|
|
|
}
|
|
|
|
|
|
void onActionClick() {
|
|
|
@@ -142,7 +76,7 @@ class RecordController extends BaseController {
|
|
|
if (nextStatus == RecordStatus.recording) {
|
|
|
_startOrContinueRecord();
|
|
|
} else {
|
|
|
- _stopRecord();
|
|
|
+ recordHandler.stopRecord();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -155,7 +89,7 @@ class RecordController extends BaseController {
|
|
|
confirmText: "删除",
|
|
|
cancelText: "取消",
|
|
|
confirmOnTap: () async {
|
|
|
- await _deleteCurrentRecord();
|
|
|
+ await recordHandler.deleteCurrentRecord();
|
|
|
EAAlertDialog.dismiss();
|
|
|
Get.back();
|
|
|
},
|
|
|
@@ -174,77 +108,7 @@ class RecordController extends BaseController {
|
|
|
}
|
|
|
|
|
|
Future<void> _startOrContinueRecord() async {
|
|
|
- bool hasPermission = await _record.hasPermission();
|
|
|
- if (!hasPermission) {
|
|
|
- _onRecordPermissionDenied();
|
|
|
- return;
|
|
|
- } else {
|
|
|
- KVUtil.putBool(isRequestSuccessAudio, true);
|
|
|
- }
|
|
|
-
|
|
|
- await _requestForegroundTaskPermission().catchError((error) {
|
|
|
- debugPrint("requestForegroundTaskPermission error: $error");
|
|
|
- });
|
|
|
-
|
|
|
- File targetFile = await _getCurrentRecordFile();
|
|
|
- Stream<Uint8List> recordStream = await _record.startStream(_recordConfig);
|
|
|
- _setWakeLock();
|
|
|
- _startForegroundService();
|
|
|
- if (currentStatus.value != RecordStatus.recording) {
|
|
|
- _changeRecordStatus(RecordStatus.recording);
|
|
|
- }
|
|
|
- recordStream.listen((data) async {
|
|
|
- if (data.isEmpty) {
|
|
|
- return;
|
|
|
- }
|
|
|
- targetFile.writeAsBytesSync(data, mode: FileMode.append);
|
|
|
- currentDuration.value = currentDuration.value +
|
|
|
- _getPcmDuration(data.length, _recordConfig.sampleRate, 16,
|
|
|
- _recordConfig.numChannels);
|
|
|
- }, onDone: () {
|
|
|
- _changeRecordStatus(RecordStatus.paused);
|
|
|
- }, onError: (error) {
|
|
|
- _changeRecordStatus(RecordStatus.paused);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- _onRecordPermissionDenied() {
|
|
|
- ToastUtil.showToast("需要授予录音权限才能使用录音功能");
|
|
|
- }
|
|
|
-
|
|
|
- Future<void> _stopRecord() {
|
|
|
- _releaseWakeLock();
|
|
|
- return _record
|
|
|
- .pause()
|
|
|
- .then((_) => _changeRecordStatus(RecordStatus.paused))
|
|
|
- .then((_) => FlutterForegroundTask.stopService());
|
|
|
- }
|
|
|
-
|
|
|
- Future<File> _getCurrentRecordFile() async {
|
|
|
- Directory documentDir = await getApplicationDocumentsDirectory();
|
|
|
- File file = File("${documentDir.path}/.atmob/record/$_lastRecordId");
|
|
|
- if (!file.existsSync()) {
|
|
|
- file.createSync(recursive: true);
|
|
|
- }
|
|
|
- return file;
|
|
|
- }
|
|
|
-
|
|
|
- double _getPcmDuration(
|
|
|
- int fileSize, int sampleRate, int bitDepth, int channels) {
|
|
|
- final bytesPerSecond = sampleRate * (bitDepth / 8) * channels;
|
|
|
- final durationInSeconds = fileSize / bytesPerSecond;
|
|
|
- return durationInSeconds;
|
|
|
- }
|
|
|
-
|
|
|
- Future<void> _deleteCurrentRecord() async {
|
|
|
- await _stopRecord();
|
|
|
- File file = await _getCurrentRecordFile();
|
|
|
- if (file.existsSync()) {
|
|
|
- file.deleteSync();
|
|
|
- }
|
|
|
- KVUtil.putString(keyLastRecordId, "");
|
|
|
- currentDuration.value = 0;
|
|
|
- _changeRecordStatus(RecordStatus.pending);
|
|
|
+ recordHandler.startOrContinueRecord();
|
|
|
}
|
|
|
|
|
|
Future<void> _saveCurrentRecord() async {
|
|
|
@@ -253,21 +117,12 @@ class RecordController extends BaseController {
|
|
|
ToastUtil.showToast("录音时长不足$minRecordDuration秒");
|
|
|
return;
|
|
|
}
|
|
|
- await _stopRecord();
|
|
|
+ await recordHandler.stopRecord();
|
|
|
talkRepository
|
|
|
- .talkCreate(_lastRecordId, currentDuration.value.toInt())
|
|
|
+ .talkCreate(recordHandler.lastRecordId, currentDuration.value.toInt())
|
|
|
.then((talkInfo) async {
|
|
|
- File pcmFile = await _getCurrentRecordFile();
|
|
|
- if (pcmFile.existsSync()) {
|
|
|
- File wavFile = await getRecordFile(talkInfo.id);
|
|
|
- PcmWavConverter.convert(pcmFile, wavFile, _recordConfig.sampleRate,
|
|
|
- _recordConfig.numChannels, 16);
|
|
|
- pcmFile.delete();
|
|
|
- KVUtil.putString(keyLastRecordId, "");
|
|
|
- _dealSuccessNextStep(talkInfo);
|
|
|
- } else {
|
|
|
- throw Exception("pcm file not found");
|
|
|
- }
|
|
|
+ await recordHandler.getConvertWavFile(talkInfo.id);
|
|
|
+ _dealSuccessNextStep(talkInfo);
|
|
|
}).catchError((error) {
|
|
|
if (error is ServerErrorException) {
|
|
|
if (error.code == ErrorCode.errorCodeNoLogin) {
|
|
|
@@ -296,68 +151,5 @@ class RecordController extends BaseController {
|
|
|
TalkPage.start(talkInfo, eventTag: EventId.id_001);
|
|
|
}
|
|
|
|
|
|
- void _changeRecordStatus(RecordStatus status) {
|
|
|
- currentStatus.value = status;
|
|
|
- }
|
|
|
|
|
|
- Future<void> _requestForegroundTaskPermission() async {
|
|
|
- final NotificationPermission notificationPermission =
|
|
|
- await FlutterForegroundTask.checkNotificationPermission();
|
|
|
- if (notificationPermission != NotificationPermission.granted) {
|
|
|
- await FlutterForegroundTask.requestNotificationPermission();
|
|
|
- }
|
|
|
-
|
|
|
- if (Platform.isAndroid) {
|
|
|
- if (!await FlutterForegroundTask.isIgnoringBatteryOptimizations) {
|
|
|
- // This function requires `android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` permission.
|
|
|
- await FlutterForegroundTask.requestIgnoreBatteryOptimization();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Future<ServiceRequestResult> _startForegroundService() async {
|
|
|
- if (await FlutterForegroundTask.isRunningService) {
|
|
|
- return FlutterForegroundTask.restartService();
|
|
|
- } else {
|
|
|
- return FlutterForegroundTask.startService(
|
|
|
- serviceId: 256,
|
|
|
- notificationTitle: StringName.appName.tr,
|
|
|
- notificationText: StringName.recordStatusRecording.tr,
|
|
|
- notificationIcon: null,
|
|
|
- notificationButtons: [],
|
|
|
- callback: setRecordCallback,
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- void _setWakeLock() {
|
|
|
- WakelockPlus.enable();
|
|
|
- }
|
|
|
-
|
|
|
- void _releaseWakeLock() {
|
|
|
- WakelockPlus.disable();
|
|
|
- }
|
|
|
-
|
|
|
- /// 获取录音文件地址
|
|
|
- static Future<File> getRecordFile(String talkId) async {
|
|
|
- Directory documentDir = await getApplicationDocumentsDirectory();
|
|
|
- return File("${documentDir.path}/.atmob/record/$talkId.wav");
|
|
|
- }
|
|
|
-
|
|
|
- /// 判断是否有未上传的录音
|
|
|
- static Future<bool> hasUnUploadRecord() async {
|
|
|
- String? lastRecordId = KVUtil.getString(keyLastRecordId, null);
|
|
|
- if (lastRecordId == null || lastRecordId.isEmpty) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- Directory documentDir = await getApplicationDocumentsDirectory();
|
|
|
- File file = File("${documentDir.path}/.atmob/record/$lastRecordId");
|
|
|
- return await file.exists() && await file.length() > 0;
|
|
|
- }
|
|
|
-
|
|
|
- canPop() async {
|
|
|
- if (currentStatus.value == RecordStatus.recording) {
|
|
|
- return _stopRecord();
|
|
|
- }
|
|
|
- }
|
|
|
}
|