Search code examples
androidandroid-mediaplayerandroid-videoviewmediametadataretriever

android video show total duration without using media controller


I am trying to get duration of video being played as i am creating my custom media controller.When I use default mediacontroller there seem no problem as it shows all the details.I want to create my own mediacontroller only to add some graphics in my app. I could not get total duration of my online video using any of the following codes. Some experts please help.

    mVideoView.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {

            long duration1 = mp.getDuration();

            Log.d("DURATION of media player", duration1+"");

            Log.d("Videoview duration", mVideoView.getDuration()+"");

            int msec = MediaPlayer.create(getApplicationContext(),video).getDuration();

            Log.d("Duration of msec", msec+"");

            try{
                //this block not working ... why???
                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//video = uri.parse(String myurl)
                retriever.setDataSource(getApplicationContext(),video);   

                String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                long timeInmillisec = Long.parseLong( time );

                long duration = timeInmillisec / 1000;
                long hours = duration / 3600;
                long minutes = (duration - hours * 3600) / 60;
                long seconds = duration - (hours * 3600 + minutes * 60);
                finalTime = timeInmillisec;
                Log.d("HH:MM:SS", hours+":"+minutes+":"+seconds+""); 

            }catch(IllegalArgumentException e){
                Log.d(TAG, "media meta retriever error");
                e.printStackTrace();
                Log.d(TAG, e.getMessage()+"");

            }

my last attempt always returned illegal argument exception though the url is played properly even using vlc media player I tried different ways available for it e.g

retriever.setDataSource (String path)
retriever.setDataSource (Context context, Uri uri)

Solution

  • I finally got my solution following custom-android-media-controller as I got to change whole UI, I needn't need to get total duration which I needed to show in my custom media-controller.