Android MediaMetadataRetriever setDataSource failed

8

I'm trying to use Android MediaMetadataRetriever to get the length of recorded videos in mp4 format - but I'm getting the exception:

07-13 13:54:32.686: E/AndroidRuntime(19790): FATAL EXCEPTION: main
07-13 13:54:32.686: E/AndroidRuntime(19790): java.lang.RuntimeException: setDataSource failed: status = 0x80000000

My code is:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(video.getMediaUrl());
            String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            long timeInmillisec = Long.parseLong(time);

And the video.getMediaUrl() path is similar to this:

file:///storage/emulated/0/Foldername/Videos/VID_20130713_135318.mp4

I don't know what i am doing wrong - anyone help me out, please! Also this code works fine for Audio Files, but not for videos.

android
video
media
asked on Stack Overflow Jul 13, 2013 by Lukas Olsen • edited May 8, 2020 by Bugs Happen

4 Answers

9
public static String getFileDuration(Context context, File file) {
    String result = null;
    MediaMetadataRetriever retriever = null;
    FileInputStream inputStream = null;

    try {
        retriever = new MediaMetadataRetriever();
        inputStream = new FileInputStream(file.getAbsolutePath());
        retriever.setDataSource(inputStream.getFD());
        long time = Long.parseLong(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
        result = String.format(context.getResources().getString(R.string.player_time_format),
        AppUtil.getPlayerMinutes(time), AppUtil.getPlayerSoconds(time));

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally{
       if (retriever != null){
           retriever.release()
       }if (inputStream != null){
           inputStream.close()
       }
    }  
    return result;
}
answered on Stack Overflow Apr 6, 2016 by Khairul Alam Licon • edited Nov 4, 2019 by HB.
2

Remove file:// from video URI before passing it to setDataSource method.

answered on Stack Overflow Sep 23, 2015 by Kirill Feoktistov
0

Kirill F.'s answer helped me fix this error.

path = path.substring(7);
mmr.setDataSource(path);
answered on Stack Overflow May 12, 2017 by Symous • edited Jun 29, 2017 by Pang
0

FileInputStream works for me.

String path = "somepath"; java.io.FileInputStream input = new FileInputStream(path); mediaMetadataRetriever.setDataSource(input.getFD());

answered on Stack Overflow Oct 6, 2018 by Leandro Cadete

User contributions licensed under CC BY-SA 3.0