I have a little problem with my project.
In my project I want to change the volume of media player with the function "setVolume", but it's not working for me.
the volume options are in the spinner.
the code:
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) {
}
Can anybody tell me what is the problem in this code and fix me please?
Thanks for helpers!
use this code
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
}
}