瀏覽代碼

[new]跟换录音插件

zk 1 年之前
父節點
當前提交
48fcad32e2
共有 5 個文件被更改,包括 178 次插入99 次删除
  1. 2 0
      android/app/src/main/AndroidManifest.xml
  2. 1 1
      lib/data/consts/constants.dart
  3. 59 17
      lib/module/record/record_handler.dart
  4. 112 80
      pubspec.lock
  5. 4 1
      pubspec.yaml

+ 2 - 0
android/app/src/main/AndroidManifest.xml

@@ -1,4 +1,5 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
     package="com.atmob.elec_asst">
 
     <uses-permission android:name="android.permission.INTERNET" />
@@ -30,6 +31,7 @@
     <application
         android:name=".MyApplication"
         android:allowBackup="false"
+        tools:replace="android:allowBackup, android:icon, android:theme"
         android:enableOnBackInvokedCallback="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"

+ 1 - 1
lib/data/consts/constants.dart

@@ -6,7 +6,7 @@ import '../../utils/common_utils.dart';
 class Constants {
   Constants._();
 
-  static const String env = envProd;
+  static const String env = envTest;
 
   static const String envDev = 'dev';
 

+ 59 - 17
lib/module/record/record_handler.dart

@@ -5,11 +5,13 @@ import 'package:custom_notification/custom_notification.dart';
 import 'package:electronic_assistant/module/record/record_task.dart';
 import 'package:electronic_assistant/router/app_pages.dart';
 import 'package:flutter/cupertino.dart';
+import 'package:flutter_sound/flutter_sound.dart';
+import 'package:flutter_sound/public/flutter_sound_recorder.dart';
 import 'package:get/get.dart';
 import 'package:path_provider/path_provider.dart';
 import 'package:flutter_foreground_task/flutter_foreground_task.dart';
 import 'package:get/get_rx/src/rx_types/rx_types.dart';
-import 'package:record/record.dart';
+import 'package:permission_handler/permission_handler.dart';
 import 'package:uuid/uuid.dart';
 import 'package:wakelock_plus/wakelock_plus.dart';
 import '../../data/bean/talks.dart';
@@ -37,14 +39,17 @@ class RecordHandler {
 
   final Rx<RecordStatus> currentStatus = RecordStatus.pending.obs;
   final RxDouble currentDuration = 0.0.obs;
-  final AudioRecorder _record = AudioRecorder();
+
+  final FlutterSoundRecorder _soundPlayer = FlutterSoundRecorder();
+  StreamController<Uint8List>? recordingDataController;
+
   final RecordConfig _recordConfig = RecordConfig(
-    encoder: AudioEncoder.pcm16bits,
-    bitRate: 16000,
+    codec: Codec.pcm16,
     sampleRate: SampleRate.rate44_1k.value,
     numChannels: Channel.mono.value,
   );
-
+  StreamSubscription? _recorderSubscription;
+  bool? _isSoundInited;
   String? _lastRecordId;
 
   final int _serviceId = 256;
@@ -148,8 +153,8 @@ class RecordHandler {
 
   Future<void> stopRecord({bool? isStopService}) async {
     _releaseWakeLock();
-    if (await _record.isRecording()) {
-      await _record.stop();
+    if (_soundPlayer.isRecording) {
+      await _soundPlayer.pauseRecorder();
     }
     _changeRecordStatus(RecordStatus.paused);
     if (isStopService == true) {
@@ -206,8 +211,8 @@ class RecordHandler {
   }
 
   Future<void> startOrContinueRecord() async {
-    bool hasPermission = await _record.hasPermission();
-    if (!hasPermission) {
+    var status = await Permission.microphone.request();
+    if (status != PermissionStatus.granted) {
       _onRecordPermissionDenied();
       return;
     }
@@ -215,15 +220,12 @@ class RecordHandler {
     await _requestForegroundTaskPermission().catchError((error) {
       debugPrint("requestForegroundTaskPermission error: $error");
     });
-
+    recordingDataController = StreamController<Uint8List>();
     File targetFile = await _getCurrentRecordFile();
-    Stream<Uint8List> recordStream = await _record.startStream(_recordConfig);
-    _setWakeLock();
-    _startForegroundService();
-    if (currentStatus.value != RecordStatus.recording) {
-      _changeRecordStatus(RecordStatus.recording);
+    if (_recorderSubscription != null) {
+      _recorderSubscription?.cancel();
     }
-    recordStream.listen((data) async {
+    _recorderSubscription = recordingDataController!.stream.listen((data) {
       if (data.isEmpty) {
         return;
       }
@@ -236,6 +238,18 @@ class RecordHandler {
     }, onError: (error) {
       _changeRecordStatus(RecordStatus.paused);
     });
+    await _soundPlayer.openRecorder();
+    _isSoundInited = true;
+    await _soundPlayer.startRecorder(
+        toStream: recordingDataController!.sink,
+        codec: _recordConfig.codec,
+        numChannels: _recordConfig.numChannels,
+        sampleRate: _recordConfig.sampleRate);
+    _setWakeLock();
+    _startForegroundService();
+    if (currentStatus.value != RecordStatus.recording) {
+      _changeRecordStatus(RecordStatus.recording);
+    }
   }
 
   Future<void> _requestForegroundTaskPermission() async {
@@ -290,7 +304,24 @@ class RecordHandler {
     return await file.exists() && await file.length() > 0;
   }
 
-  void onClose() async {}
+  void onClose() async {
+    if (currentStatus.value != RecordStatus.recording) {
+      releaseSoundRecorder();
+      _cancelRecorderSubscriptions();
+    }
+  }
+
+  void releaseSoundRecorder() {
+    if (_isSoundInited == true) {
+      _soundPlayer.closeRecorder();
+      _isSoundInited = false;
+    }
+  }
+
+  void _cancelRecorderSubscriptions() {
+    recordingDataController = null;
+    _recorderSubscription?.cancel();
+  }
 
   Future<void> getConvertWavFile(String talkId) async {
     File pcmFile = await _getCurrentRecordFile();
@@ -326,4 +357,15 @@ class RecordHandler {
   }
 }
 
+class RecordConfig {
+  Codec codec;
+  int numChannels;
+  int sampleRate;
+
+  RecordConfig(
+      {required this.codec,
+      required this.numChannels,
+      required this.sampleRate});
+}
+
 final recordHandler = RecordHandler._();

+ 112 - 80
pubspec.lock

@@ -461,10 +461,10 @@ packages:
     dependency: "direct main"
     description:
       name: flutter_foreground_task
-      sha256: d8496f95257df0688406b9bc3ff606297b78dde1145cff3d6e6d97cd0c54e023
+      sha256: daf2e0a6dd863aeab84055c9671f4fb1449205771dcba01f4d635e3d0dfa8b3e
       url: "https://pub.dev"
     source: hosted
-    version: "8.10.4"
+    version: "8.12.0"
   flutter_gen_core:
     dependency: transitive
     description:
@@ -501,10 +501,10 @@ packages:
     dependency: "direct main"
     description:
       name: flutter_local_notifications
-      sha256: "49eeef364fddb71515bc78d5a8c51435a68bccd6e4d68e25a942c5e47761ae71"
+      sha256: "674173fd3c9eda9d4c8528da2ce0ea69f161577495a9cc835a2a4ecd7eadeb35"
       url: "https://pub.dev"
     source: hosted
-    version: "17.2.3"
+    version: "17.2.4"
   flutter_local_notifications_linux:
     dependency: transitive
     description:
@@ -545,6 +545,30 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "4.9.8+3"
+  flutter_sound:
+    dependency: "direct main"
+    description:
+      name: flutter_sound
+      sha256: "31f9b2058a152520774f98147adb9f5df3b2e9ee0d5b6a7f4f07bec9feeca4e3"
+      url: "https://pub.dev"
+    source: hosted
+    version: "9.16.3"
+  flutter_sound_platform_interface:
+    dependency: transitive
+    description:
+      name: flutter_sound_platform_interface
+      sha256: "60ce97b065ca12161d501575dfcd49ec0764545a5e9ff9576dc96eb19f189e76"
+      url: "https://pub.dev"
+    source: hosted
+    version: "9.16.3"
+  flutter_sound_web:
+    dependency: transitive
+    description:
+      name: flutter_sound_web
+      sha256: c557aebe181ce1d2261a4cb9e3bb8eada18af30745791a7241e8d00152c5fa06
+      url: "https://pub.dev"
+    source: hosted
+    version: "9.16.3"
   flutter_svg:
     dependency: transitive
     description:
@@ -749,10 +773,10 @@ packages:
     dependency: transitive
     description:
       name: in_app_purchase_android
-      sha256: "6bbb526f2a405d9fe6556bb33e63fbeaff13fde3e42207b19aa5300eca9a1de6"
+      sha256: "81507c7cacc6fc45e7980f887d53566b2847ac02859d0156f171f5056c2dd1b7"
       url: "https://pub.dev"
     source: hosted
-    version: "0.3.6+10"
+    version: "0.3.6+11"
   in_app_purchase_platform_interface:
     dependency: transitive
     description:
@@ -857,6 +881,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "3.0.0"
+  logger:
+    dependency: transitive
+    description:
+      name: logger
+      sha256: "697d067c60c20999686a0add96cf6aba723b3aa1f83ecf806a8097231529ec32"
+      url: "https://pub.dev"
+    source: hosted
+    version: "2.4.0"
   logging:
     dependency: transitive
     description:
@@ -925,42 +957,42 @@ packages:
     dependency: "direct main"
     description:
       name: mmkv
-      sha256: feaf601749734a36cbfb840df45f9ece6e0d3b619959a5f63f002feca0518c1a
+      sha256: "2761606d02b44ce481e87efd7405343687f74761d2be916f2b76296a868e3154"
       url: "https://pub.dev"
     source: hosted
-    version: "1.3.9"
+    version: "1.3.10"
   mmkv_android:
     dependency: transitive
     description:
       name: mmkv_android
-      sha256: ab0921f7d766d85c031b584e48ec89f94019f34b99786dd755f2430ce1a4fdc2
+      sha256: "721540c18d8858020fd1cb763efe81621026b2a434da0da50a4810ac19a5ab31"
       url: "https://pub.dev"
     source: hosted
-    version: "1.0.6"
+    version: "1.0.7"
   mmkv_ios:
     dependency: transitive
     description:
       name: mmkv_ios
-      sha256: f28a4f80703d294365d41367502ff43d895e8aa23d4cf882692dd0096c146e32
+      sha256: d303510b1d5584fac531f271b2229cfbdd06dcb068b1e6f3c2863ac7c031715e
       url: "https://pub.dev"
     source: hosted
-    version: "1.0.7"
+    version: "1.0.8"
   mmkv_ohos:
     dependency: transitive
     description:
       name: mmkv_ohos
-      sha256: b6c82843fbec967b2dacc985fbdd407df375251f1941a34541201ad38761b26f
+      sha256: e6d29df9ee25841f1669549d83fd8f86ad705bce8bacb83188d7e54296506c8f
       url: "https://pub.dev"
     source: hosted
-    version: "1.0.2"
+    version: "1.0.3"
   mmkv_platform_interface:
     dependency: transitive
     description:
       name: mmkv_platform_interface
-      sha256: b5e060f1cfe590b9b22960e6580861e6f1e0798ccc8c5a5891358e3dea814eed
+      sha256: "7e120ee1d8cfbbeb054a49e7ed6b889e362208815766f64c1c9c190e2ce15047"
       url: "https://pub.dev"
     source: hosted
-    version: "1.0.2"
+    version: "1.0.3"
   nested:
     dependency: transitive
     description:
@@ -1021,10 +1053,10 @@ packages:
     dependency: transitive
     description:
       name: path_parsing
-      sha256: e3e67b1629e6f7e8100b367d3db6ba6af4b1f0bb80f64db18ef1fbabd2fa9ccf
+      sha256: "45f7d6bba1128761de5540f39d5ca000ea8a1f22f06b76b61094a60a2997bd0e"
       url: "https://pub.dev"
     source: hosted
-    version: "1.0.1"
+    version: "1.0.2"
   path_provider:
     dependency: transitive
     description:
@@ -1073,6 +1105,54 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "2.3.0"
+  permission_handler:
+    dependency: "direct main"
+    description:
+      name: permission_handler
+      sha256: "18bf33f7fefbd812f37e72091a15575e72d5318854877e0e4035a24ac1113ecb"
+      url: "https://pub.dev"
+    source: hosted
+    version: "11.3.1"
+  permission_handler_android:
+    dependency: transitive
+    description:
+      name: permission_handler_android
+      sha256: "71bbecfee799e65aff7c744761a57e817e73b738fedf62ab7afd5593da21f9f1"
+      url: "https://pub.dev"
+    source: hosted
+    version: "12.0.13"
+  permission_handler_apple:
+    dependency: transitive
+    description:
+      name: permission_handler_apple
+      sha256: e6f6d73b12438ef13e648c4ae56bd106ec60d17e90a59c4545db6781229082a0
+      url: "https://pub.dev"
+    source: hosted
+    version: "9.4.5"
+  permission_handler_html:
+    dependency: transitive
+    description:
+      name: permission_handler_html
+      sha256: af26edbbb1f2674af65a8f4b56e1a6f526156bc273d0e65dd8075fab51c78851
+      url: "https://pub.dev"
+    source: hosted
+    version: "0.1.3+2"
+  permission_handler_platform_interface:
+    dependency: transitive
+    description:
+      name: permission_handler_platform_interface
+      sha256: e9c8eadee926c4532d0305dff94b85bf961f16759c3af791486613152af4b4f9
+      url: "https://pub.dev"
+    source: hosted
+    version: "4.2.3"
+  permission_handler_windows:
+    dependency: transitive
+    description:
+      name: permission_handler_windows
+      sha256: "1a790728016f79a41216d88672dbc5df30e686e811ad4e698bfc51f76ad91f1e"
+      url: "https://pub.dev"
+    source: hosted
+    version: "0.2.1"
   petitparser:
     dependency: transitive
     description:
@@ -1153,62 +1233,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "2.0.0"
-  record:
-    dependency: "direct main"
-    description:
-      name: record
-      sha256: "4a5cf4d083d1ee49e0878823c4397d073f8eb0a775f31215d388e2bc47a9e867"
-      url: "https://pub.dev"
-    source: hosted
-    version: "5.1.2"
-  record_android:
-    dependency: transitive
-    description:
-      name: record_android
-      sha256: d7af0b3119725a0f561817c72b5f5eca4d7a76d441deef519ae04e4824c0734c
-      url: "https://pub.dev"
-    source: hosted
-    version: "1.2.6"
-  record_darwin:
+  recase:
     dependency: transitive
     description:
-      name: record_darwin
-      sha256: fe90d302acb1f3cee1ade5df9c150ca5cee33b48d8cdf1cf433bf577d7f00134
+      name: recase
+      sha256: e4eb4ec2dcdee52dcf99cb4ceabaffc631d7424ee55e56f280bc039737f89213
       url: "https://pub.dev"
     source: hosted
-    version: "1.1.2"
-  record_linux:
-    dependency: transitive
-    description:
-      name: record_linux
-      sha256: "74d41a9ebb1eb498a38e9a813dd524e8f0b4fdd627270bda9756f437b110a3e3"
-      url: "https://pub.dev"
-    source: hosted
-    version: "0.7.2"
-  record_platform_interface:
-    dependency: transitive
-    description:
-      name: record_platform_interface
-      sha256: "11f8b03ea8a0e279b0e306571dbe0db0202c0b8e866495c9fa1ad2281d5e4c15"
-      url: "https://pub.dev"
-    source: hosted
-    version: "1.1.0"
-  record_web:
-    dependency: transitive
-    description:
-      name: record_web
-      sha256: "656b7a865f90651fab997c2a563364f5fd60a0b527d5dadbb915d62d84fc3867"
-      url: "https://pub.dev"
-    source: hosted
-    version: "1.1.3"
-  record_windows:
-    dependency: transitive
-    description:
-      name: record_windows
-      sha256: e653555aa3fda168aded7c34e11bd82baf0c6ac84e7624553def3c77ffefd36f
-      url: "https://pub.dev"
-    source: hosted
-    version: "1.0.3"
+    version: "4.1.0"
   retrofit:
     dependency: "direct main"
     description:
@@ -1237,10 +1269,10 @@ packages:
     dependency: "direct main"
     description:
       name: share_plus
-      sha256: "334fcdf0ef9c0df0e3b428faebcac9568f35c747d59831474b2fc56e156d244e"
+      sha256: "3af2cda1752e5c24f2fc04b6083b40f013ffe84fb90472f30c6499a9213d5442"
       url: "https://pub.dev"
     source: hosted
-    version: "10.1.0"
+    version: "10.1.1"
   share_plus_platform_interface:
     dependency: transitive
     description:
@@ -1520,10 +1552,10 @@ packages:
     dependency: transitive
     description:
       name: url_launcher_android
-      sha256: "8fc3bae0b68c02c47c5c86fa8bfa74471d42687b0eded01b78de87872db745e2"
+      sha256: "0dea215895a4d254401730ca0ba8204b29109a34a99fb06ae559a2b60988d2de"
       url: "https://pub.dev"
     source: hosted
-    version: "6.3.12"
+    version: "6.3.13"
   url_launcher_ios:
     dependency: transitive
     description:
@@ -1624,10 +1656,10 @@ packages:
     dependency: transitive
     description:
       name: video_player_android
-      sha256: "2800d68d6d5b4c22da62453568ed68e63c35bea524d4fa42062e53d6bb591433"
+      sha256: "391e092ba4abe2f93b3e625bd6b6a6ec7d7414279462c1c0ee42b5ab8d0a0898"
       url: "https://pub.dev"
     source: hosted
-    version: "2.7.13"
+    version: "2.7.16"
   video_player_avfoundation:
     dependency: transitive
     description:
@@ -1648,10 +1680,10 @@ packages:
     dependency: transitive
     description:
       name: video_player_web
-      sha256: "6dcdd298136523eaf7dfc31abaf0dfba9aa8a8dbc96670e87e9d42b6f2caf774"
+      sha256: "881b375a934d8ebf868c7fb1423b2bfaa393a0a265fa3f733079a86536064a10"
       url: "https://pub.dev"
     source: hosted
-    version: "2.3.2"
+    version: "2.3.3"
   vm_service:
     dependency: transitive
     description:
@@ -1752,10 +1784,10 @@ packages:
     dependency: transitive
     description:
       name: win32
-      sha256: e1d0cc62e65dc2561f5071fcbccecf58ff20c344f8f3dc7d4922df372a11df1f
+      sha256: "10169d3934549017f0ae278ccb07f828f9d6ea21573bab0fb77b0e1ef0fce454"
       url: "https://pub.dev"
     source: hosted
-    version: "5.7.1"
+    version: "5.7.2"
   win32_registry:
     dependency: transitive
     description:

+ 4 - 1
pubspec.yaml

@@ -63,7 +63,10 @@ dependencies:
   archive: ^3.6.1
 
   #录音
-  record: ^5.1.2
+  flutter_sound: ^9.16.3
+
+  #权限申请
+  permission_handler: ^11.3.1
 
   #uuid
   uuid: ^4.5.0