Doesn't gets frame from video in android 3.2.1 using MediaMetadataRetriever

I trying to get thumbnail from video using MediaMetadataRetriever class :

  MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource("/mnt/sdcard/test.ts");

            Bitmap thumbnail = retriever.getFrameAtTime();
            ivThumbnail.setImageBitmap(thumbnail);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

But all I get in my log:

E/MediaMetadataRetrieverJNI﹕ getFrameAtTime: videoFrame is a NULL pointer

But the documentation (http://developer.android.com/guide/appendix/media-formats.html) says that this format supported in android 3.0+

For now this code works for .mp4 files. Any suggestions?


This may help
get the video uri, and call this method..

public void setVideoThumbnail(Uri uri) {
    String[] projection = { BaseColumns._ID, MediaColumns.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndexOrThrow(BaseColumns._ID);
    int id = cursor.getInt(columnIndex);

    ContentResolver crThumb = getContentResolver();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    Bitmap thumbnail= MediaStore.Video.Thumbnails.getThumbnail(crThumb, id,
            MediaStore.Video.Thumbnails.MICRO_KIND, options);
    ivThumbnail.setScaleType(ScaleType.CENTER_CROP);
    ivThumbnail.setImageBitmap(thumbnail);
}

Try to extract the frame of the video with this external library. It works on API 7 and doesn´t fails so many times than the MediaMetadataRetriever class. It should be very easy to add into your project...

Hope it´s useful


I had the same error.

Check if you can extract meta data information from the video via ExtractMetadata . If not, there might be something wrong with the setDataSource() call or the video is in a wrong format/codec.

This code works for me.

AssetFileDescriptor afd=getAssets().openFd("t.mp4");
mediaMetadataRetriever.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());

I had to provide afd.getStartOffset() and afd.getLength()

Maybe it helps you get on the right track.

链接地址: http://www.djcxy.com/p/84040.html

上一篇: 在某些Android设备(版本4+)中播放视频时出现问题,但在其他设备上则不会播放

下一篇: 在Android 3.2.1中使用MediaMetadataRetriever从视频中获取帧