Tran Thanh Tuan
Tran Thanh Tuan

Reputation: 23

How to get duration of video when it is recording-Android

I'm facing with a problem. I want to get duration of video when i 'm recording it. I can get duration of video when it's finish by code

MediaPlayer mp = MediaPlayer.create(mContext, Uri.parse(video));
if(mp == null)
    return -1;
int duration = mp.getDuration();
mp.release();

But i want to get duration everytime when i record video to update to progressbar. Hope your reply!

Upvotes: 1

Views: 1452

Answers (1)

Deejaygeekout
Deejaygeekout

Reputation: 242

   private class PlaybackObserver extends Thread {

        public void run() {
            currentPosition = 0;
            try {
                while (!killObserverThread) {
                    Thread.sleep(1000);

                    currentPosition = (int) mediaPlayer.getCurrentPosition();

                    runOnUiThread(new Runnable() {
                        public void run() {

                                yourProgressBar.setProgress(currentPosition);


                        }
                    });
                }
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

   @Override
    protected void onDestroy() {

        super.onDestroy();

        killObserverThread = false;
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();



        if (task == null || !task.isAlive()) {
            task = new PlaybackObserver();
            task.start();
        }

        startPlaying();

    }

add a private class for your UiThread to update your seekbar/progressbar. Then start it from onResume()

Upvotes: 1

Related Questions