0

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();
        }
    }
};
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • Do you get any kind of error? If not put some logs and see what is the flow of the code, if it gets stuck somewhere or does not enter an if statement – Goran Horia Mihail Dec 03 '14 at 06:18
  • @GoranHoriaMihail if i use Thread in place of the Handler then i got error " Only the original thread that created a view hierarchy can touch its views." – Shreyash Mahajan Dec 03 '14 at 06:45
  • Yes, you can not touch any views from other thread other than the UI thread. Handler will post modifications on the UI thread thats why it works and yes you should use handler. What is the error / problem when you use handler now? – Goran Horia Mihail Dec 03 '14 at 06:59
  • @GoranHoriaMihail the problem is that, my seekBar is not updating the progress. And then i have put log to check, i came to know that the thread is not calling as it should be... I am getting log only once.. then nothing happen... – Shreyash Mahajan Dec 03 '14 at 07:02
  • @GoranHoriaMihail can we talk over chat on SO ? – Shreyash Mahajan Dec 03 '14 at 07:07
  • sure, how do you do that? do you have a link to the chat? – Goran Horia Mihail Dec 03 '14 at 07:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66101/discussion-between-goran-horia-mihail-and-idroid-explorer). – Goran Horia Mihail Dec 03 '14 at 08:46

1 Answers1

3

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();
            }
        }
    }
}
Goran Horia Mihail
  • 3,536
  • 2
  • 29
  • 40
  • Thanks for the answer. can you please let me know how to make it smooth. I mean right now it is updating in every 1 seconds. Now i remove Thread.Sleep(1000) then it will runn very fast and smooth and with that it hangs the other UI also. – Shreyash Mahajan Dec 03 '14 at 10:25
  • Now the seekbar is updates 1 per second. you can try with Thread.Sleep(500) and see is better. – Goran Horia Mihail Dec 03 '14 at 10:30
  • your answer is perfect working and updating the progress bar. but there is something happening which heavy worked on UI thread. because its effecting on my other UI part. I am not able to work smooth if the Player is in Play condition. if the Player is in pause condition means no thread running at that time then all UI works fine in that case.... do you know what is causing that issue...? – Shreyash Mahajan Dec 04 '14 at 12:40
  • I would say don't try to update the progress bar too often. it can hang the ui thread. A Thread.sleep() between 500 - 1000 milliseconds should be safe. What value are you using? – Goran Horia Mihail Dec 04 '14 at 12:45
  • thanks for your support. right now i am passing 50 just for test but even it is blocking my UI thread. Before it was running smooth. I just want to know that which other thread is running on UI. Thanks again... – Shreyash Mahajan Dec 05 '14 at 05:10
  • yes i will do. but i have not done yet because it is effecting my app. And my app become hang with that.... need more proper solution of it. – Shreyash Mahajan Dec 08 '14 at 12:58
  • ok, hopefully you will find a better solution, not just equivalent but better. Please write it as an answer, I'm interested in it as well. – Goran Horia Mihail Dec 13 '14 at 07:03