AVAudioPlayer via Speakers

后端 未结 8 643
甜味超标
甜味超标 2021-02-06 13:16

I got the following code:

- (id)init {
    if (self = [super init]) {
        UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
        AudioSessionS         


        
8条回答
  •  生来不讨喜
    2021-02-06 13:39

    While the other answers do provide the desired effect of playing audio on the speaker instead of the receiver, they are not the idiomatic way to achieve that goal.

    One should rather use the AVAudioSessionCategoryOption (what a mouthful) DefaultToSpeaker. The documentation for that reads (emphasis mine):

    When using this option and no other audio route (such as a headset) is available, session audio will play through the device’s built-in speaker. When not using this option, and no other audio output is available or selected, audio will play through the receiver (a speaker intended to be held to the ear). Note that only iPhone devices are equipped with a receiver; on iPad and iPod touch devices, this option has no effect.

    To use this, change your call to setCategory: to pass the option, like this:

    let session = AVAudioSession.sharedInstance()
    try session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [.DefaultToSpeaker])
    

提交回复
热议问题