MediaPlayer does not work sometimes

1

The problem I have not located will therefore try to be as specific as possible because the compiler does not throw any errors and when it passes through the debugger test, either, but in some cell phones a very peculiar error occurs which I have tried at least identify but I do not give with the answer

In some cell phones when I create many MediaPlayer some at the time of being reproduced do not work, to be more clear I will put the code that is explained by itself.

        sonido1 = MediaPlayer.create(this,R.raw.sonido1);
        sonido2 = MediaPlayer.create(this,R.raw.sonido2);
        sonido3 = MediaPlayer.create(this,R.raw.sonido3);
        sonido4 = MediaPlayer.create(this,R.raw.sonido4);

Example event click and start of a MediaPlayer

btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(sonido1.isPlaying() ){
                    sonido1.pause();
                    sonido1.seekTo(0);

                    btn1.setBackgroundColor(Color.parseColor("#ffb806"));

                }else{
                    sonido1.seekTo(0);
                    sonido1.start();
                    btn1.setBackgroundColor(Color.parseColor("#e1a206"));
                }
            }

        });

The problem is that in some cell phones it works without problem and when I compile it or test it with a debugger there are no errors that I can observe however in some cell phones when there are many sounds the last ones do not reproduce as they should. Example the sound18.

I hope I have been as clear and specific as possible.

    
asked by WhySoBizarreCode 07.11.2018 в 04:09
source

1 answer

3

The main problem is because you create several instances of MediaPlayer, in reality you only need one instance.

Very important to use the listener MediaPlayer.OnPreparedListener that is invoked when the media source is ready for playback, at this time you can call .start() .

This is an example according to the code of your question:

import android.graphics.Color;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static String TAG = "MainActivity";
    private int[] sonidos = {R.raw.sound1, R.raw.sound2, R.raw.sound3, R.raw.sound4};
    private MediaPlayer mediaPlayer;
    private Button btn1, btn2, btn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


         //Obtiene referencia de botones.
         btn1 = findViewById(R.id.button);
         btn2 = findViewById(R.id.button2);
         btn3 = findViewById(R.id.button3);



        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(mediaPlayer !=null && mediaPlayer.isPlaying() ){
                    mediaPlayer.pause();
                    mediaPlayer.seekTo(0);

                    btn1.setBackgroundColor(Color.parseColor("#ffb806"));

                }else{
                    playMedia(0);
                    btn1.setBackgroundColor(Color.parseColor("#e1a206"));
                }
            }

        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(mediaPlayer !=null && mediaPlayer.isPlaying() ){
                    mediaPlayer.pause();
                    mediaPlayer.seekTo(0);

                    btn2.setBackgroundColor(Color.parseColor("#ffb806"));

                }else{
                    playMedia(1);
                    btn2.setBackgroundColor(Color.parseColor("#e1a206"));
                }
            }

        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(mediaPlayer !=null && mediaPlayer.isPlaying() ){
                    mediaPlayer.pause();
                    mediaPlayer.seekTo(0);

                    btn3.setBackgroundColor(Color.parseColor("#ffb806"));

                }else{
                    playMedia(2);
                    btn3.setBackgroundColor(Color.parseColor("#e1a206"));
                }
            }

        });


    }

    /*Recibe el indice del medio a reproducir*/
    public void playMedia(int media){

        try {

            mediaPlayer = MediaPlayer.create(getApplicationContext(), sonidos[media]);
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    Log.w(TAG, "inicia reproduccion!  ");
                    mediaPlayer.start();

                }
            });


        }
        catch (IllegalArgumentException e) {
            Log.e(TAG, "IllegalArgumentException Unable to play audio : " + e.getMessage());
        }
        catch (IllegalStateException e) {
            Log.e(TAG, "IllegalStateException Unable to play audio : " + e.getMessage());
        }

    }

}

Review this related question:

How to Play 3 audios consecutively in Android Studio?

    
answered by 07.11.2018 / 06:38
source