将声音添加到 iPhone 应用程序的标准方法(全局音量指示器)

发布于 2024-07-27 16:58:50 字数 539 浏览 3 评论 0原文

我有一个具有多种视图的应用程序。 主菜单没有任何声音,但接下来的视图使用 AVAudioPlayer 类播放声音。

因此,当有人启动应用程序并位于主菜单时,如果他更改设备上的音量,他实际上会更改“铃声”音量。 如果他进入其他视图(有声音的地方),当他更改设备上的音量时,他会更改游戏的音量,而不是铃声。

有没有办法让他们每次从我的应用程序开始,在我创建任何 AVAudioPlayer 实例之前更改应用程序音量? (有些人误解了。我的意思是每次按下音量按钮时操作系统覆盖在屏幕上的那个方块。如果您在主屏幕上,您可以更改“铃声”。当您在游戏中时,您可以更改应用程序的音量)。

附: 我在主菜单上初始化了 AudioSession,但这没有任何区别。 我发现的唯一黑客是在我的主菜单上实际创建 AVAudioPlayer 的实例并将其设置为“preparedToPlay”。 但我宁愿听听其他人在做什么(正确的解决方案)。

I have an app with various views. The main menu does not have any sounds, but the next views play sounds using the AVAudioPlayer Class.

So when someone launches the app and is in the main menu, if he changes the volume on his device, he actually changes the "Ringer" volume. If he proceeds to the other views (where we have sound), when he changes the volume on the device he changes the volume of the game, not the ringer.

Is there a way to have them change the app volume every time, from the beginning of my app, before I create any instances of AVAudioPlayer??
(some misunderstood that. What I mean is that square that the OS overlays on the screen every time you press the volume buttons. If you are on the home screen, you change the "Ringer". When you are in a game, you change the app's volume).

PS:
I initialize the AudioSession on my main menu but that doesn't make any difference.
The only hack i have found is to actually create an instance of AVAudioPlayer on my main menu and set it to "preparedToPlay". But I would rather hear what the others are doing (a proper solution).

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

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

发布评论

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

评论(2

迷途知返 2024-08-03 16:58:50

如果您的应用程序当前未播放音频,音量按钮将影响铃声音量。 一些应用程序通过播放无声音频文件来解决此问题。 不过,请仔细考虑如何做到这一点 - 当应用程序实际上没有播放真正有用的内容时是否调整应用程序的音量?

看来您应该能够通过修改 AudioSession 属性来完成您想要的操作,但我还没有弄清楚如何(如果可能的话)。

The volume buttons will affect the ringer volume if your application isn't currently playing audio. Some apps work around this by playing a silent audio file. Carefully consider how you do this, though - is adjusting the apps volume when it's not actually playing something actually useful?

It seems like you ought to be able to do what you want by modifying the AudioSession properties, but I haven't figured out how (if it is possible).

上课铃就是安魂曲 2024-08-03 16:58:50

有一个名为 MPVolumeView 的类用于此目的。 这是解释 MPVolumeView 使用的好链接

将 MPVolumeView 添加到您的视图中将创建一个类似 iPod 的音量滑块,当您使用摇杆按钮更改音量时,该滑块会发生变化。 AVAudioPlayer 类将自动使用该值。

不使用滑块跟踪音量

默认情况下,摇杆按钮仅在播放某些音频时更改应用程序音量。 此问题有一个解决方法 - 这个 SO 线程 讨论了一些解决方法。

There is a class called MPVolumeView which is used for this purpose. Here's a good link explaining the use of MPVolumeView

Adding MPVolumeView to your view will create an iPod-like volume slider which will change when you change the volume using the rocker buttons. The value will automatically be used by the AVAudioPlayer class.

To keep track of volume without the slider

The rocker buttons by default change the app volume only when some audio is playing. There is a workaround to this problem - this SO thread discusses some workarounds.

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