web-dev-qa-db-ja.com

AVAudioSession setCategory Swift 4.2 iOS 12-サイレントでサウンドを再生

サイレントモードでもサウンドを再生するには、以下の方法を使用します。しかし、それがどのように機能しないのか。

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

Her derTöneのコメントは新しい構文を示していますが、setCategoryの後にオーディオセッションをアクティブにする必要もあります。

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

回避策として、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では問題が修正されています。

37
ricardopereira

IOS 12のSwift 4.2の場合は、単純に次のとおりです。

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

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

上記の回答(Rhythmic Fistmanによる)は、アプリがiOS 9以下と互換性がない場合に正しいです。

アプリがiOS 9と互換性がある場合、次のエラーが表示されます。

「setCategory」はSwiftでは使用できません

その場合、Swift 4.2にバグがあります。これはXcode 10のSDKのAVFoundationの問題です。Objectiveで使用可能なため、古いAPIを呼び出すObjective-C関数を記述することで回避できます。 -C。

次のリンクで詳細を読むことができます:

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

6

これをviewDidLoad()に貼り付けます

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

スイフト5

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

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

役立つことを願っています

0
micheal woods