Android – Show play timing, remaining time of play

androidseekbar

I use the following code to play the recorded audio file, it works fine. I have a seekbar which shows the progress of the play. I have two static text called start and end. I need to show the duration of the play (00.00 to max duration) in the start text and decrement the time from max duration to 00.00. I need to show the progressing time, remaining time… Any suggestion..

private void playAudio() {
        // TODO Auto-generated method stub
        start.setVisibility(View.VISIBLE);
        end.setVisibility(View.VISIBLE);
        Context context = getApplicationContext();
        CharSequence text1 = "Your audio will play";
        int duration = Toast.LENGTH_SHORT;
        Toast toast1 = Toast.makeText(context, text1, duration);
        toast1.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast1.show();

String newFolderName = "/ComAudioRecorder"; String extstoredir = Environment.getExternalStorageDirectory() .toString(); String filename = "/combinedrecaudio.wav"; String path1 = extstoredir + newFolderName + filename; Log.d("path1", path1); /* * String path=path2; Log.d("path",path); */ mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(path1); mPlayer.prepare(); mPlayer.start(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mSeekBar.setMax(mPlayer.getDuration()); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (mPlayer != null && mPlayer.getCurrentPosition() < mPlayer.getDuration()) { Log.d("Indide Run Method",Integer.toString(mPlayer.getCurrentPosition())); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } mSeekBar.setProgress(mPlayer.getCurrentPosition()); start.setText(Long.toString(startTime)); //end.setText(Double.toString()); } } }).start(); pos=mPlayer.getCurrentPosition(); mSeekBar.setProgress(mPlayer.getCurrentPosition()); // mPlay.setVisibility(View.INVISIBLE); pauseRecord.setVisibility(View.INVISIBLE); resumeRec.setVisibility(View.VISIBLE); /*start.setText(Double.toString(starttext)); starttext=starttext+00.01;*/ // start.setText((int) (mPlayer.getCurrentPosition()/100000.0)); // end.setText(currentTime); // start.setText(); // mDelete.setVisibility(View.INVISIBLE); }

Best Answer

you cann't update the UI in the thread.you can post them to Runnable.

in the run method() put the following code.

mHandler.post(updateUI);

then use handler to update the ui

 private final Runnable updateUI= new Runnable() 
        {
            public void run() 
            {
                try 
                {
                    //update ur ui here     
                      start.setText((mPlayer.getCurrentPosition()/mPlayer.getDuration())*100); ‌​ end.setText(mPlayer.getDuration()-mPlayer.getCurrentPosition())

                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        };
        private final Handler mHandler = new Handler();
Related Topic