AVAudioSession setCategory Swift 4.2 iOS 12 - Play Sound on Silent

前端 未结 9 1941
醉酒成梦
醉酒成梦 2020-12-13 06:06

To play sound even on Silent mode I use to use below method. But how it\'s not working.

// Works on Swift 3  
do {
    try AVAudioSession.sharedInstance().se         


        
相关标签:
9条回答
  • 2020-12-13 06:32

    Her der Töne's comment shows you the new syntax, but you also need to activate the audio session after setCategory:

    do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error)
    }
    
    0 讨论(0)
  • 2020-12-13 06:33

    Paste this into your viewDidLoad()

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: [])
        try AVAudioSession.sharedInstance().setActive(true)
    }
    catch {
        print(error)
    }
    
    0 讨论(0)
  • 2020-12-13 06:37
    //Swift 4.2
    if #available(iOS 10.0, *) {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, 
        mode: AVAudioSessionModeDefault)
    } else {
     //Fallback on earlier versions
    }
    
    0 讨论(0)
提交回复
热议问题