Android Mediaplayer play different songs after eachother
store the source locations in an array and in the onCompletionListener cycle through the source. something like this (use the same media player instance)
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp)
{
if(currentPosition<sourceArray.size())
{
mediaPlayer.reset();
/* load the new source */
mediaPlayer.setDataSource(sourceArray[position]);
/* Prepare the mediaplayer */
mediaPlayer.prepare();
/* start */
mediaPlayer.start();
}
else
{
/* release mediaplayer */
mediaPlayer.release();
}
}
dorien
Assistant Professor at Singapore University of Technology and Design. Working on Machine learning and optimization algorithms for digital music and other novel applications.
Updated on June 28, 2022Comments
-
dorien almost 2 years
I generate midi files on the go. I want to play these files continuously.
I initialise a mediaplayer and start song1.mid. Then I use the following code to play song2.mid
// set on completion listener music file mediaPlayer .setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { String filePath2 = null; File file = null; FileInputStream inputStream = null; //set the filePath try { filePath2 = getCacheDir() + "/optimuse" + song + ".mid"; file = new File(filePath2); if (file.exists()) { inputStream = new FileInputStream(file); if (inputStream.getFD().valid()) { System.out.println("Valid!"); } } } catch (Exception e1) { e1.printStackTrace(); System.exit(-1); } //set Mediaplayer's datasource if (file.exists()) { try { mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(inputStream.getFD()); inputStream.close(); } catch (Exception e1) { e1.printStackTrace(); System.exit(-1); } try { mediaPlayer.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //if the player is not running if (!mediaPlayer.isPlaying()) { //start the player mediaPlayer.start(); Toast.makeText(MainActivity.this, "mediaPlayer.start()", Toast.LENGTH_LONG) .show(); } } }); } });
The problem is that the mediaplayer stops after song2. But I want song3 to start. I can increment the global variable, ok. But the onCompletionListener does not seem to work when the second song finishes.
I guess I should initialise MediaPlayer mp to have an onCompletionListener too? Not sure what the best approach is.
Or should I do something like:
new class MediaPlayer implements OnCompletionListener(){ @Override song++; //code to start the mediaplayer }
Thank you for putting me in the right direction. I am also a little bit concerned with efficiency, when I keep starting up new mediaplayers...
Basically, what I want to do is play song1.mid, song2.mid,... continuously, but the files are generated during the program.
EDIT Thanks to the wonderful help of @Gan. I know have the following working code:
// set on completion listener music file mediaPlayer .setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { String filePath2 = null; File file = null; FileInputStream inputStream = null; //set the filePath try { filePath2 = getCacheDir() + "/optimuse" + song + ".mid"; file = new File(filePath2); if (file.exists()) { inputStream = new FileInputStream(file); if (inputStream.getFD().valid()) { System.out.println("Valid!"); } } } catch (Exception e1) { e1.printStackTrace(); System.exit(-1); } //set Mediaplayer's datasource if (file.exists()) { try { mp.stop(); mp.reset(); mp.setDataSource(inputStream.getFD()); inputStream.close(); } catch (Exception e1) { e1.printStackTrace(); System.exit(-1); } try { mp.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //if the player is not running if (!mp.isPlaying()) { //start the player mp.start(); Toast.makeText(MainActivity.this, "mp.start()", Toast.LENGTH_LONG) .show(); } } }); } });
-
dorien over 11 yearsThanks, but mediaPlayer doesn't seem to go to oncompletionlistener after the second song ends (at least it does not do it now). Because with the current code, it should keep repeating song2.mid, but it does not (I which it did, lol)
-
Gan over 11 yearsi see that in your code that you have created a new instance and you did not set the onCompletionListener. if you use a new instance you have to specify the onCompletionListener every time.
-
Gan over 11 yearsTry using the same instance, its more efficient.
-
dorien over 11 yearsThat seems to make sence, but How do I do that? If I delete @Override public void onCompletion(MediaPlayer mp) { }); it doesn't want to compile.
-
Gan over 11 yearsyou have to override the onCompletion method since its the callback method. just follow the edited code. just load the next source file in the mediaplayer, prepare it and start it. ( I assume that you have stored the source file locations in the
soureceArray
andposition
is a just counter to cycle through the array) -
dorien over 11 yearsThanks! Ok, I see I shouldn't call mediaPlayer = new MediaPlayer(); I deleted this and now I still get a runtime error attachNewPlayer called. Let me debug...
-
dorien over 11 yearsGot it! Wow great! So stupid, I had to use mp instead of mediaPlayer (of course). And add a mp.stop() and mp.reset(). I have put my working code as an edit in my question, I hope that is ok.
-
Gan over 11 years@dorien, stop is not necessary, but reset is! ( i somehow forgot that, i have edited it now). you can also use mediaPlayer, but using mp is better.