Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get total length of video in video view in android

Tags:

android

I have a problem that I want to get total length of video which is running in Video View for this I am using getDuration Method of video view but it always returns -1 when I am comparing it with currentPosition. Actually I want that if video's currentposition is equals to the total length of the video then it should start from the 0th position means starting. Mean I want to say that I want to compare Video current position with the total length of the video, how can I do that?

Thanks in advance.

Code:

videoPosition=VideoPositionFinding(IDValue,video.getDuration());
    video.seekTo(videoPosition);
    if(video.getCurrentPosition()==video.getDuration()){
        videoPosition=0;
    }
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
     System.out.println("The Video Duration: "+video.getCurrentPosition());

     video.start();
like image 598
Sanat Pandey Avatar asked Nov 01 '11 08:11

Sanat Pandey


2 Answers

To get total length of video use code:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()  {
            @Override
            public void onPrepared(MediaPlayer mp) {                         
                long duration = videoView.getDuration();
            }
        });
like image 149
Alex Kucherenko Avatar answered Nov 06 '22 05:11

Alex Kucherenko


To get length of video use the below code

myVideoView.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            int duration=mp.getDuration()/1000;
            int hours = duration / 3600; 
            int minutes = (duration / 60) - (hours * 60);
            int seconds = duration - (hours * 3600) - (minutes * 60) ;
            String formatted = String.format("%d:%02d:%02d", hours, minutes, seconds);
            Toast.makeText(getApplicationContext(), "duration is " + formatted ,  Toast.LENGTH_LONG).show();
        }
    });
like image 9
user1835052 Avatar answered Nov 06 '22 05:11

user1835052