MediaMetadataRetriever setDataSource throws IllegalArgumentException

大憨熊 提交于 2019-11-27 22:16:29

Maybe you are running into this bug. If so try:

try {
    MediaMetadataRetriever retriever = new  MediaMetadataRetriever();
    Bitmap bmp = null;      
    retriever.setDataSource("http://www.myweb.com/myvideo.mp4", new HashMap<String, String>());
    bmp = retriever.getFrameAtTime();           
    videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
    e.printStackTrace();
}

If that doesn't work you can always try FFmpegMediaMetadataRetriever:

FFmpegMediaMetadataRetriever retriever = new  FFmpegMediaMetadataRetriever();

try {
    Bitmap bmp = null;      
    retriever.setDataSource("http://www.myweb.com/myvideo.mp4"));
    bmp = retriever.getFrameAtTime();           
    videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
    e.printStackTrace();
}

retriever.release();

In my case, I was creating a simple metadata extraction test app, so I copied a file to my phone using adb, like so:

adb push 350950598.mp4 /sdcard/Movies

but I forgot to add the read external storage directory permission in the app manifest.

Specifically:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.cool.package.name">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    ...
</manifest>

Adding those permissions fixed it for me, even for the simple file string call:

    MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
    mediaMetadataRetriever.setDataSource(movie.getPath());

And of course, if you're targeting API 23+ marshmallow then you'll have to dynamically ask for those permissions, as well.

You need to give runtime permissions if you are using Android Marshmallow or later.

Android Manifest File:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.cool.package.name">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...

Then add code for runtime permissions in your activity. After that, run your application and it should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!