Um den Ton auch im Silent-Modus wiederzugeben, verwende ich die unten beschriebene Methode. Aber wie funktioniert es nicht?.
// Works on Swift 3
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
print(error)
}
Wie bekomme ich es in 4.2/iOS 12?
In der neueren Version müssen Modus und Optionen eingestellt werden.
try AVAudioSession.sharedInstance().setCategory(
<#T##category:AVAudioSession.Category##AVAudioSession.Category#>,
mode: <#T##AVAudioSession.Mode#>,
options: <#T##AVAudioSession.CategoryOptions#>)`
Ihr Kommentar des Tönes zeigt Ihnen die neue Syntax, aber Sie müssen die Audiositzung auch nach setCategory
aktivieren:
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
Als Workaround können Sie die Objective-C AVAudioSession.setCategory:
-Methode mit dem NSObject.performSelector:
aufrufen:
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)
}
Wenn Sie die Kategorie und Optionen für iOS 9 und früher festlegen möchten, verwenden Sie Folgendes:
AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:withOptions:error:"), with: AVAudioSession.Category.playback, with: [AVAudioSession.CategoryOptions.duckOthers])
Für Swift 4.2 in iOS 12 ist es einfach:
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [])
Sie können eine Zielkategorie verwenden, um dieses Problem zu beheben.
Erstellen Sie einen 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
Mit einem 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
Dann importieren Sie "AVAudioSession + Swift.h" in Ihren <#target_name#>-Bridging-Header.h
#import "AVAudioSession+Swift.h"
Das Ergebnis ist, dass Sie die Methode in Swift wie zuvor aufrufen können.
do {
try AVAudioSession.sharedInstance().setCategory(.playback)
try AVAudioSession.sharedInstance().setCategory(.playback, options: [.mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
Die obige Antwort (von Rhythmic Fistman) ist korrekt, wenn Ihre App nicht mit iOS 9 oder niedriger kompatibel ist.
Wenn Ihre App mit iOS 9 kompatibel ist, wird der nächste Fehler angezeigt:
'setCategory' ist in Swift nicht verfügbar
In diesem Fall gibt es einen Fehler mit Swift 4.2. Dies ist ein Problem mit AVFoundation in den SDKs von Xcode 10. Sie können dies umgehen, indem Sie eine Objective-C-Funktion schreiben, die die alte API aufruft, da sie in Objective noch verfügbar sind -C.
Im nächsten Link können Sie weitere Details lesen:
https://forums.Swift.org/t/using-methods-marked-unavailable-in-Swift-4-2/14949
Fügen Sie dies in Ihr viewDidLoad()
ein
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
}
Swift 5
let audioSession = AVAudioSession.sharedInstance()
_ = try? audioSession.setCategory(.playback, options: .defaultToSpeaker)
_ = try? audioSession.setActive(true)
Code für 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)
}
Hoffentlich hilft das