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

查看:31
本文介绍了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 类,这是播放我的 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); 
}

问题是我播放剪辑的音量似乎取决于用户设置的设置/音频/音量"设置.相反,它似乎与硬件音量按钮设置无关.

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 :)

一些实际执行此操作的代码,我使用的是 MediaPlayer 而不是声音池,因为这为您提供了播放完整回调,而声音池中似乎不存在:

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);
   }
});

顺便说一下 with call mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 0, 0, 1.0f); streamVolume 值实际上是浮点数 0 -> 1,代表一个最大值的百分比,所以你真的只想在那里输入 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天全站免登陆