@@ -33,10 +33,9 @@ class SensorRecorderProvider with ChangeNotifier {
3333 bool _isBLEMicrophoneStreamingEnabled = false ;
3434 bool get isBLEMicrophoneStreamingEnabled => _isBLEMicrophoneStreamingEnabled;
3535
36- // Separate AudioRecorder for streaming
37- AudioRecorder ? _streamingAudioRecorder ;
36+ // Path for temporary streaming file
37+ String ? _streamingPath ;
3838 bool _isStreamingActive = false ;
39- StreamSubscription <Amplitude >? _streamingAmplitudeSub;
4039
4140 Future <void > _selectBLEDevice () async {
4241 try {
@@ -85,17 +84,14 @@ class SensorRecorderProvider with ChangeNotifier {
8584 return false ;
8685 }
8786
88- _streamingAudioRecorder = AudioRecorder ();
89-
9087 const encoder = AudioEncoder .wav;
91- if (! await _streamingAudioRecorder ! .isEncoderSupported (encoder)) {
88+ if (! await _audioRecorder .isEncoderSupported (encoder)) {
9289 logger.w ("WAV encoder not supported" );
93- _streamingAudioRecorder = null ;
9490 return false ;
9591 }
9692
9793 final tempDir = await getTemporaryDirectory ();
98- final tempPath =
94+ _streamingPath =
9995 '${tempDir .path }/ble_stream_${DateTime .now ().millisecondsSinceEpoch }.wav' ;
10096
10197 final config = RecordConfig (
@@ -106,13 +102,13 @@ class SensorRecorderProvider with ChangeNotifier {
106102 device: _selectedBLEDevice,
107103 );
108104
109- await _streamingAudioRecorder ! .start (config, path: tempPath );
105+ await _audioRecorder .start (config, path: _streamingPath ! );
110106 _isStreamingActive = true ;
111107 _isBLEMicrophoneStreamingEnabled = true ;
112108
113109 // Set up amplitude monitoring for waveform display
114- _streamingAmplitudeSub ? .cancel ();
115- _streamingAmplitudeSub = _streamingAudioRecorder !
110+ _amplitudeSub ? .cancel ();
111+ _amplitudeSub = _audioRecorder
116112 .onAmplitudeChanged (const Duration (milliseconds: 100 ))
117113 .listen ((amp) {
118114 final normalized = (amp.current + 50 ) / 50 ;
@@ -125,17 +121,6 @@ class SensorRecorderProvider with ChangeNotifier {
125121 notifyListeners ();
126122 });
127123
128- Future .delayed (const Duration (seconds: 1 ), () async {
129- try {
130- final file = File (tempPath);
131- if (await file.exists ()) {
132- await file.delete ();
133- }
134- } catch (e) {
135- // Ignore cleanup errors
136- }
137- });
138-
139124 logger.i (
140125 "BLE microphone streaming started with device: ${_selectedBLEDevice !.label }" ,
141126 );
@@ -145,8 +130,7 @@ class SensorRecorderProvider with ChangeNotifier {
145130 logger.e ("Failed to start BLE microphone streaming: $e " );
146131 _isStreamingActive = false ;
147132 _isBLEMicrophoneStreamingEnabled = false ;
148- _streamingAudioRecorder? .dispose ();
149- _streamingAudioRecorder = null ;
133+ _streamingPath = null ;
150134 notifyListeners ();
151135 return false ;
152136 }
@@ -158,15 +142,26 @@ class SensorRecorderProvider with ChangeNotifier {
158142 }
159143
160144 try {
161- await _streamingAudioRecorder? .stop ();
162- _streamingAmplitudeSub? .cancel ();
163- _streamingAmplitudeSub = null ;
164- _streamingAudioRecorder? .dispose ();
165- _streamingAudioRecorder = null ;
145+ await _audioRecorder.stop ();
146+ _amplitudeSub? .cancel ();
147+ _amplitudeSub = null ;
166148 _isStreamingActive = false ;
167149 _isBLEMicrophoneStreamingEnabled = false ;
168150 _waveformData.clear ();
169151
152+ // Clean up temporary streaming file
153+ if (_streamingPath != null ) {
154+ try {
155+ final file = File (_streamingPath! );
156+ if (await file.exists ()) {
157+ await file.delete ();
158+ }
159+ } catch (e) {
160+ // Ignore cleanup errors
161+ }
162+ _streamingPath = null ;
163+ }
164+
170165 logger.i ("BLE microphone streaming stopped" );
171166 notifyListeners ();
172167 } catch (e) {
@@ -202,10 +197,23 @@ class SensorRecorderProvider with ChangeNotifier {
202197
203198 // Stop streaming session before starting actual recording
204199 if (_isStreamingActive) {
205- await _streamingAudioRecorder ? .stop ();
206- _streamingAmplitudeSub ? .cancel ();
207- _streamingAmplitudeSub = null ;
200+ await _audioRecorder .stop ();
201+ _amplitudeSub ? .cancel ();
202+ _amplitudeSub = null ;
208203 _isStreamingActive = false ;
204+
205+ // Clean up temporary streaming file
206+ if (_streamingPath != null ) {
207+ try {
208+ final file = File (_streamingPath! );
209+ if (await file.exists ()) {
210+ await file.delete ();
211+ }
212+ } catch (e) {
213+ // Ignore cleanup errors
214+ }
215+ _streamingPath = null ;
216+ }
209217 }
210218
211219 try {
@@ -284,7 +292,6 @@ class SensorRecorderProvider with ChangeNotifier {
284292 _amplitudeSub? .cancel ();
285293 _amplitudeSub = null ;
286294 _isAudioRecording = false ;
287- _waveformData.clear ();
288295
289296 logger.i ("Audio recording saved to: $path " );
290297 _currentAudioPath = null ;
0 commit comments