AVFoundation音频不能停止静音

时间:2016-12-04 06:43:59

标签: ios swift xcode avfoundation

嘿伙计们出于某种原因,当你将手机静音时,音频仍然来自应用程序。关于如何解决的任何想法?

private func myFunc() {
    // Set up the audio session
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        fatalError("Unable to set the audio session because: \(error)")
    }


    guard
        let audioURL = Bundle.main.url(forResource: "lololol", withExtension: "mp3"),
        let player = try? AVAudioPlayer(contentsOf: audioURL)

        else { fatalError("Unable to create player") }

    if let aPlayer = audioPlayer {

        if audioPlayer?.isPlaying == false {
            audioPlayer = player

            player.play()
        }

    } else {
            audioPlayer = player
            player.play()
    }


}

1 个答案:

答案 0 :(得分:1)

我通过将AVAudioSessionCategoryPlayback更改为AVAudioSessionCategoryAmbient来解决此问题。