我希望能够通过屏幕按钮(A加号和减号)
来控制音量我在OnClick方法中使用以下内容:
case R.id.bMinus:
currentRingerVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
newvolume(-1);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
volume, 0);
break;
case R.id.bPlus:
currentRingerVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
newvolume(1);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
volume, 0);
newvolume是我想要使用的方法,因此我不会低于0或更高的最大音量
private void newvolume(int i) {
/*
currentRingerVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
maxRingerVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
*/
if(volume != maxRingerVolume){
volume = currentRingerVolume + i;
volumeInt.setText(volume.toString());
Log.v("tag ", "v:" + volume + " cv:" + currentRingerVolume);
}
if (currentRingerVolume == 0 && i == -1){
volume = 0;
volumeInt.setText(currentRingerVolume.toString());
Log.v("tag ", "v:" + volume + " cv:" + currentRingerVolume);
}
}
在logcat中我的cv(currentRingerVolume)总是8而我的v(音量)只在5-8之间变化
我也是在logcat
中得到这个12908-12914/com.asdf.wasd D/dalvikvm﹕ JIT code cache reset in 0 ms (4096 bytes 2/0)
01-19 13:35:11.779 12908-12914/com.asdf.wasd D/dalvikvm﹕ GC_EXPLICIT freed 1204K, 29% free 19409K/27312K, paused 7ms+14ms, total 103ms
01-19 13:35:12.940 12908-12914/com.asdf.wasd I/dalvikvm﹕ hprof: dumping heap strings to "[DDMS]".
01-19 13:35:17.224 12908-12914/com.asdf.wasd I/dalvikvm﹕ hprof: heap dump completed (20349KB)
01-19 13:35:17.244 12908-12908/com.adsf.wasd I/Choreographer﹕ Skipped 300 frames! The application may be doing too much work on its main thread.
任何帮助将不胜感激
答案 0 :(得分:1)
更改您的代码,以便从您获得的相同流中设置音量:
case R.id.bMinus:
currentRingerVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
newvolume(-1);
audioManager.setStreamVolume(AudioManager.STREAM_RING,
volume, 0);
break;
case R.id.bPlus:
currentRingerVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
newvolume(1);
audioManager.setStreamVolume(AudioManager.STREAM_RING,
volume, 0);
此外,如果当前音量达到最大值并且您尝试减小音量调节代码,则此音量调节代码将不起作用。改为:
if((volume != maxRingerVolume) || (i == -1)){
volume = currentRingerVolume + i;
volumeInt.setText(volume.toString());
Log.v("tag ", "v:" + volume + " cv:" + currentRingerVolume);
}
重新排列代码的更容易阅读的方法可能是这样的:
volume = currentRingerVolume + i; // apply change
if(volume > maxRingerVolume)
volume = maxRingerVolume; // clamp to max
if(volume < 0)
volume = 0; // clamp to min
// output debug information:
volumeInt.setText(volume.toString());
Log.v("tag ", "v:" + volume + " cv:" + currentRingerVolume);