Laerdal / Xamarin.AzureCommunicationCalling

Xamarin iOS and Android binding libraries for Microsofts Azure Communication Services
MIT License
35 stars 11 forks source link

Play sound through loud speaker #14

Closed Immons closed 3 years ago

Immons commented 3 years ago

Hello,

Thanks for binding libs. Have you maybe figured out how to play sound through loud speaker? Tried to iterate through SpeakerList, but there is only one, default and it plays through phone speaker.

Best regards

tompi commented 3 years ago

Hi @Immons , you need to do this per platform. I do it when call state changes to "Connected". On Android:

private void SetSpeakerOutput()
  {
      _logger.Debug($"Trying to route audio volume to speaker and set volume to max");
      var audioManager = (AudioManager)MainActivity.Instance.GetSystemService(Android.Content.Context.AudioService);
      if (audioManager == null) throw new Exception("No audio manager");
      audioManager.Mode = Mode.InCall;
      audioManager.SpeakerphoneOn = true;
      audioManager.SetStreamVolume(Stream.VoiceCall, audioManager.GetStreamMaxVolume(Stream.VoiceCall), VolumeNotificationFlags.ShowUi);            
}

On iOS:

private void SetAudioToSpeaker()
{
    try
    {
        _logger.Debug($"Trying to route audio volume to speaker and set volume to max");
        var error = AVFoundation.AVAudioSession.SharedInstance()
            .SetCategory(AVFoundation.AVAudioSessionCategory.PlayAndRecord,
                AVFoundation.AVAudioSessionCategoryOptions.DefaultToSpeaker);
        ThrowIfError(error);
        AVFoundation.AVAudioSession.SharedInstance()
            .SetMode(AVFoundation.AVAudioSession.ModeVoiceChat, out error);
        ThrowIfError(error);
        AVFoundation.AVAudioSession.SharedInstance()
            .OverrideOutputAudioPort(AVFoundation.AVAudioSessionPortOverride.Speaker, out error);
        ThrowIfError(error);
        error = AVFoundation.AVAudioSession.SharedInstance().SetActive(true);
        ThrowIfError(error);
        _logger.Debug($"Finished audio setup");
    }
    catch (Exception e)
    {
        _logger.Error("audio_setup_failed", e);
    }
}
Immons commented 3 years ago

Thanks, that works well.