簡體   English   中英

AVAudioSession setCategory Swift 4.2 iOS 12 - 靜音播放聲音

[英]AVAudioSession setCategory Swift 4.2 iOS 12 - Play Sound on Silent

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

// 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#>)`

HerderTöne的評論顯示了新語法,但您還需要在setCategory之后激活音頻會話:

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

作為解決方法,您可以使用NSObject.performSelector:調用Objective-C AVAudioSession.setCategory:方法:

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中得到修復。

對於iOS 12中的Swift 4.2,它只是:

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

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” <#target_name#>-Bridging-Header.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)
}

如果您的應用與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

將其粘貼到viewDidLoad()

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

斯威夫特5

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

代碼為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)
    }

希望有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM