我下载了一些代码,这些代码不是在本地类中播放我的声音,而是调用另一个类来播放声音,唯一的问题是不允许我使用手机上的音量键来调整音量,而本地类中的旧代码允许我这样做。
按照旧的方式,本地类有以下代码:
AudioManager mgr = (AudioManager) GameScreen_bugfix.this.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0,1f);新代码如下所示:
public static void playSound(int index,float speed)
{
float streamVolume;
try {
streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
} catch (Exception e) {
e.printStackTrace();
//Log.v("THE ERROR", mSoundPoolMap+" "+index);
}
}所以我试着这样改变它:
AudioManager mgr = (AudioManager) SoundManager.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr
.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;但是这个getSystemService会被突出显示为红色,当我鼠标点击它的时候,它会说:
方法getSystemService(String)未定义为SoundManager类型
该怎么办呢?谢谢!
发布于 2011-10-05 18:34:41
如果要在系统定义的声音级别上播放声音,请更改以下内容:
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);对此:
mSoundPool.play(mSoundPoolMap.get(index), 1, 1, 1, 0, speed);您传递给play的音量级别在0到1之间。它代表系统音量的一个百分比来播放声音。因此,.5的值将在系统音量水平的50%处发挥作用。
发布于 2011-11-08 20:03:18
实际上,如果您想要的是硬件卷键来调整媒体流而不是铃声流,则需要使用http://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int)请求
@Override
public void onStart() {
super.onStart();
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}https://stackoverflow.com/questions/7636897
复制相似问题