Android : how do you stop the running song and play the newly selected song using mediaplayer in android?

on Friday, September 19, 2014


I have tried all the posts I could find and I haven't gotten any solution. This is the code I have so far:



public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mPlayer = new MediaPlayer();
boolean isPlaying = false;

String songid = ((TextView) view).getText().toString();
Uri u = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, songid);

try {
mPlayer.setDataSource(getApplicationContext(), u);
} catch (IOException e) {
e.printStackTrace();
}

if (!isPlaying) {
try {
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mPlayer.start();
isPlaying = true;
} else {
isPlaying = false;
mPlayer.stop();
mPlayer.reset();

mPlayer.start();
isPlaying = true;
}
}

0 comments:

Post a Comment