0

I have a mediaplayer service in which i have a Runnable which updates the seekbar and currentposition TextView every 100 milliseconds

private Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            long totalDuration = mediaPlayer.getDuration();
            long currentDuration = mediaPlayer.getCurrentPosition();

           seekBar.setProgress(progress);
           nowduration.setText("" + utils.milliSecondsToTimer(currentDuration));
           totalduration.setText(""+utils.milliSecondsToTimer(totalDuration));

            // Running this thread after 100 milliseconds
            mHandler.postDelayed(this, 100);
        }
    };

The problem is, what after some time (5-10 seconds) Activity stops, and the only error I see in logcat is this. While removing nowduration.setText all works fine, seekbar is updating without any problem, why with TextView activity stops?

04-10 10:22:40.610      288-813/system_process I/ActivityManager﹕ Process com.package.appname (pid 3734) has died.
04-10 10:22:40.610     288-3633/system_process I/WindowManager﹕ WIN DEATH: Window{424b5708 com.package.appname/com.package.appname.MainActivity paused=false}

What I tried: 1. using runOnUithread on TextView.setText - same behaviour 2. Using a Thread instead of Handler - but I have problem stoping the Thread, sometimes I need to call Handler.removeCallBacks, but as I know stoping a thread is a very bad action, this is why Thread.stop( was depreciated).

artouiros
  • 3,947
  • 12
  • 41
  • 54
  • are you using a thread other than the handler? you can use `Handler.removeCallBacks`. I think you have misunderstood use of handler http://developer.android.com/reference/android/os/Handler.html and do post the full stacktrace – Raghunandan Apr 10 '14 at 07:36

2 Answers2

1

The UserInterface can only be updated by the UI Thread. You need a Handler, to post to the UI Thread:

private void startTimerThread() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
    private long startTime = System.currentTimeMillis();
    public void run() {

            try {
                Thread.sleep(1000);
            }    
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            handler.post(new Runnable(){
                public void run() {
        long totalDuration = mediaPlayer.getDuration();
        long currentDuration = mediaPlayer.getCurrentPosition();

       seekBar.setProgress(progress);
       nowduration.setText("" + utils.milliSecondsToTimer(currentDuration));
       totalduration.setText(""+utils.milliSecondsToTimer(totalDuration));


            }
        });
        }

};
new Thread(runnable).start();
}
Irshad Khan
  • 794
  • 6
  • 21
  • 1
    I don't see a background thread in op's code. Op is using a Handler. `mHandler.postDelayed(this, 100);`. If you create a handler in the ui thread its associated with the same. Quoting **When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.** – Raghunandan Apr 10 '14 at 07:39
  • Hmm... it looks like Android OS Bug... Maybe this post can help you http://stackoverflow.com/a/17955940/1145140 – Roman Black Apr 10 '14 at 08:04
0

Try to use Handler messages to do that:

private Handler handler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg); 

        switch(msg.what){
            case 1:{
                long totalDuration = mediaPlayer.getDuration();
                long currentDuration = mediaPlayer.getCurrentPosition();

                seekBar.setProgress(progress);
                nowduration.setText("" + utils.milliSecondsToTimer(currentDuration));
                totalduration.setText(""+utils.milliSecondsToTimer(totalDuration));

                // Running this thread after 100 milliseconds
                mHandler.sendEmptyMessageDelayed(1, 100);
            }break;
        }
    }

};

And first update start:

...
handler.sendEmptyMessage(1);
...
Roman Black
  • 3,501
  • 1
  • 22
  • 31
  • I don't know why, but this givs me the same behaviour as my code. Activity just closes. I think it's a memory leak, but dunno why. Maybe this some trouble with my phone? – artouiros Apr 10 '14 at 07:49
  • Does your player complete playing before you error causes? – Roman Black Apr 10 '14 at 07:51
  • No, activity stops and media player service stops too. The problem is in textView, because while removing TextView.setText() your code works just fine. – artouiros Apr 10 '14 at 07:59