我正在开发一个IME,我有按钮点击的声音。我在首选项屏幕中有一个选项可以改变声音的音量。 SeekBar值从0.0到1.0。现在我尝试让用户在首选项屏幕中配置按钮的音量,稍后我会得到此值并将其保存为mSoundVol
参数。因此,对于点击的声音,我写了以下方法:
float soundVolume;
int maxVolume, sound;
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE:
maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_DELETE);
sound = mAudioManager.FX_KEYPRESS_DELETE;
Log.d(TAG+ "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_DELETE");
break;
case ASCII_ENTER:
maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_RETURN);
sound = mAudioManager.FX_KEYPRESS_RETURN;
Log.d(TAG+ "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_RETURN");
break;
case ASCII_SPACE:
maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_SPACEBAR);
sound = mAudioManager.FX_KEYPRESS_SPACEBAR;
Log.d(TAG+ "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_SPACEBAR");
break;
default:
maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_STANDARD);
sound = mAudioManager.FX_KEYPRESS_STANDARD;
Log.d(TAG + "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_STANDARD");
}
soundVolume = maxVolume * mSoundVol;
Log.d(TAG+ "-volume", "current max volume: " + maxVolume + " current volume setting: " +mSoundVol * 100 +"%" + " volume result: " + soundVolume);
mAudioManager.playSoundEffect(sound, soundVolume);
但由于某种原因,这不会改变用户的声音音量。
有人可以告诉我,我在这里做错了什么吗?
感谢。
答案 0 :(得分:0)
您需要使用此方法获得更准确的结果
AudioManager audioManager =
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
[int value],
[if desired a flag]);
修改
以此FLAg为例
AudioManager.FLAG_PLAY_SOUND
这意味着每当用户按下音量按钮时,双音将被输出
编辑2
这是代码的实现
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.getStreamMaxVolume(),
AudioManager.FLAG_PLAY_SOUND);
答案 1 :(得分:0)
我的案例中的解决方案是:
float soundVolume;
int maxVolume, sound;
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE:
sound = mAudioManager.FX_KEYPRESS_DELETE;
break;
case ASCII_ENTER:
sound = mAudioManager.FX_KEYPRESS_RETURN;
break;
case ASCII_SPACE:
sound = mAudioManager.FX_KEYPRESS_SPACEBAR;
break;
default:
sound = mAudioManager.FX_KEYPRESS_STANDARD;
break;
}
maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
soundVolume = maxVolume * mSoundVol;
mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM , (int)(soundVolume * 1.5) , 0);
mAudioManager.playSoundEffect(sound);
重要的是将0
作为setStreamVolume
方法的最后一个参数传递,这样一来,此操作只会更改音量而不播放任何其他显示音量UI的声音。