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?
Opening the file yourself and using the FileDescriptor seems to work better on API 10:
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.
Putting the setDataSource() method in try-catch block solved the issue.
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: