AVAudioSession setCategory Swift 4.2 iOS 12 - 静音播放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51010390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AVAudioSession setCategory Swift 4.2 iOS 12 - Play Sound on Silent
提问by Nitesh
To play sound even on Silent mode I use to use below method. But how it's not working.
即使在静音模式下播放声音,我也使用以下方法。但它如何不起作用。
// Works on Swift 3
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
print(error)
}
How to get it work in 4.2 / iOS 12?
如何让它在 4.2 / iOS 12 中工作?
In newer version we need to set mode and options.
在较新的版本中,我们需要设置模式和选项。
try AVAudioSession.sharedInstance().setCategory(
<#T##category:AVAudioSession.Category##AVAudioSession.Category#>,
mode: <#T##AVAudioSession.Mode#>,
options: <#T##AVAudioSession.CategoryOptions#>)`
回答by Rhythmic Fistman
Her der T?ne's comment shows you the new syntax, but you also need to activate the audio session after setCategory
:
Her der T?ne 的评论向您展示了新语法,但您还需要在以下之后激活音频会话setCategory
:
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
回答by ricardopereira
As a workaround, you can call the Objective-C AVAudioSession.setCategory:
method with the NSObject.performSelector:
:
作为一种解决方法,您可以使用以下命令调用 Objective-CAVAudioSession.setCategory:
方法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)
}
If you want to set the category and options for iOS 9 and earlier then use:
如果要为 iOS 9 及更早版本设置类别和选项,请使用:
AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:withOptions:error:"), with: AVAudioSession.Category.playback, with: [AVAudioSession.CategoryOptions.duckOthers])
UPDATE:
更新:
Issue is fixed in Xcode 10.2.
问题已在 Xcode 10.2 中修复。
回答by Chewie The Chorkie
For Swift 4.2 in iOS 12, it is simply:
对于 iOS 12 中的 Swift 4.2,它很简单:
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [])
回答by Galvin
Update for Xcode 10.2:
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.
You can use an objective-c category to help to fix this issue.
您可以使用 Objective-c 类别来帮助解决此问题。
Create a AVAudioSession+Swift.h
:
创建一个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
With a AVAudioSession+Swift.m
:
随着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
Then import the "AVAudioSession+Swift.h" in your <#target_name#>-Bridging-Header.h
然后在你的 <#target_name#>-Bridging-Header.h
#import "AVAudioSession+Swift.h"
Result is you can call the method in swift like before.
结果是您可以像以前一样快速调用该方法。
do {
try AVAudioSession.sharedInstance().setCategory(.playback)
try AVAudioSession.sharedInstance().setCategory(.playback, options: [.mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
回答by JMGM
The above answer (by Rhythmic Fistman) is correct if your app is not compatible with iOS 9 or below.
如果您的应用与 iOS 9 或更低版本不兼容,则上述答案(由 Rhythmic Fistman 提供)是正确的。
If your app is compatible with iOS 9 you will see the next error:
如果您的应用与 iOS 9 兼容,您将看到下一个错误:
'setCategory' is unavailable in Swift
“setCategory”在 Swift 中不可用
In that case there is a bug with Swift 4.2, this is an issue with AVFoundation in Xcode 10's SDKs, you can work around it by writing an Objective-C function that calls through to the old API, since they're still available in Objective-C.
在这种情况下,Swift 4.2 存在错误,这是 Xcode 10 的 SDK 中 AVFoundation 的问题,您可以通过编写一个调用旧 API 的 Objective-C 函数来解决它,因为它们在 Objective 中仍然可用-C。
In the next link you can read more details:
在下一个链接中,您可以阅读更多详细信息:
https://forums.swift.org/t/using-methods-marked-unavailable-in-swift-4-2/14949
https://forums.swift.org/t/using-methods-marked-unavailable-in-swift-4-2/14949
回答by Will Reynolds
Paste this into your viewDidLoad()
将此粘贴到您的 viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: [])
try AVAudioSession.sharedInstance().setActive(true)
}
catch {
print(error)
}
回答by Piyush Sharma
//Swift 4.2
if #available(iOS 10.0, *) {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,
mode: AVAudioSessionModeDefault)
} else {
//Fallback on earlier versions
}
回答by budidino
Swift 5
斯威夫特 5
let audioSession = AVAudioSession.sharedInstance()
_ = try? audioSession.setCategory(.playback, options: .defaultToSpeaker)
_ = try? audioSession.setActive(true)
回答by Sasan Soroush
Code for swift 4 :
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)
}
Hope that helps
希望有帮助