In the application in MediaPlayer I load two sounds when a button is pressed, but they are played simultaneously. How to make the second sound played only after the first?

@Override public void onClick(View v) { switch (v.getId()) { case R.id.flats_btn1: if (flats == null || !flats.isPlaying()) { flats = MediaPlayer.create(getActivity(), R.raw.carrier); flats.start(); flats.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer flats) { flats.stop(); flats.release(); //запускаем следующий так же flats = MediaPlayer.create(getActivity(), R.raw.carrier2); flats.start(); } }); } break; 

Error log:

 08-15 01:23:33.921: E/AndroidRuntime(912): FATAL EXCEPTION: main 08-15 01:23:33.921: E/AndroidRuntime(912): java.lang.IllegalStateException 08-15 01:23:33.921: E/AndroidRuntime(912): at android.media.MediaPlayer.isPlaying(Native Method) 08-15 01:23:33.921: E/AndroidRuntime(912): at android.view.View.performClick(View.java:4204) 08-15 01:23:33.921: E/AndroidRuntime(912): at android.view.View$PerformClick.run(View.java:17355) 08-15 01:23:33.921: E/AndroidRuntime(912): at android.os.Handler.handleCallback(Handler.java:725) 08-15 01:23:33.921: E/AndroidRuntime(912): at android.os.Handler.dispatchMessage(Handler.java:92) 08-15 01:23:33.921: E/AndroidRuntime(912): at android.os.Looper.loop(Looper.java:137) 08-15 01:23:33.921: E/AndroidRuntime(912): at android.app.ActivityThread.main(ActivityThread.java:5041) 08-15 01:23:33.921: E/AndroidRuntime(912): at java.lang.reflect.Method.invokeNative(Native Method) 08-15 01:23:33.921: E/AndroidRuntime(912): at java.lang.reflect.Method.invoke(Method.java:511) 08-15 01:23:33.921: E/AndroidRuntime(912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08-15 01:23:33.921: E/AndroidRuntime(912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-15 01:23:33.921: E/AndroidRuntime(912): at dalvik.system.NativeStart.main(Native Method) 

    1 answer 1

    You need a listener to finish playing.

     MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.carrier); mp.start(); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Log.i("player", "onComplete hit"); mp.stop(); mp.release(); //запускаем следующий так же MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.carrier); mp.start(); } }); 
    • Embed this code in the if condition (flats == null ||! Flats.isPlaying ()) {? If you do this, then the next time you click, an error takes off. Code added above in question - turok09
    • And another question, is it possible to remove the delay between playing files? - turok09
    • @ turok09, if it falls, then you need to specify on which line and with what error. And the delay can be removed if you first load the files into the player, wait for the download to finish, and only then start - Yuriy SPb
    • Can you see why the application stops? Error log added above - turok09
    • @ turok09, well, probably mp.release (); need to be removed ... - YuriySPb