@@ -127,6 +127,11 @@ private FlutterWebRTCPlugin(Registrar registrar, MethodChannel channel) {
127127 .setVideoDecoderFactory (new DefaultVideoDecoderFactory (eglContext ))
128128 .setAudioDeviceModule (audioDeviceModule )
129129 .createPeerConnectionFactory ();
130+ }
131+
132+ private void startAudioManager () {
133+ if (rtcAudioManager != null )
134+ return ;
130135
131136 rtcAudioManager = RTCAudioManager .create (registrar .context ());
132137 // Store existing audio settings and change audio mode to
@@ -141,12 +146,14 @@ public void onAudioDeviceChanged(
141146 onAudioManagerDevicesChanged (audioDevice , availableAudioDevices );
142147 }
143148 });
144- /*
145- if (audioManager != null) {
146- audioManager.stop();
147- audioManager = null;
149+ }
150+
151+ private void stopAudioManager () {
152+ if (rtcAudioManager != null ) {
153+ Log .d (TAG , "Stoping the audio manager..." );
154+ rtcAudioManager .stop ();
155+ rtcAudioManager = null ;
148156 }
149- */
150157 }
151158
152159 // This method is called when the audio manager reports audio device change,
@@ -297,7 +304,7 @@ public void onMethodCall(MethodCall call, Result notSafeResult) {
297304 result .success (null );
298305 } else if (call .method .equals ("peerConnectionDispose" )){
299306 String peerConnectionId = call .argument ("peerConnectionId" );
300- peerConnectionClose (peerConnectionId );
307+ peerConnectionDispose (peerConnectionId );
301308 result .success (null );
302309 }else if (call .method .equals ("createVideoRenderer" )) {
303310 TextureRegistry .SurfaceTextureEntry entry = textures .createSurfaceTexture ();
@@ -362,6 +369,9 @@ public void onMethodCall(MethodCall call, Result notSafeResult) {
362369 result .success (null );
363370 } else if (call .method .equals ("enableSpeakerphone" )) {
364371 boolean enable = call .argument ("enable" );
372+ if (rtcAudioManager == null ){
373+ startAudioManager ();
374+ }
365375 rtcAudioManager .setSpeakerphoneOn (enable );
366376 result .success (null );
367377 } else if (call .method .equals ("getDisplayMedia" )) {
@@ -723,6 +733,9 @@ public String peerConnectionInit(
723733 parseMediaConstraints (constraints ),
724734 observer );
725735 observer .setPeerConnection (peerConnection );
736+ if (mPeerConnectionObservers .size () == 0 ) {
737+ startAudioManager ();
738+ }
726739 mPeerConnectionObservers .put (peerConnectionId , observer );
727740 return peerConnectionId ;
728741 }
@@ -1285,6 +1298,9 @@ public void peerConnectionDispose(final String id) {
12851298 pco .dispose ();
12861299 mPeerConnectionObservers .remove (id );
12871300 }
1301+ if (mPeerConnectionObservers .size () == 0 ) {
1302+ stopAudioManager ();
1303+ }
12881304 }
12891305
12901306 public void mediaStreamRelease (final String id ) {
0 commit comments