Ahmad Arslan
Ahmad Arslan

Reputation: 4528

How do I find out the duration of video without mediaPlayer?

Actually I'm trying to find out the duration of video from URl but it throws null pointer exception

try{
    File file = new File("http://84.--.--.--:8--9/"+filename);
    if ( file.toString().endsWith(".jpg")) {
            //photo
        } else if (file.toString().endsWith(".mp4")) {
            //video
            long mills = MediaPlayer.create(UploadFileService.this, Uri.parse(file.getAbsolutePath().toString())).getDuration();                                
            Log.d("duration", "" + mills);
        }                        
    }catch(Exception e) {
        e.printStackTrace();
    }           
}

Upvotes: 1

Views: 5343

Answers (4)

William Seemann
William Seemann

Reputation: 3530

Use FFmpegMediaMetadataRetriever:

import wseemann.media.FFmpegMediaMetadataRetriever;

...

FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();

retriever.setDataSource("some URL");
String time = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
int duration = Integer.parseInt(time);
retriever.release();

Upvotes: 1

Harsh Parikh
Harsh Parikh

Reputation: 3845

MediaMetadataRetriever retriever = new MediaMetadataRetriever();

retriever.setDataSource(intent.getStringExtra("fileName"));
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
int VideoDuration = Integer.parseInt(time);// This will give time in millesecond

Upvotes: 3

Salmaan
Salmaan

Reputation: 3624

You should try this,
I hope this works.

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(File.getPath()); // Enter Full File Path Here
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong(time); // You will get time in milliseconds

Upvotes: 0

rscnt
rscnt

Reputation: 1005

You cloud try using the class MediaMetadataRetriever, you just need to set a datasource to an instace of it, then check for the information you're looking for.

Upvotes: 0

Related Questions