Reputation: 2354
I want to play audio while the phone is in silent mode.
I can do this in >= iOS 10 with the following statement:
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: .mixWithOthers)
But how do I do this for iOS 9? Xcode tells me that this statement is only available in iOS 10 or newer.
I tried the following but it is not working:
Upvotes: 1
Views: 1954
Reputation: 47886
In fact, the method setCategory
is rapidly changing in recent Xcodes & SDKs.
You need to find the right combination.
For Xcode 10.2 (Swift Language Version 4.2, 5)
if #available(iOS 10.0, *) {
//Xcode 10.2 (Swift Language Version 4.2,5)
//Xcode 10.1 (Swift Language Version 4.2)
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .mixWithOthers)
} else {
//Xcode 10.2 (Swift Language Version 4.2,5)
try AVAudioSession.sharedInstance().setCategory(.playback, options: .mixWithOthers)
}
For Xcode 10.2 (Swift Language Version 4.0) or Xcode 10.1 (Swift Language Version 4.0)
if #available(iOS 10.0, *) {
//Xcode 10.2 (Swift Language Version 4.0)
//Xcode 10.1 (Swift Language Version 4.0)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: .mixWithOthers)
} else {
//Xcode 10.2 (Swift Language Version 4.0)
//Xcode 10.1 (Swift Language Version 4.0)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
}
(Unfortunately, I could not have found a way to call setCategory
for iOS 9 with Xcode 10.1 (Swift Language Version 4.2).)
Anyway, please re-check your Swift Language Version setting:
Upvotes: 1