AVAudioSession setCategory Swift 4.2 iOS 12-静音播放声音 [英] AVAudioSession setCategory Swift 4.2 iOS 12 - Play Sound on Silent

查看:149
本文介绍了AVAudioSession setCategory Swift 4.2 iOS 12-静音播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在静音模式下也要播放声音,我使用以下方法.但是它不起作用.

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

如何使其在4.2/iOS 12中正常工作?

How to get it work in 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#>)`

推荐答案

Her derTöne的注释向您显示了新语法,但是您还需要在setCategory之后激活音频会话:

Her der Töne's comment shows you the new syntax, but you also need to activate the audio session after setCategory:

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

这篇关于AVAudioSession setCategory Swift 4.2 iOS 12-静音播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆