AVAudioSession setCategory Swift 4.2 iOS 12 - Play Sound on Silent

前端 未结 9 1943
醉酒成梦
醉酒成梦 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:22

    As a workaround, you can call the Objective-C AVAudioSession.setCategory: method with the NSObject.performSelector::

    if #available(iOS 10.0, *) {
        try! AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
    }
    else {
        // Workaround until https://forums.swift.org/t/using-methods-marked-unavailable-in-swift-4-2/14949 isn't fixed
        AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playback)
    }
    

    If you want to set the category and options for iOS 9 and earlier then use:

    AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:withOptions:error:"), with: AVAudioSession.Category.playback, with:  [AVAudioSession.CategoryOptions.duckOthers])
    

    UPDATE:

    Issue is fixed in Xcode 10.2.

提交回复
热议问题