Android MediaMetadataRetriever setDataSource 失敗 (Android MediaMetadataRetriever setDataSource failed)


問題描述

Android MediaMetadataRetriever setDataSource 失敗 (Android MediaMetadataRetriever setDataSource failed)

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.


參考解法

方法 1:

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

方法 2:

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

方法 3:

FileInputStream works for me.

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

(by Lukas OlsenKhairul Alam LiconKirill FeoktistovLeandro Cadete)

參考文件

  1. Android MediaMetadataRetriever setDataSource failed (CC BY‑SA 3.0/4.0)

#video #Android #media






相關問題

當前在網站上放置音頻和視頻的最佳方式是什麼? (what's the current best way to put audio and video on a web site?)

相當於PNG的視頻? (Video equivalent of PNG?)

IOS - UISaveVideoAtPathToSavedPhotosAlbum 如何返回保存的視頻路徑? (IOS -how could UISaveVideoAtPathToSavedPhotosAlbum return the saved video path?)

如何使用 c# 編寫一個簡單的視頻播放器,以準確的 fps 播放視頻? (How to use c# to write a simple video player which plays the video with accurate fps?)

Monotouch - MoviePlayer 永遠不會超越“正在加載...” (Monotouch - MoviePlayer never gets beyond "loading...")

Android MediaMetadataRetriever setDataSource 失敗 (Android MediaMetadataRetriever setDataSource failed)

枚舉Windows上所有可用視頻編解碼器的最佳方法? (Best way to enumerate all available video codecs on Windows?)

如何通過上傳單個視頻文件為任何 html5 視頻獲得多種質量(360p、480p、720p)選擇 (how can i get multiple quality(360p,480p, 720p) selection for any html5 video with uploading single video file)

由於不支持的編解碼器,FFmpeg/Avconv 無法複製字幕? (FFmpeg/Avconv unable to copy subtitles due to unsupported codec?)

嵌入 youtube 視頻響應解決方案 (embed youtube video responsive solution)

如何輕鬆識別流是視頻還是圖片【ffmpeg庫】 (How to easily recognize whether stream is video or image [ffmpeg library])

livestream.com 如何顯示存檔的視頻? (how is livestream.com displaying the archived video(s)?)







留言討論