Android的:如何在最大可能的音量播放音乐?音量、如何在、最大、音乐

2023-09-12 10:01:34 作者:槿 栀

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

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.