我的项目有点问题。
在我的项目中,我想用功能" setVolume"来改变媒体播放器的音量,但它不适合我。
音量选项位于微调器中。
代码:
public void setSpinner() {
Spinner spine = (Spinner) findViewById(R.id.spinner);
spine.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.power, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spine.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String s = parent.getItemAtPosition(pos).toString();
if (s == "minimum") {
int maxVolume = 50;
float log1 = (float) (Math.log(maxVolume - 49) / Math.log(maxVolume));
song.setVolume(1 - log1, 1 - log1);
}
if (s == "average") {
int maxVolume = 50;
float log1 = (float) (Math.log(maxVolume - 24) / Math.log(maxVolume));
song.setVolume(1 - log1, 1 - log1);
}
if (s == "maximum") {
int maxVolume = 50;
float log1 = (float) (Math.log(maxVolume - 1) / Math.log(maxVolume))
song.setVolume(1 - log1, 1 - log1);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
有人可以告诉我这段代码中的问题并解决我的问题吗?
感谢帮助者!
答案 0 :(得分:0)
使用此代码
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
String s = parent.getItemAtPosition(pos).toString();
if (s.equals("minimum")) {
am.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}else if (s.equals("average")) {
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)/2, 0);
}else if (s.equals("maximum")) {
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
}else {
//set default volume
}
}