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

0

I've been creating a music player for a while, but I'm getting abnormal behavior only when I try on a real device. I've never had any problems with the emulator. The problem usually happens in range swapping or in pause / resume.

Could you check if I'm committing some kind of gaffe or bad practice in using 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());


            }

        });


    }}
    
asked by anonymous 01.06.2016 / 01:15

0 answers