dart-sip-ua icon indicating copy to clipboard operation
dart-sip-ua copied to clipboard

Toggle Speaker

Open ifightcode opened this issue 3 years ago • 9 comments

How can I toggle speaker? I mean switch between loudspeaker and top speaker. Despite of voiceonly true or false, my phones always using loudspeakers.

ifightcode avatar Jul 25 '21 15:07 ifightcode

Same issue! Toggle speaker not working. @ifightcode have you find any solution?

RaviGaruda avatar Sep 03 '21 07:09 RaviGaruda

Same issue! Toggle speaker not working. @ifightcode have you find any solution?

No. I had to abandon the project due to numerous issues.

ifightcode avatar Sep 03 '21 07:09 ifightcode

okay, so have you found any other sip/voip client for flutter?

RaviGaruda avatar Sep 03 '21 08:09 RaviGaruda

I'm using Flutter as a module in our native iOS and Android apps (which I assume a lot of people who are using this are doing since they have to interface with CallKit for iOS). I was able to solve this by just talking to native directly via MethodChannel's. I've only done Android at this point but will do it in iOS in a week or two, they both work similarly as far as audio goes, you just get the apps current audio session (which flutter is also using) and you can mess with it. When I start the call it starts the audio session just fine. From that point forward I can use a method channel to tell native to set the audio output to whatever I want it to be. Here's what the native Android side looks like: flutterPhoneChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() { @Override public void onMethodCall(MethodCall call, MethodChannel.Result result) { if (call.method.equals("toggleSpeaker")) { boolean speakerOn = ((Map<String, Boolean>) call.arguments).get("speakerOn"); AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); am.setMode(AudioManager.MODE_IN_CALL); am.setSpeakerphoneOn(speakerOn); } } });

If someone wants the native solution for iOS tag me if it's been a week or so since this post and I can share my solution from that side. Would definitely be nice if controlling the audio this way was part of this library though since you can't really release a VOIP solution without those basic controls. Alternatively there may be a Flutter package out there that allows you to mess with the audio sessions from dart. I didn't look into it since my apps are already going back and forth from Flutter and native, making this a quick solution for me.

turtlepigg avatar Sep 10 '21 18:09 turtlepigg

  void _handelStreams(CallState event) async {
    MediaStream stream = event.stream;
    if (event.originator == 'local') {
      if (_localRenderer != null) {
        _localRenderer.srcObject = stream;
      }
      if (!kIsWeb) {
        event.stream?.getAudioTracks()?.first?.enableSpeakerphone(false);
      }
      _localStream = stream;
    }
    if (event.originator == 'remote') {
      if (_remoteRenderer != null) {
        _remoteRenderer.srcObject = stream;
      }
      _remoteStream = stream;
    }

    this.setState(() {
      _resizeLocalVideo();
    });
  }

This can help you

zi6xuan avatar Sep 21 '21 13:09 zi6xuan

I'm using Flutter as a module in our native iOS and Android apps (which I assume a lot of people who are using this are doing since they have to interface with CallKit for iOS). I was able to solve this by just talking to native directly via MethodChannel's. I've only done Android at this point but will do it in iOS in a week or two, they both work similarly as far as audio goes, you just get the apps current audio session (which flutter is also using) and you can mess with it. When I start the call it starts the audio session just fine. From that point forward I can use a method channel to tell native to set the audio output to whatever I want it to be. Here's what the native Android side looks like: flutterPhoneChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() { @Override public void onMethodCall(MethodCall call, MethodChannel.Result result) { if (call.method.equals("toggleSpeaker")) { boolean speakerOn = ((Map<String, Boolean>) call.arguments).get("speakerOn"); AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); am.setMode(AudioManager.MODE_IN_CALL); am.setSpeakerphoneOn(speakerOn); } } });

If someone wants the native solution for iOS tag me if it's been a week or so since this post and I can share my solution from that side. Would definitely be nice if controlling the audio this way was part of this library though since you can't really release a VOIP solution without those basic controls. Alternatively there may be a Flutter package out there that allows you to mess with the audio sessions from dart. I didn't look into it since my apps are already going back and forth from Flutter and native, making this a quick solution for me.

I have been trying to achieve the solution through native code using Swift. So far I am unable to achieve it, can you share the code that worked on iOS device switching from Speaker to Receiver?

Although my native code print statement shows that the output port is Receiver but still the audio is attached to speaker somehow.

ShahzadUmarBaig avatar Dec 25 '21 00:12 ShahzadUmarBaig