Music player showing strange flaws. Am I making a mistake?

Asked

Viewed 37 times

0

I’ve been creating a music player for a while, but I’m receiving abnormal behavior only when I test on a real device. I never had any kind of problem in the emulator. The problem usually happens in the track change or pause/resume.

They could check if I’m committing some kind of gaffe or bad practice in the use of Mediaplayer?

public class ThePlayerTeste extends AppCompatActivity {



    private static final String TAG = "MyActivity";
    static MediaPlayer mp;
    static File[] list;
    boolean parada = false;
    int position;

    SeekBar mSeekBar;
    ImageButton btnPlay, btnPrev, btnNext, btnMenu;


    Thread updateSeekBar;



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

        position = 0;


        File folder = new File("/mnt/extsd/playlist");
        list = gg.listFiles();
        Arrays.sort(list);

        btnPlay = (ImageButton) findViewById(R.id.playbutton);

        btnPlay.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {


                if (mp != null) {
                    if (mp.isPlaying()) {

                        btnPlay.setImageDrawable(null);

                        Drawable d = ContextCompat.getDrawable(getApplicationContext(), R.drawable.playbutton); 
                        btnPlay.setImageDrawable(d); // Define Imagem do botão para dar play.
                        mp.pause();
                    } else {

                        btnPlay.setImageDrawable(null);

                        Drawable d = ContextCompat.getDrawable(getApplicationContext(), R.drawable.pausebutton);
                        btnPlay.setImageDrawable(d);//// Define Imagem do botão para dar pausa.
                        mp.start();
                    }
                }
            }
        });




        btnNext = (ImageButton) findViewById(R.id.nextbutton);
        btnNext.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {

                mp.stop();
                mp.reset();
                position = (position + 1) % list.length;

                try {
                    mp.setDataSource(list[position].getPath());
                    mp.prepare();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                mSeekBar.setMax(mp.getDuration());
                mSeekBar.setProgress(0);
                btnPlay.setImageResource(R.drawable.pausebutton);//Define imagem do botão play para pausa.
            }
        });


        btnPrev = (ImageButton) findViewById(R.id.backbutton);
        btnPrev.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {

                mp.stop();
                mp.reset();
                position = (position - 1 < 0) ? list.length - 1 : position - 1;

                try {
                    mp.setDataSource(list[position].getPath());
                    mp.prepare();

                } catch (IllegalArgumentException e) {
                    Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
                } catch (IllegalStateException e) {
                    Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mSeekBar.setMax(mp.getDuration());
                mSeekBar.setProgress(0);
                btnPlay.setImageResource(R.drawable.pausebutton);//Define imagem do botão para o de pausa.


            }
        });

        mSeekBar = (SeekBar) findViewById(R.id.seekBar);
        updateSeekBar = new Thread() { //Thread para manter a posição da seekbar proprorcional a musica tocando.
            @Override
            public void run() {



                int currentPostion = 0;

                while (!isInterrupted()) {

                    try {


                        if (mp.isPlaying()) {
                            currentPostion = mp.getCurrentPosition();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                //Se barra de progresso chegar ao fim,  clicar no botão proximo.
                                if (Math.abs(mSeekBar.getProgress() - mSeekBar.getMax()) < 1000) {
                                    btnNext.performClick();
                                }

                            }
                        });


                    } catch (IllegalStateException e) {

                        e.printStackTrace();

                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }

                    mSeekBar.setProgress(currentPostion);


                }


            }

        };

        //Iniciar do player de musica.
        try {
            mp = null;
            mp = new MediaPlayer();
            mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
            mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();

                }
            });
            mp.setDataSource(list[position].getPath());
            mp.prepare();


        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

      /*  mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
            @Override
            public void OnCompletionListener()
        });*/

        mSeekBar.setMax(mp.getDuration());
        updateSeekBar.start();
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {


            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { //Metodo para usuario arrastar barra de progresso e musica ir pra onde foi.

                mp.seekTo(seekBar.getProgress());


            }

        });


    }}
  • But what’s the problem?

  • I randomly get fatal Exception and the program closes. I noticed that in general during breaks and when the track is advanced.

  • Give more details. Edit your asking by placing the output of Logcat.

  • Here’s the problem. I only get errors like this while running on the device. Never in the test environment. I wonder if I committed some bad practice or something similar.

  • 1

    The code is too big to look for random errors. Connect your device to your computer and you can see errors the moment they happen or even errors that occurred in the past, as long as it hasn’t been long.

  • 1

    on which line of the program the error occurred?

  • In the updateSeekbar thread : currentPostion = mp.getCurrentPosition();

  • @Thekilz can put error logcat?

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.