kAudioSessionCategory_MediaPlayback 在铃声关闭时静音?

发布于 2024-11-16 21:38:42 字数 428 浏览 3 评论 0原文

根据文档,即使铃声开关设置为关闭,也允许播放声音的正确方法如下:

 UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);

然后,我在整个应用程序中播放短的 wav 文件,这些文件可以使用

AudioServicesPlaySystemSound(sysSound)

.... 完美播放,直到铃声关闭,并且然后……沉默?

如果铃声关闭,无论我做什么都无法播放声音?

请问有谁吗?

according to the documentation the correct way to allow sounds to be played even if the ringer switch is set to off is like so:

 UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);

I then throughout the app play short wav files that play perfectly using

AudioServicesPlaySystemSound(sysSound)

....until the ringer is switched off and then.....silence?

No matter what I do I cannot get the sound to play if the ringer is off?

Any one ..... please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤慢歌 2024-11-23 21:38:42

您最后是否尝试使用 AudioSessionSetActive(YES);

Did you try using AudioSessionSetActive(YES); at the end?

ヤ经典坏疍 2024-11-23 21:38:42

根据我的经验,当铃声关闭时,AudioServicesPlaySystemSound 永远不会播放。如果您使用 AVAudioPlayer 播放声音,您应该获得您正在寻找的行为:

let player: AVAudioPlayer = try AVAudioPlayer(contentsOf: url)
player.delegate = self
player.play()

如果您还没有激活音频会话,则还需要在播放之前激活音频会话。并设置一个音频会话类别,支持静音时发出声音,就像您已经做的那样。请考虑在没有播放音频时停止音频会话以节省电池寿命。

AudioServices 和 AVAudioPlayer 之间的一个显着区别是,如果铃声静音,AudioServices 播放几乎会立即返回,而 AVAudioPlayer 将在完成之前播放完整的声音长度。据我所知,检测和响应用户铃声设置的唯一方法是使用 AudioServices 而不是 AVAudioPlayer。如果您只是想在静音时玩游戏,那么这应该不相关。

AudioServicesPlaySystemSound never plays while the ringer is switched off in my experience. If you play sounds using AVAudioPlayer instead, you should get the behavior you are looking for:

let player: AVAudioPlayer = try AVAudioPlayer(contentsOf: url)
player.delegate = self
player.play()

You will also need to activate the audio session before playing if you have not already. And set an audio session category that supports sounds while silenced like you already did. Consider stopping the audio session when no audio is playing to conserve battery life.

A notable difference between AudioServices and AVAudioPlayer is that if the ringer is silenced, AudioServices playback will return almost immediately while AVAudioPlayer will take the full length of the sound before completing. As far as I know, the only way to detect and respond to the user's ringer setting is to use AudioServices instead of AVAudioPlayer. If you just want to play while silenced though this should not be relevant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文