代码之家  ›  专栏  ›  技术社区  ›  Nitesh

avaudiosesession setcategory swift 4.2 iOS 12-静音播放声音

  •  43
  • Nitesh  · 技术社区  · 7 年前

    要在静音模式下播放声音,我使用下面的方法。但它是如何不起作用的。

    // Works on Swift 3  
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    } catch {
        print(error)
    }
    

    如何让它在4.2/iOS 12中工作?

    在较新的版本中,我们需要设置模式和选项。

    try AVAudioSession.sharedInstance().setCategory(
        <#T##category:AVAudioSession.Category##AVAudioSession.Category#>,
        mode: <#T##AVAudioSession.Mode#>, 
        options: <#T##AVAudioSession.CategoryOptions#>)`
    
    9 回复  |  直到 6 年前
        1
  •  51
  •   Gordon Childs    7 年前

    她的评论向您展示了新的语法,但您还需要在 setCategory 以下内容:

    do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error)
    }
    
        2
  •  37
  •   ricardopereira    6 年前

    作为一个解决方法,你可以称之为目标-C AVAudioSession.setCategory: 方法 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)
    }
    

    如果要为iOS 9及更早版本设置类别和选项,请使用:

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

    更新:

    问题在Xcode 10.2中修复。

        3
  •  18
  •   Chewie The Chorkie    7 年前

    对于iOS 12中的Swift 4.2,它只是:

    try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [])
    
        4
  •  11
  •   Galvin    6 年前

    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.
    

    您可以使用Objective-C类别来帮助解决此问题。

    创建 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
    

    用一个 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
    

    然后将“avaudiosesession+swift.h”导入 <#target_name#>-Bridging-Header.h

    #import "AVAudioSession+Swift.h"
    

    结果是您可以像以前一样迅速调用该方法。

    do {
        try AVAudioSession.sharedInstance().setCategory(.playback)
        try AVAudioSession.sharedInstance().setCategory(.playback, options: [.mixWithOthers])
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error)
    }
    
        5
  •  6
  •   Cœur Gustavo Armenta    6 年前

    如果您的应用程序与iOS 9或更低版本不兼容,上述答案(由节奏拳手提供)是正确的。

    如果您的应用程序与iOS 9兼容,您将看到下一个错误:

    “setCategory”在swift中不可用

    在这种情况下,Swift 4.2有一个bug,这是Xcode10的sdks中的avfoundation的一个问题,您可以通过编写一个调用旧API的objective-c函数来解决这个问题,因为它们仍然在objective-c中可用。

    在下一个链接中,您可以阅读更多详细信息:

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

        6
  •  2
  •   karel Aadil Minhaz    6 年前

    将此粘贴到 viewDidLoad()

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: [])
        try AVAudioSession.sharedInstance().setActive(true)
    }
    catch {
        print(error)
    }
    
        7
  •  0
  •   Piyush Sharma    6 年前
    //Swift 4.2
    if #available(iOS 10.0, *) {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, 
        mode: AVAudioSessionModeDefault)
    } else {
     //Fallback on earlier versions
    }
    
        8
  •  0
  •   budiDino    6 年前

    斯威夫特5

    let audioSession = AVAudioSession.sharedInstance()
    _ = try? audioSession.setCategory(.playback, options: .defaultToSpeaker)
    _ = try? audioSession.setActive(true)
    
        9
  •  -1
  •   Sasan Soroush    6 年前

    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)
        }
    

    希望有帮助