gpt4 book ai didi

android - 使用按钮控制音量

转载 作者:行者123 更新时间:2023-11-30 02:24:39 25 4
gpt4 key购买 nike

我希望能够使用屏幕按钮(加号和减号)控制音量

我在 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(Volume) 仅在 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.

任何帮助将不胜感激

最佳答案

更改您的代码以设置您从中获取的同一流的音量:

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);

关于android - 使用按钮控制音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28031512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com