覆盖iOS 11.3中的输出音频端口 [英] Override output audio port in iOS 11.3

查看:126
本文介绍了覆盖iOS 11.3中的输出音频端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以覆盖扬声器的音频端口(

Is it possible to override audio port to speaker(

[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil]

)当设备通过airplay连接到外部屏幕时,它可以在iOS 11.2和更早版本上运行,但是在iOS 11.3上不能运行吗?

) when device is connected to external screen via airplay, it worked on iOS 11.2 and earlier, but on iOS 11.3 it doesn't?

推荐答案

根据我的经验,这还不够:您还必须将AVAudioSession类别设置为AVAudioSessionCategoryPlayAndRecord.在overrideOutputAudioPort: 方法的文档中建议:

In my experience that's not enough: you also have to set the AVAudioSession category to AVAudioSessionCategoryPlayAndRecord. This is suggested in the docs for the overrideOutputAudioPort: method:

如果您的应用使用AVAudioSessionCategoryPlayAndRecord类别, 使用Speaker选项调用此方法将导致音频被路由 内置扬声器和麦克风,而不管其他设置如何. 该更改仅在当前路线更改或更改之前一直有效. 您可以使用none选项再次调用此方法.如果您愿意 要永久启用此行为,您应该设置 类别的defaultToSpeaker选项.设置此选项将始终 如果没有其他附件,则将其路由到扬声器而不是接收器 因为正在使用耳机.

If your app uses the AVAudioSessionCategoryPlayAndRecord category, calling this method with the speaker option causes audio to be routed to the built-in speaker and microphone regardless of other settings. This change remains in effect only until the current route changes or you call this method again with the none option. If you would prefer to permanently enable this behavior, you should instead set the category's defaultToSpeaker option. Setting this option will always route to the speaker rather than receiver if no other accessory such as headphones are in use.

请注意,这些文档指定足够了.我不知道是否有必要:我只是没有找到其他可以可靠工作的选择.

Note that those docs specify that that is sufficient. I don't know if it's necessary: I just haven't found other options that work as reliably.

但是,defaultToSpeaker 文档确实会说它仅可用于AVAudioSessionCategoryPlayAndRecord类别.

所以,为我做以下工作:

So, doing the following works for me:

let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord,
                             mode: AVAudioSessionModeDefault)
try audioSession.overrideOutputAudioPort(.speaker)

这篇关于覆盖iOS 11.3中的输出音频端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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