From 3fd2d9e2e1eb556651e42db197d38e6a665c5ad6 Mon Sep 17 00:00:00 2001 From: Ankit Srivastava Date: Wed, 6 Mar 2024 23:33:31 +0530 Subject: [PATCH] auto stop voice --- example/lib/main.dart | 81 ++++++++++- example/pubspec.lock | 312 ++++++++++++++++++++++++++++++++++++++---- example/pubspec.yaml | 4 +- 3 files changed, 366 insertions(+), 31 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index e2a37ac..a92dd88 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -10,11 +10,16 @@ // import 'dart:async'; +import 'dart:io'; import 'dart:math'; + import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_sound/flutter_sound.dart'; import 'package:flutter_voice_processor/flutter_voice_processor.dart'; import 'package:flutter_voice_processor_example/vu_meter_painter.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:record/record.dart'; void main() { runApp(MyApp()); @@ -37,13 +42,23 @@ class _MyAppState extends State { bool _isProcessing = false; String? _errorMessage; VoiceProcessor? _voiceProcessor; + final record = AudioRecorder(); @override void initState() { super.initState(); _initVoiceProcessor(); + _initRecorder(); + } + + @override + void dispose() { + record.dispose(); + super.dispose(); } + void _initRecorder() async {} + void _initVoiceProcessor() async { _voiceProcessor = VoiceProcessor.instance; } @@ -62,6 +77,19 @@ class _MyAppState extends State { setState(() { _isProcessing = isRecording!; }); + final directory = await getApplicationDocumentsDirectory(); + final path = directory.path; + final file = File('$path/file.wav'); + // Start recording with flutter_sound + await record.start( + RecordConfig( + encoder: AudioEncoder.wav, + sampleRate: sampleRate, + bitRate: 96000, + numChannels: 1, + ), + path: file.path, + ); } else { setState(() { _errorMessage = "Recording permission not granted"; @@ -85,6 +113,8 @@ class _MyAppState extends State { try { await _voiceProcessor?.stop(); + // Stop recording with flutter_sound + await record.stop(); } on PlatformException catch (ex) { setState(() { _errorMessage = "Failed to stop recorder: " + ex.toString(); @@ -118,8 +148,12 @@ class _MyAppState extends State { return normalizedValue.clamp(0.0, 1.0); } +// Add a timer variable at the class level + Timer? _volumeCheckTimer; + void _onFrame(List frame) { double volumeLevel = _calculateVolumeLevel(frame); + print(volumeLevel); if (_volumeHistory.length == volumeHistoryCapacity) { _volumeHistory.removeAt(0); } @@ -129,6 +163,19 @@ class _MyAppState extends State { _smoothedVolumeValue = _volumeHistory.reduce((a, b) => a + b) / _volumeHistory.length; }); + + // Check if volume is below the threshold + if (volumeLevel < 0.2) { + // If the timer is not running, start it + _volumeCheckTimer ??= Timer(Duration(seconds: 2), () { + // If the timer completes, stop the recording + _stopProcessing(); + }); + } else { + // If the volume is above the threshold, cancel the timer + _volumeCheckTimer?.cancel(); + _volumeCheckTimer = null; + } } void _onError(VoiceProcessorException error) { @@ -137,6 +184,29 @@ class _MyAppState extends State { }); } + Future _playAudio() async { + try { + // Assuming you're saving the file in the app's documents directory + final directory = await getApplicationDocumentsDirectory(); + final path = directory.path; + final file = File('$path/file.wav'); + + if (await file.exists()) { + final _recorder = FlutterSound(); + await _recorder.thePlayer.openPlayer(); + await _recorder.thePlayer.startPlayer(fromURI: file.path); + } else { + setState(() { + _errorMessage = "No audio file found to play."; + }); + } + } on PlatformException catch (ex) { + setState(() { + _errorMessage = "Failed to play audio: " + ex.toString(); + }); + } + } + Color picoBlue = Color.fromRGBO(55, 125, 255, 1); @override @@ -149,12 +219,21 @@ class _MyAppState extends State { body: Column(children: [ buildVuMeter(context), buildStartButton(context), + buildPlayButton(context), // Add this line + buildErrorMessage(context) ]), ), ); } + buildPlayButton(BuildContext context) { + return ElevatedButton( + onPressed: _isProcessing || _errorMessage != null ? null : _playAudio, + child: Text("Play", style: TextStyle(fontSize: 20)), + ); + } + buildVuMeter(BuildContext context) { return Expanded( flex: 2, @@ -171,7 +250,7 @@ class _MyAppState extends State { buildStartButton(BuildContext context) { final ButtonStyle buttonStyle = ElevatedButton.styleFrom( - primary: picoBlue, + backgroundColor: picoBlue, shape: CircleBorder(), textStyle: TextStyle(color: Colors.white)); diff --git a/example/pubspec.lock b/example/pubspec.lock index 5d3d83d..cfc81d0 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -41,6 +41,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.18.0" + crypto: + dependency: transitive + description: + name: crypto + sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab + url: "https://pub.dev" + source: hosted + version: "3.0.3" fake_async: dependency: transitive description: @@ -49,14 +57,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.1" + ffi: + dependency: transitive + description: + name: ffi + sha256: "493f37e7df1804778ff3a53bd691d8692ddf69702cf4c1c1096a2e41b4779e21" + url: "https://pub.dev" + source: hosted + version: "2.1.2" file: dependency: transitive description: name: file - sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d" + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" url: "https://pub.dev" source: hosted - version: "6.1.4" + version: "7.0.0" flutter: dependency: "direct main" description: flutter @@ -67,6 +83,30 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_sound: + dependency: "direct main" + description: + name: flutter_sound + sha256: "090a4694b11ecc744c2010621c4ffc5fe7c3079d304ea014961a72c7b72cfe6c" + url: "https://pub.dev" + source: hosted + version: "9.2.13" + flutter_sound_platform_interface: + dependency: transitive + description: + name: flutter_sound_platform_interface + sha256: "4537eaeb58a32748c42b621ad6116f7f4c6ee0a8d6ffaa501b165fe1c9df4753" + url: "https://pub.dev" + source: hosted + version: "9.2.13" + flutter_sound_web: + dependency: transitive + description: + name: flutter_sound_web + sha256: ad4ca92671a1879e1f613e900bbbdb8170b20d57d1e4e6363018fe56b055594f + url: "https://pub.dev" + source: hosted + version: "9.2.13" flutter_test: dependency: "direct dev" description: flutter @@ -75,10 +115,16 @@ packages: flutter_voice_processor: dependency: "direct main" description: - path: ".." - relative: true - source: path + name: flutter_voice_processor + sha256: "3c91d8ab34b33016643ae7586a305b81c47c1b66a95b489807c34f127537fcbc" + url: "https://pub.dev" + source: hosted version: "1.1.1" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" fuchsia_remote_debug_protocol: dependency: transitive description: flutter @@ -89,6 +135,38 @@ packages: description: flutter source: sdk version: "0.0.0" + js: + dependency: transitive + description: + name: js + sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 + url: "https://pub.dev" + source: hosted + version: "0.6.7" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa" + url: "https://pub.dev" + source: hosted + version: "10.0.0" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0 + url: "https://pub.dev" + source: hosted + version: "2.0.1" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47 + url: "https://pub.dev" + source: hosted + version: "2.0.1" lints: dependency: "direct dev" description: @@ -97,54 +175,198 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.1" + logger: + dependency: transitive + description: + name: logger + sha256: "7ad7215c15420a102ec687bb320a7312afd449bac63bfb1c60d9787c27b9767f" + url: "https://pub.dev" + source: hosted + version: "1.4.0" matcher: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e + sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.11.0" + nested: + dependency: transitive + description: + name: nested + sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20" + url: "https://pub.dev" + source: hosted + version: "1.0.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" + path_provider: + dependency: "direct main" + description: + name: path_provider + sha256: b27217933eeeba8ff24845c34003b003b2b22151de3c908d0e679e8fe1aa078b + url: "https://pub.dev" + source: hosted + version: "2.1.2" + path_provider_android: + dependency: transitive + description: + name: path_provider_android + sha256: "477184d672607c0a3bf68fbbf601805f92ef79c82b64b4d6eb318cbca4c48668" + url: "https://pub.dev" + source: hosted + version: "2.2.2" + path_provider_foundation: + dependency: transitive + description: + name: path_provider_foundation + sha256: "5a7999be66e000916500be4f15a3633ebceb8302719b47b9cc49ce924125350f" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279 + url: "https://pub.dev" + source: hosted + version: "2.2.1" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + sha256: "8bc9f22eee8690981c22aa7fc602f5c85b497a6fb2ceb35ee5a5e5ed85ad8170" + url: "https://pub.dev" + source: hosted + version: "2.2.1" platform: dependency: transitive description: name: platform - sha256: ae68c7bfcd7383af3629daafb32fb4e8681c7154428da4febcff06200585f102 + sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec" + url: "https://pub.dev" + source: hosted + version: "3.1.4" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" url: "https://pub.dev" source: hosted - version: "3.1.2" + version: "2.1.8" process: dependency: transitive description: name: process - sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09" + sha256: "21e54fd2faf1b5bdd5102afd25012184a6793927648ea81eea80552ac9405b32" url: "https://pub.dev" source: hosted - version: "4.2.4" + version: "5.0.2" + provider: + dependency: transitive + description: + name: provider + sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c + url: "https://pub.dev" + source: hosted + version: "6.1.2" + recase: + dependency: transitive + description: + name: recase + sha256: e4eb4ec2dcdee52dcf99cb4ceabaffc631d7424ee55e56f280bc039737f89213 + url: "https://pub.dev" + source: hosted + version: "4.1.0" + record: + dependency: "direct main" + description: + name: record + sha256: "5c8e12c692a4800b33f5f8b6c821ea083b12bfdbd031b36ba9322c40a4eeecc9" + url: "https://pub.dev" + source: hosted + version: "5.0.4" + record_android: + dependency: transitive + description: + name: record_android + sha256: "805ecaa232a671aff2ee9ec4730ef6addb97c548d2db6b1fbd5197f1d4f47a5a" + url: "https://pub.dev" + source: hosted + version: "1.0.4" + record_darwin: + dependency: transitive + description: + name: record_darwin + sha256: ee8cb1bb1712d7ce38140ecabe70e5c286c02f05296d66043bee865ace7eb1b9 + url: "https://pub.dev" + source: hosted + version: "1.0.1" + record_linux: + dependency: transitive + description: + name: record_linux + sha256: "7d0e70cd51635128fe9d37d89bafd6011d7cbba9af8dc323079ae60f23546aef" + url: "https://pub.dev" + source: hosted + version: "0.7.1" + record_platform_interface: + dependency: transitive + description: + name: record_platform_interface + sha256: "3a4b56e94ecd2a0b2b43eb1fa6f94c5b8484334f5d38ef43959c4bf97fb374cf" + url: "https://pub.dev" + source: hosted + version: "1.0.2" + record_web: + dependency: transitive + description: + name: record_web + sha256: "24847cdbcf999f7a5762170792f622ac844858766becd0f2370ec8ae22f7526e" + url: "https://pub.dev" + source: hosted + version: "1.0.5" + record_windows: + dependency: transitive + description: + name: record_windows + sha256: "39998b3ea7d8d28b04159d82220e6e5e32a7c357c6fb2794f5736beea272f6c3" + url: "https://pub.dev" + source: hosted + version: "1.0.2" sky_engine: dependency: transitive description: flutter @@ -190,6 +412,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.3.1" + synchronized: + dependency: transitive + description: + name: synchronized + sha256: "539ef412b170d65ecdafd780f924e5be3f60032a1128df156adad6c5b373d558" + url: "https://pub.dev" + source: hosted + version: "3.1.0+1" term_glyph: dependency: transitive description: @@ -206,6 +436,22 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.1" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + url: "https://pub.dev" + source: hosted + version: "1.3.2" + uuid: + dependency: transitive + description: + name: uuid + sha256: "648e103079f7c64a36dc7d39369cabb358d377078a051d6ae2ad3aa539519313" + url: "https://pub.dev" + source: hosted + version: "3.0.7" vector_math: dependency: transitive description: @@ -218,26 +464,34 @@ packages: dependency: transitive description: name: vm_service - sha256: c538be99af830f478718b51630ec1b6bee5e74e52c8a802d328d9e71d35d2583 + sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957 url: "https://pub.dev" source: hosted - version: "11.10.0" - web: + version: "13.0.0" + webdriver: dependency: transitive description: - name: web - sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152 + name: webdriver + sha256: "003d7da9519e1e5f329422b36c4dcdf18d7d2978d1ba099ea4e45ba490ed845e" url: "https://pub.dev" source: hosted - version: "0.3.0" - webdriver: + version: "3.0.3" + win32: dependency: transitive description: - name: webdriver - sha256: "3c923e918918feeb90c4c9fdf1fe39220fa4c0e8e2c0fffaded174498ef86c49" + name: win32 + sha256: "464f5674532865248444b4c3daca12bd9bf2d7c47f759ce2617986e7229494a8" + url: "https://pub.dev" + source: hosted + version: "5.2.0" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + sha256: faea9dee56b520b55a566385b84f2e8de55e7496104adada9962e0bd11bcff1d url: "https://pub.dev" source: hosted - version: "3.0.2" + version: "1.0.4" sdks: - dart: ">=3.2.0-194.0.dev <4.0.0" - flutter: ">=1.20.0" + dart: ">=3.3.0-279.1.beta <4.0.0" + flutter: ">=3.10.0" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index d59ac49..e9ce049 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -10,8 +10,10 @@ dependencies: flutter: sdk: flutter + flutter_sound: ^9.2.13 flutter_voice_processor: - path: ../ + path_provider: ^2.1.2 + record: ^5.0.4 dev_dependencies: integration_test: