我试图做到这一点,以便当用户打开应用程序时,它将音乐的音量设置为手机铃声的音量。到目前为止,这是我的代码,但我不太确定 setVolume(float, float) 上的参数是什么。 android 文档没有很好地解释它。我的代码在这里做错了什么?
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
mPlayer = MediaPlayer.create(this, R.raw.song);
mPlayer.setOnErrorListener(this);
if(mPlayer!= null)
{
mPlayer.setLooping(true);
mPlayer.setVolume(currentVolume,1);
}
好像音频.setStreamVolume http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume%28int,%20int,%20int%29是你想要的,但传入 STREAM_MUSIC 而不是 STREAM_RING。
注意:音乐音量和铃声音量可能有不同的最大值,因此您需要对它们进行标准化。为此,请使用 getStreamMaxVolume。
我以前没有这样做过,也没有编译过这个,但是代码应该看起来像这样
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Get the current ringer volume as a percentage of the max ringer volume.
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int maxRingerVolume = audio.getStreamMaxVolume(AudioManager.STREAM_RING);
double proportion = currentVolume/(double)maxRingerVolume;
// Calculate a desired music volume as that same percentage of the max music volume.
int maxMusicVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int desiredMusicVolume = (int)(proportion * maxMusicVolume);
// Set the music stream volume.
audio.setStreamVolume(AudioManager.STREAM_MUSIC, desiredMusicVolume, 0 /*flags*/);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)