Core Audio: Working with Speaker, Is it possible t

2019-07-04 17:02发布

问题:

As per the docs, Ther is no documentation about routing or even getting of the Port details for the "AVAudioSessionPortBuiltInReceiver". (Note: Please read again, its not about this port AVAudioSessionPortBuiltInSpeaker) .

As I found that and only possible overrideOutputAudioPort can be done only for

    public enum AVAudioSessionPortOverride : UInt {
    case None
    case Speaker
    }

The Question is, Is ther any possibilities to play an audio through the :

public let AVAudioSessionPortBuiltInReceiver: String /* The speaker you hold to your ear when on a phone call */

Edit: Using MPVolumeView class(RouteButton) is listing out(routing to) of the AVAudioSessionPortBuiltInReceiver possible, Is it possible to achieve the same?.

回答1:

If you configure the audio session for play-and-record (and leave mix-with-others off), the default route on an iPhone becomes to play audio out the smaller ear speaker (not the louder speaker on the bottom), or out the plugged-in headset, if the user plugs a headset in.



回答2:

Apple calls them "receiver" (i.e. top earpiece) and speaker.

to toggle between these two:

final private func changed(overrideNone: Bool) {

        let audioSession = AVAudioSession.sharedInstance()
        var override : AVAudioSessionPortOverride = .none

        if overrideNone{
                color = UIColor.red
                override = .none
            }
            else{
                color = UIColor.yellow
                override = .speaker
            }

            self.view.backgroundColor = color

            do {
                    try audioSession.overrideOutputAudioPort(override)
            } catch _ {

            }   
        }

If You call with false, You will play via override = .speaker

so lower speaker.

PS it does NOT work with iPads, as per june 2017 models..

Apple states:

https://developer.apple.com/documentation/avfoundation/avaudiosessionportbuiltinreceiver

AVAudioSessionPortBuiltInReceiver (earpiece..)

Output to a speaker intended to be held near the ear. .. Typically, this speaker is available only on iPhone devices.

(colors are for debug...)