5

I try to get some metadata informations from jpg file in my android app using android.media.MediaMetadataRetriever. Here is my code:

public long getDuration(String videoFilePath, Context context) {
    File file = loadVideoFile(videoFilePath);
    if (file == null) {
        return -1;
    }

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    file.setReadable(true, false);
    retriever.setDataSource(file.getAbsolutePath());
    return getDurationProperty(retriever);
}

When I call setDataSource method it throws RuntimeException:

09-10 15:22:25.576: D/PowerManagerService(486): releaseWakeLock(419aa2a0): CPU_MIN_NUM , tag=AbsListViewScroll_5.0, flags=0x400
09-10 15:22:26.481: I/HtcModeClient(12704): handler message = 4011
09-10 15:22:26.481: E/HtcModeClient(12704): Check connection and retry 9 times.
09-10 15:22:27.681: W/dalvikvm(13569): threadid=1: thread exiting with uncaught exception (group=0x40bc92d0)
09-10 15:22:27.696: E/AndroidRuntime(13569): FATAL EXCEPTION: main
09-10 15:22:27.696: E/AndroidRuntime(13569): java.lang.RuntimeException: setDataSource failed: status = 0x80000000
09-10 15:22:27.696: E/AndroidRuntime(13569):    at android.media.MediaMetadataRetriever.setDataSource(Native Method)
09-10 15:22:27.696: E/AndroidRuntime(13569):    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:66)

And strange is that this fails only on HTC One X with Android 4.2.2. Applications works fine with other devices which has other android versions (for example 4.2.1).

Edit:

wow. Maybe it is about my wrong dependency in maven:

<dependency>
    <groupId>com.google.android</groupId>
    <artifactId>android</artifactId>
    <version>4.1.1.4</version>
    <scope>provided</scope>
</dependency>

But I can't find dependency for android 4.2.2. Where I can find it?

Mirek
  • 391
  • 3
  • 8
  • 20
  • Ok, now I use android dependency for 4.2.2 and it still does not work. Have to try other solution. – Mirek Sep 23 '13 at 07:12

4 Answers4

4

Opening the file yourself and using the FileDescriptor seems to work better on API 10:

FileInputStream inputStream = new FileInputStream(file.getAbsolutePath());
retriever.setDataSource(inputStream.getFD());
inputStream.close();
Benoit
  • 1,922
  • 16
  • 25
3

MediaMetadataRetriever doesn't seem to work consistently across all versions of Android. I recommend trying FFmpegMediaMetadataRetriever (Disclaimer: it's my project). It has the same interface as MediaMetadataRetriever:

FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
retriever.setDataSource(file.getAbsolutePath());
retriever.release();
William Seemann
  • 3,440
  • 10
  • 44
  • 78
  • Thanks for answer. Is there any mvn dependency which I can use for include your lib to my project? Or any jars to direct download? – Mirek Sep 23 '13 at 06:50
  • I packaged your lib to apklib and inluded it to my project. I installed and deployed application to my devices and there is the same error as befeore when I used android.media.MediaMetadataRetriever: setDataSource(...). Is there any other way to get duration of media file using android apk? – Mirek Sep 23 '13 at 08:02
  • ...also get frame bitmap of video file? – Mirek Sep 23 '13 at 08:10
  • There is a readme file located here: https://github.com/wseemann/FFmpegMediaMetadataRetriever/blob/master/README.md. You can download the prebuilt libs from here: https://github.com/wseemann/FFmpegMediaMetadataRetriever/blob/master/fmmr-library/prebuilt-libs.tar.gz. Basically, you can just unzip these to your applications "libs" folder. You need to link the actual code as a library. – William Seemann Sep 23 '13 at 17:08
  • Also, you can test the URL using the demo application: https://github.com/wseemann/FFmpegMediaMetadataRetriever/blob/master/fmmr-demo/FMMRDemo.apk. This demo will display all metadata (including) tags and art included in the file. – William Seemann Sep 23 '13 at 17:09
1

After a lot of research + trial and error, I came to conclusion and agree with https://stackoverflow.com/a/46082355/2997806. I tried to reproduce the exception with different video file formats and out of the following video formats: AVI, MPG/MPEG, MOV, mov, mp4, m4v, flv, WMV, I noticed that AVI, MPG/MPEG, and WMV threw an exception for me every time. Better to exclude them before running the method and wrap it with a try-catch.

waseefakhtar
  • 1,373
  • 2
  • 24
  • 47
-3

Putting the setDataSource() method in try-catch block solved the issue.

try {
        metaRetriver.setDataSource(AudioPath);
       //
    } catch (Exception e) {
        //
    }