Android的:如何在最大可能的音量播放音乐? [英] Android: how to play music at maximum possible volume?

查看:258
本文介绍了Android的:如何在最大可能的音量播放音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题可以看作是政治不正确,但我设计一个应用程序,其中设计必须得到最大可能的距离范围内人们的重视,否则将不会被使用... : - )

I know the question can be regarded as "politically incorrect", but I'm designing an app which "by design" must get the attention of people within the maximum possible distance range, otherwise it will not be used... :-)

我目前使用SoundManager类类,这是code起着我OGG片段:

I'm currently using SoundManager class, and this is the code which plays my ogg clip:

public void playSound(int index) { 
     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 0, 0, 1.0f); 
}

的问题是,声音的音量予得到使用了与剪辑出现由设置/音频/电压快速设置用户已设定为相关的。相反,它似乎是由硬件音量按钮设置来indipendent

The problem is that the sound volume I get the clip played with appears to be dependent by "Settings/Audio/Voulme" settings the user has set. Instead it appears to be indipendent by the hardware volume buttons setting.

有没有办法为一个Android应用程序播放声音通过设备允许的最大物理卷?

Is there a way for an Android app to play a sound to the maximum physical volume allowed by the device?

推荐答案

我建议使用getStreamMaxVolume和setStreamVolume要做到这一点:

I'd suggest using getStreamMaxVolume and setStreamVolume to do this:

int origionalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);

然后,一旦你做到了这设置回原始卷。

Then once you're done just set it back to the original volume.

我觉得我被打一拳,稀释好:)

I think I was beaten to the punch, ahh well :)

有些code,实际上做到这一点,我使用的MediaPlayer,而不是作为的Soundpool这给你不似乎是对的Soundpool present播放完整的回调:

Some code that actually does this, I'm using the MediaPlayer rather than the soundpool as this gives you a play complete callback which doesn't appear to be present on the soundpool:

final AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
final int originalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource("content://media/internal/audio/media/97");
mp.prepare();
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
   @Override
   public void onCompletion(MediaPlayer mp)
   {
      mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 0);
   }
});

顺便说一句的与呼叫 mSoundPool.play(mSoundPoolMap.get(指数),streamVolume,streamVolume,0,0,1.0F); 的streamVolume值实际上是浮动0 - > 1,稀土present最大值的百分比所以你真的只是希望把1.0F有

Btw the with call mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 0, 0, 1.0f); the streamVolume values are actually floats 0 -> 1 that represent a percentage of the maximum value so you'd really just want to put in 1.0f there.

这篇关于Android的:如何在最大可能的音量播放音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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