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

问题描述 投票:41回答:9

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

// 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#>)`
ios swift avfoundation avaudiosession
9个回答
51
投票

HerderTöne的评论向您展示了新语法,但您还需要在setCategory之后激活音频会话:

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

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

UPDATE:

问题在Xcode 10.2中得到修复。


18
投票

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

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

11
投票

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

然后在<#target_name#>-Bridging-Header.h中导入“AVAudioSession + Swift.h”

#import "AVAudioSession+Swift.h"

结果是你可以像以前一样在swift中调用方法。

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

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


2
投票

将其粘贴到您的viewDidLoad()

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

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

0
投票

斯威夫特5

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

-1
投票

代码为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)
    }

希望有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.