0

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)
A_rmas
  • 784
  • 2
  • 10
  • 26

2 Answers2

0

Try FFmpegMediaMetadataRetriever:

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource("some URL");
mmr.release();
William Seemann
  • 3,440
  • 10
  • 44
  • 78
0

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.

A_rmas
  • 784
  • 2
  • 10
  • 26
  • Could you point me to sample android studio 2.2 project to learn from ? I want to create a custom media controller as well .Thanks – user1788736 Nov 27 '17 at 01:07
  • check the link you will find the sample project there https://github.com/brightec/ExampleMediaController – A_rmas Feb 23 '18 at 07:44