Unable to update seekbar with Handler

1k views Asked by At

I am trying to update seekbar with respect to the progress of the song in MediaPlayer. I am using Thread to do that task.

First i have used Thred inside thread and trying to update the UI but it crashing the app and says that only original thread can attached to the view.

Then i have try to update it with handler inside the thread runnable. which works fine but it is not updating the seekbar. When i have do log then i come to know loop is not going inside my handler. I dont know where is the problem. Please help me to updating SeekBar.

Code:

private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        try {
            if ((musicPath != mIRemoteService.getPath())) {
                System.out.println("..... MUSIC CHANGE.....");
                setOrUpdateData();
                updateFragmentLayout();     

            }

            // Displaying Current Duration time/Progress

            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    try {
                        songCurrentDurationLabel.setText(""+ Utilities.milliSecondsToTimer(mIRemoteService.position()));
                        songProgressBar.setProgress((int)mIRemoteService.position());
                        System.out.println("Runnnnn.........");
                        //songProgressBar.invalidate();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                        System.out.println("Runnnnn......... EXCEPTION....");
                    }

                }
            });
            System.out.println("Runnnnn.........MAIN....");

            if (!(mIRemoteService.isPlayerRunning())) {
                btnPlay.setImageDrawable(getResources().getDrawable(R.drawable.play_now_playing));
                mHandler.removeCallbacks(mUpdateTimeTask);
                System.out.println("Runnnnn.........MAIN....IF");

            }else{
                System.out.println("Runnnnn.........MAIN....ELSE");
                mHandler.post(mUpdateTimeTask);     
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};
1

There are 1 answers

7
Goran Horia Mihail On

you can use either a handler or a thread, with thread you should make sure to post the modifications on the UI thread:

private class UpdateSeekBar extends Thread
{
    @Override
    public void run()
    {
        super.run();

        while (null != mp && mp.isPlaying() && this.isAlive())
        {
            //final int min = (mp.getCurrentPosition() / 1000) / 60;
            //final int sec = (mp.getCurrentPosition() / 1000) % 60;

            MainActivity.this.runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        songCurrentDurationLabel.setText("" + Utilities.milliSecondsToTimer(mIRemoteService.position()));
                        songProgressBar.setProgress((int) mIRemoteService.position());
                        System.out.println("Runnnnn.........");
                        // songProgressBar.invalidate();
                    }
                    catch (RemoteException e)
                    {
                        e.printStackTrace();
                        System.out.println("Runnnnn......... EXCEPTION....");
                    }
                }
            });

            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}