簡體   English   中英

在 Swift 中將 AVAudioPlayer 輸出更改為揚聲器?

[英]Change AVAudioPlayer output to speaker in Swift?

我的 iphone 應用程序中有簡單的AVAudioPlayer ,它可以工作,但輸出應該是電話揚聲器而不是耳機。 我在這里找到了類似的主題,但不知道如何將其“翻譯”為 swift ( AudioSessionSetProperty讓我感到困惑)?

var audioPlayer = AVAudioPlayer(data: fileData, error: &playbackError) 
//fileData is url to file

if let player = audioPlayer{
    player.delegate = self

    if(player.prepareToPlay() && player.play()){
         println("Started playing the recorded audio")
    } else {
         println("Could not play the audio")
    }
}

我可以理解這是多么令人困惑,因為我剛剛找到了答案。 所以AudioSessionSetProperty在 iOS 7 中被棄用了。

添加這個:

session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: nil)

確保首先調用AVAudioSession.sharedInstance()

在 Swift 3 或 4 中:

let audioSession = AVAudioSession.sharedInstance()

do {
    try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch let error as NSError {
    print("audioSession error: \(error.localizedDescription)")
}

為避免OSStatus錯誤 -50(user462990 在評論中提到),必須在設置會話類別后調用overrideOutputAudioPort (代碼如下)。

do {
    try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch let error as NSError {
    print("setCategory error: \(error.localizedDescription)")
}

試試這些功能。它們的工作很迷人。

func SetSessionPlayerOn()
{
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().setActive(true)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
    } catch _ {
    }
}
func SetSessionPlayerOff()
{
    do {
        try AVAudioSession.sharedInstance().setActive(false)
    } catch _ {
    }
}
  func SetEarSepeakerOn()
    {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.none)
        } catch _ {
        }
    }

迅速 5

    func setSessionPlayerOn() {
        do {
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {

        }
        do {
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
        } catch _ {

        }
    }

您可以在會話設置期間將音頻會話默認設置為內置揚聲器而不是接收器。

        do {
            // 1) Configure your audio session category, options, and mode
            try session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVoiceChat, options: [.defaultToSpeaker])
            // 2) Activate your audio session to enable your custom configuration
            try session.setActive(true)
        } catch let error as NSError {
            print("Failed to set the audio session category and mode: \(error.localizedDescription)")
        }

斯威夫特 5

所有這些答案要么是過時的,要么不是永久的解決方案。

如文檔所述, If you'd prefer to permanently enable this behavior, you should instead set the category's defaultToSpeaker option ,如下所示:

try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker])

Setting this option routes to the speaker rather than the receiver if no other accessory such as headphones are in use.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM