Android MediaPlayer setNextMediaPlayer () alternative

I recently published an application, and users report setNextMediaPlayer() because my program included the setNextMediaPlayer() method. Now I understand that this only works for API 16+, and my application supports API 8+ . I was wondering if there is an alternative way to achieve the same effect.

My application has text to speech, and what I did was create an ArrayList of MediaPlayers , in which each of them had a short sound file, and then played them one by one. If I remove this method from the code, the sound will become too volatile to understand.

I was thinking about using the SoundPool class, but there is no OnCompleteListener , so I'm not sure how I would do it.

So basically, my question is: Is there a way to seamlessly transition between audio files without using the setNextMediaPlayer() method?

Thanks so much for your time!

EDIT

I added this code that I found

  private class CompatMediaPlayer extends MediaPlayer implements OnCompletionListener { private boolean mCompatMode = true; private MediaPlayer mNextPlayer; private OnCompletionListener mCompletion; public CompatMediaPlayer() { try { MediaPlayer.class.getMethod("setNextMediaPlayer", MediaPlayer.class); mCompatMode = false; } catch (NoSuchMethodException e) { mCompatMode = true; super.setOnCompletionListener(this); } } public void setNextMediaPlayer(MediaPlayer next) { if (mCompatMode) { mNextPlayer = next; } else { super.setNextMediaPlayer(next); } } @Override public void setOnCompletionListener(OnCompletionListener listener) { if (mCompatMode) { mCompletion = listener; } else { super.setOnCompletionListener(listener); } } @Override public void onCompletion(MediaPlayer mp) { if (mNextPlayer != null) { // as it turns out, starting a new MediaPlayer on the completion // of a previous player ends up slightly overlapping the two // playbacks, so slightly delaying the start of the next player // gives a better user experience SystemClock.sleep(50); mNextPlayer.start(); } mCompletion.onCompletion(this); } } 

But how to add audio files? I tried this:

  // assigns a file to each media player mediaplayers = new ArrayList<CompatMediaPlayer>(); for (int i = 0; i < files.size(); i++) { mediaplayers.add((CompatMediaPlayer) CompatMediaPlayer.create(this, files.get(i))); } 

but I get a class exception because MediaPlayer cannot be passed to CompatMediaPlayer.

+7
java android soundpool
source share
1 answer

Create a compatible player that will work with onCompletionListener to launch the following player, for example:

 public void onCompletion(MediaPlayer mp) { if (mCompatMode && mNextPlayer != null) { mNextPlayer.prepare(); mNextPlayer.start(); } } 

Somewhere in your constructor, check if there is a method (or check the SDK version) called "setNextMediaPlayer"

mCompatMode = Build.VERSION.SDK_INT < 16;

Define a method similar to this:

 public void setNextMediaPlayer(MediaPlayer next) { if (mCompatMode) { mNextPlayer = next; } else { super.setNextMediaPlayer(next); } } 
+6
source share

All Articles