我如何在我的Andr​​oid应用三立管理音频体积? [英] How can I manage audio volumes sanely in my Android app?

查看:151
本文介绍了我如何在我的Andr​​oid应用三立管理音频体积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,播放断断续续的声音,而其活动是开放的,用户总是期望它使这些噪音,但不幸的是它是由音乐流音量限制。我发现的选项是这样的:

I have an app that plays intermittent sounds while its activity is open, and the user is always expecting it to make these noises, but unfortunately it is constrained by the music stream volume. The options I have discovered are thus:

  1. 调整音乐流的音量,可能震耳欲聋的用户,如果他们碰巧在播放音乐的时候。
  2. 呼叫MediaPlayer.setVolume(),这是无效的,如果音乐流的音量为0。
  3. 在手柄音量键presses自己这presents两个问题:音量控制键,presses调整振铃的音量,除非我的音频播放,而且我一直作为尚未无法赶上onKey事件的音量按钮用我的OnKeyListener preSS。如果有一些方法来强制调整适用于音乐流,而我的活动有焦点,或将正好赶上按钮presses自己这是可行的。
  4. 播放无声的循环一段音频在后台保持在上下文中的音乐流的数量调整。

我觉得第三个选项可能是最好的,因为它割让卷到用户的控制,但如果有某种方式只覆盖在一个媒体播放器实例的系统容量,将工作了。

I think the 3rd option is probably best since it cedes control of the volume to the user, but if there were some way to just override the system volume in a single mediaplayer instance that would work too.

推荐答案

得到通过谷歌论坛的另一个建议,是该平台集成的解决方案,我一直在寻找和工程罚款:

Got another suggestion via Google Groups that is the platform integrated solution I was looking for and works fine:

请不要处理的音量键   自己 - 这是几乎不可能   保证你不会打破   音量键的行为。

Please don't handle the volume keys yourself - it is almost impossible to guarantee that you won't break the behavior of the volume keys.

调用此API在你的onCreate():

Call this API in your onCreate():

setVolumeControlStream(AudioManager.STREAM_MUSIC);

setVolumeControlStream(AudioManager.STREAM_MUSIC);

这告诉AudioManager,当   您的应用程序具有焦点,音量   关键要调整音乐的音量。

This tells the AudioManager that when your application has focus, the volume keys should adjust music volume.

这篇关于我如何在我的Andr​​oid应用三立管理音频体积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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