Reputation: 187
I want to be able to control the volume with on screen buttons (A plus and a minus)
I'm using the following in the OnClick method:
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 is a method I want to use so I don't go below volume 0 or above max volume
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);
}
}
In logcat my cv (currentRingerVolume) is always 8 and my v(Volume) only varies between 5-8
I'm also getting this in the 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.
any help would be greatly appreciated
Upvotes: 1
Views: 66
Reputation: 24417
Change your code to set the volume from the same stream you get it from:
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);
Also, this volume adjustment code will not work if the current volume is at the max and you are trying to reduce it. Change to this:
if((volume != maxRingerVolume) || (i == -1)){
volume = currentRingerVolume + i;
volumeInt.setText(volume.toString());
Log.v("tag ", "v:" + volume + " cv:" + currentRingerVolume);
}
A more easy to read way to rearrange your code might be something like this:
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);
Upvotes: 1