triniwiz / nativescript-webrtc

Apache License 2.0
42 stars 25 forks source link

Audio Output #7

Closed JijeshP closed 5 years ago

JijeshP commented 5 years ago

@triniwiz How can we change the audio output from the earpiece speaker to the main speaker. ?

triniwiz commented 5 years ago

webrtc.speakerEnabled(true)

JijeshP commented 5 years ago

@triniwiz I tried using this.webrtc.speakerEnabled(true); but it's not working in iOS.

triniwiz commented 5 years ago

https://github.com/triniwiz/nativescript-webrtc/blob/master/src/webrtc.ios.ts#L435 is the code used guessing some error could be throwing but i'm not logging it you can try throwing in a log there

JijeshP commented 5 years ago

Logging below error NSErrorWrapper: Must call lockForConfiguration before calling this method.

triniwiz commented 5 years ago

Thanks for the update will publish a new fix soon

codingcronus commented 5 years ago

This fix did not resolve the issue for me (on iPhone XS / iOS 12), so I wrote this piece of code that fixed it:

    NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(
        AVAudioSessionRouteChangeNotification,
        null,
        NSOperationQueue.mainQueue,
        (notification: NSNotification) => {

            const interuptionDict = notification.userInfo;
            const routeChangeReason = interuptionDict.valueForKey(AVAudioSessionRouteChangeReasonKey);

            if (routeChangeReason == AVAudioSessionRouteChangeReason.CategoryChange) {

                const audioSession = AVAudioSession.sharedInstance();

                try {
                    const success = audioSession.overrideOutputAudioPortError(
                        AVAudioSessionPortOverride.Speaker
                    );

                    if (success == false) {
                        console.log('Could not overrideOutputAudioPortError.');
                    }
                } catch (error) {
                    console.log('Could not overrideOutputAudioPortError:', error);
                }
            }
        });

    const audioSession = ios.getter(
        RTCAudioSession,
        RTCAudioSession.sharedInstance
    );

    try {
        audioSession.setCategoryWithOptionsError(
            AVAudioSessionCategoryPlayAndRecord,
            AVAudioSessionCategoryOptions.DefaultToSpeaker
        );
    } catch (e) {
        console.log('MINSAMTALE: Could not toggle speaker:', e);
    }