简体   繁体   中英

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();
    }           
}
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

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

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM