pixiv / webrtc

This is a fork of WebRTC made by pixiv Inc.
https://github.com/pixiv/webrtc/blob/branch-heads/pixiv-m78/README.pixiv.md
BSD 3-Clause "New" or "Revised" License
112 stars 35 forks source link

How can I enable loudspeaker on iOS? (Unity) #20

Closed camnewnham closed 4 years ago

camnewnham commented 4 years ago

Thanks for writing this library, it was a great start to getting running in Unity!

I'm having trouble getting the audio to play through the speaker on iOS and am hoping someone can offer some advice. For android this technique from sebjf worked but I was unable to create a similar function on iOS.

(Note I am not using the broadcast extension or anything - the code is mostly based on the Sora/PeerConnection example - I can provide more if relevant)

What I have tried

  1. Setting the configuration in Unity: image

  2. Calling this function either before or after initialization of the RTC module and audio track:

#import <AVFoundation/AVFoundation.h>

void iOSAudio_setupAudioSession()
{
    AVAudioSession *session = [AVAudioSession sharedInstance];

    NSError *setCategoryError = nil;
    if (![session setCategory:AVAudioSessionCategoryPlayback
                  withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
                        error:&setCategoryError]) {
        // handle error
        NSLog(@"Audio already playing through speaker!");
    }
    else{
        NSLog(@"Forcing audio to speaker");
    }
}
  1. Calling this function either before or after initialization of the RTC module and audio track:
#import <AVFoundation/AVFoundation.h>

void iOSAudio_setupAudioSession()
{
    AVAudioSession*session =[AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord
                    mode:AVAudioSessionModeVideoChat
                 options:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetooth
                    error:nil];

    [session setActive:true error:nil];
    [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
}

None of these seem to affect how audio is played back, and it is always coming out of the earpiece rather than the loudspeaker.

Is there any ideas as to why this might not work?

Cheers Cam

akihikodaki commented 4 years ago

Use RTCAudioSessionConfiguration. This project does not provide a binding for the class (yet), so write a generic binding or a function specific to your use case by yourself just like you did as iOSAudio_setupAudioSession. If you wrote a generic binding, we would like to appreciate a pull request for it.