AVAudioSession setCategory Swift 4.2 iOS 12 - Play Sound on Silent

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

    For Swift 4.2 in iOS 12, it is simply:

    try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [])
    
    0 讨论(0)
  • 2020-12-13 06:16

    Update for Xcode 10.2:

    Apple finally fix this issue in Xcode 10.2. 
    So no need to add these workaround code anymore if you use Xcode 10.2 or newer version.
    
    But you also could refer this code for any problem like this.
    

    You can use an objective-c category to help to fix this issue.

    Create a AVAudioSession+Swift.h:

    @import AVFoundation;
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface AVAudioSession (Swift)
    
    - (BOOL)swift_setCategory:(AVAudioSessionCategory)category error:(NSError **)outError NS_SWIFT_NAME(setCategory(_:));
    - (BOOL)swift_setCategory:(AVAudioSessionCategory)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError NS_SWIFT_NAME(setCategory(_:options:));
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    With a AVAudioSession+Swift.m:

    #import "AVAudioSession+Swift.h"
    
    @implementation AVAudioSession (Swift)
    
    - (BOOL)swift_setCategory:(AVAudioSessionCategory)category error:(NSError **)outError {
        return [self setCategory:category error:outError];
    }
    - (BOOL)swift_setCategory:(AVAudioSessionCategory)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError {
        return [self setCategory:category withOptions:options error:outError];
    }
    
    @end
    

    Then import the "AVAudioSession+Swift.h" in your <#target_name#>-Bridging-Header.h

    #import "AVAudioSession+Swift.h"
    

    Result is you can call the method in swift like before.

    do {
        try AVAudioSession.sharedInstance().setCategory(.playback)
        try AVAudioSession.sharedInstance().setCategory(.playback, options: [.mixWithOthers])
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error)
    }
    
    0 讨论(0)
  • 2020-12-13 06:18

    Code for swift 4 :

    do {
            try AKSettings.setSession(category: .playback, with: [])
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: [])
            try AVAudioSession.sharedInstance().setActive(true)
    
    
        } catch {
            print(error)
        }
    

    Hope that helps

    0 讨论(0)
  • 2020-12-13 06:20

    The above answer (by Rhythmic Fistman) is correct if your app is not compatible with iOS 9 or below.

    If your app is compatible with iOS 9 you will see the next error:

    'setCategory' is unavailable in Swift

    In that case there is a bug with Swift 4.2, this is an issue with AVFoundation in Xcode 10's SDKs, you can work around it by writing an Objective-C function that calls through to the old API, since they're still available in Objective-C.

    In the next link you can read more details:

    https://forums.swift.org/t/using-methods-marked-unavailable-in-swift-4-2/14949

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 06:22

    Swift 5

    let audioSession = AVAudioSession.sharedInstance()
    _ = try? audioSession.setCategory(.playback, options: .defaultToSpeaker)
    _ = try? audioSession.setActive(true)
    
    0 讨论(0)
提交回复
热议问题