Android 用按钮控制音量
我希望能够通过屏幕上的按钮(加号和减号)控制音量 我在OnClick方法中使用了以下内容:Android 用按钮控制音量,android,Android,我希望能够通过屏幕上的按钮(加号和减号)控制音量 我在OnClick方法中使用了以下内容: case R.id.bMinus: currentRingerVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING); newvolume(-1); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
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(Volume)仅在5-8之间变化
我也在日志中找到了这个
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.
非常感谢您提供的任何帮助更改您的代码,以设置来自同一流的音量:
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);
volume=currentRingerVolume+i;//应用更改
如果(音量>最大音量)
体积=maxRingerVolume;//夹紧至最大
如果(体积<0)
体积=0;//夹紧至最小值
//输出调试信息:
volumeInt.setText(volume.toString());
Log.v(“标签”,“v:+体积+”cv:+currentRingerVolume);
是因为您正在从AudioManager.STREAM\u铃声中获取音乐,但设置为AudioManager.STREAM\u音乐吗?