android spinner.setSelection(position, false); is not executed
I have a MediaPlayer object and a spinner with all the songs located on
the sd card. I'm trying to create the codes of each Play, Pause, Stop,
Previous and Next buttons.
When an item from the spinner is being selected, i'm getting the
MediaPlayer from it, and set its data source and calling prepare method.
Here's the code:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Context context = getApplicationContext();
CharSequence text = "onItemSelected";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(sdcard_playlist.get(arg2));
applyValuesToEqualizer();
mediaPlayer.prepare();
index = arg2;
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
And here's the code for each Stop and Next buttons:
public void stopSong(View view) {
if (isPlaying) {
mediaPlayer.reset();
isPlaying = false;
spinner.setSelection(index, false); // index is the index of the
chosen item from spinner
seekbar.setProgress(0);
}
}
public void nextSong(View view) {
if (isPlaying) {
mediaPlayer.reset();
isPlaying = false;
spinner.setSelection(index + 1, false);
seekbar.setProgress(0);
playPauseSong(findViewById(R.id.pause_music_button));
} else {
spinner.setSelection(index + 1, false);
seekbar.setProgress(0);
}
}
What's is happening is that when nextSong() is called, everything is
working fine and the toast in onItemSelected() is shown, but when
stopSong() is called, onItemSelected() is not being executed and the toast
is not shown, the song is being stopped but when I click play button
again, i get an exception: start called in state 1, error (-38, 0), it's
because the mediaPlayer is reset and not prepared again.
Thanks in advance :)
No comments:
Post a Comment