Skip to content

Commit 31ec6b0

Browse files
authored
Fix audio visualizer exception (#1008)
Fix `MissingPluginException` when stopping audio visualizer.
1 parent e9beaed commit 31ec6b0

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
patch type="fixed" "MissingPluginException when stopping audio visualizer on Android/iOS"

lib/src/track/audio_visualizer_native.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ class AudioVisualizerNative extends AudioVisualizer {
5353
return;
5454
}
5555

56-
await Native.stopVisualizer(mediaStreamTrack.id!, visualizerId: visualizerId);
57-
56+
// Cancel subscription before native stop, otherwise the native
57+
// StreamHandler is already removed and cancel throws MissingPluginException.
5858
await _streamSubscription?.cancel();
5959
_streamSubscription = null;
6060
_eventChannel = null;
61+
62+
await Native.stopVisualizer(mediaStreamTrack.id!, visualizerId: visualizerId);
6163
}
6264
}
6365

shared_swift/LiveKitPlugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public class LiveKitPlugin: NSObject, FlutterPlugin {
201201
}
202202

203203
for processors in audioProcessors.values {
204+
processors.visualizers[visualizerId]?.stop()
204205
processors.visualizers.removeValue(forKey: visualizerId)
205206
}
206207

shared_swift/Visualizer.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ public class Visualizer: NSObject, RTCAudioRenderer, FlutterStreamHandler {
6969
channel?.setStreamHandler(self)
7070
}
7171

72-
deinit {
72+
public func stop() {
7373
_track?.remove(audioRenderer: self)
74+
channel?.setStreamHandler(nil)
75+
}
76+
77+
deinit {
78+
stop()
7479
}
7580

7681
public func render(pcmBuffer: AVAudioPCMBuffer) {

0 commit comments

Comments
 (0)