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) {
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.
java android soundpool
user2397906
source share