AVAudioSession setCategory Swift 4.2 iOS 12-静音播放声音

时间:2018-06-24 13:29:48

标签: ios swift avfoundation avaudiosession

即使在静音模式下也要播放声音,我使用以下方法。但是它不起作用。

// 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 个答案:

答案 0 :(得分:50)

Her derTöne的注释向您显示了新语法,但是您还需要在setCategory之后激活音频会话:

do {
    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
    try AVAudioSession.sharedInstance().setActive(true)
} catch {
    print(error)
}

答案 1 :(得分:37)

作为一种解决方法,您可以使用AVAudioSession.setCategory:调用Objective-C 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中修复。

答案 2 :(得分:18)

对于iOS 12中的Swift 4.2,它很简单:

try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [])

答案 3 :(得分:10)

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

然后将“ AVAudioSession + 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)
}

答案 4 :(得分:6)

如果您的应用与iOS 9或更低版本不兼容,则上述答案(由Rhythmic Fistman提出)是正确的。

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

  

'setCategory'在Swift中不可用

在这种情况下,Swift 4.2会出现错误,这是Xcode 10 SDK中AVFoundation的问题,您可以通过编写调用旧API的Objective-C函数来解决该问题,因为它们仍然在Objective-C中可用。

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

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

答案 5 :(得分:2)

将此内容粘贴到您的viewDidLoad()

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: [])
    try AVAudioSession.sharedInstance().setActive(true)
}
catch {
    print(error)
}

答案 6 :(得分:0)

//Swift 4.2
if #available(iOS 10.0, *) {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, 
    mode: AVAudioSessionModeDefault)
} else {
 //Fallback on earlier versions
}

答案 7 :(得分:0)

雨燕5

let audioSession = AVAudioSession.sharedInstance()
_ = try? audioSession.setCategory(.playback, options: .defaultToSpeaker)
_ = try? audioSession.setActive(true)

答案 8 :(得分:-1)

快捷键4的代码:

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


    } catch {
        print(error)
    }

希望有帮助

相关问题