Example usage for android.media MediaMetadataRetriever release

List of usage examples for android.media MediaMetadataRetriever release

Introduction

In this page you can find the example usage for android.media MediaMetadataRetriever release.

Prototype

public native void release();

Source Link

Document

Call it when one is done with the object.

Usage

From source file:Main.java

public static Bitmap getVideoScreenShot(String filePath) {
    MediaMetadataRetriever media = new MediaMetadataRetriever();
    media.setDataSource(filePath);//from   w  ww. j a  v a  2s  . c  o  m
    Bitmap bitmap = media.getFrameAtTime();
    media.release();
    return bitmap;
}

From source file:Main.java

public static long getVideoDurationInMillis(String videoFile) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(videoFile);/*w  w  w  . j  av a2s.c  o m*/
    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    long timeInmillisec = Long.parseLong(time);
    retriever.release();
    return timeInmillisec;
}

From source file:Main.java

public static String getMetadataInfo(MediaMetadataRetriever retriever, int tag) {
    String value;//  w  w w  .j a  v a2 s  . c o  m
    if (tag != -1) {
        value = retriever.extractMetadata(tag);
        Log.d(LOG_TAG, "Return metadata with tag : " + tag + " value: " + value);
        return value;
    }
    retriever.release();
    return null;
}

From source file:Main.java

public static Bitmap createVideoThumbnail(String filePath) {
    Bitmap bitmap = null;/*from w  w  w.  j a va  2s .  c  o  m*/

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        bitmap = android.media.ThumbnailUtils.createVideoThumbnail(filePath,
                MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
    } catch (IllegalArgumentException ex) {
    } catch (RuntimeException ex) {
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap getVideoThumbnail(String filePath) {
    Bitmap bitmap = null;// ww  w  .ja  v a2  s . c o  m
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(filePath);
        bitmap = retriever.getFrameAtTime();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap extractFromMediaMetadataRetriever(String filePath) {
    Bitmap bitmap = null;//ww  w.  j a  v a 2s  .  com
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(filePath);
        bitmap = retriever.getFrameAtTime(-1);
        return bitmap;
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
            // Ignore failures while cleaning up.
        }
    }
    return null;
}

From source file:Main.java

public static Bitmap getVideoFrame(String videoPath, long frameTime) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    try {/*from   w w  w. jav  a2  s.c  o  m*/
        retriever.setDataSource(videoPath);
        return retriever.getFrameAtTime(frameTime, MediaMetadataRetriever.OPTION_CLOSEST);
    } catch (IllegalArgumentException ex) {
        Log.w("FFMPEG.MediaUtils", "illegal argument exception");

    } catch (RuntimeException ex) {
        Log.w("FFMPEG.MediaUtils", "error getting video frame");
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }
    return null;
}

From source file:Main.java

@SuppressLint("NewApi")
public static Bitmap createVideoThumbnail(String filePath, int kind) {
    Bitmap bitmap = null;//from  ww  w . ja  va  2s  .  com
    if (Build.VERSION.SDK_INT < 10) {
        // This peace of code is for compatibility with android 8 and 9.
        return android.media.ThumbnailUtils.createVideoThumbnail(filePath, kind);
    }

    // MediaMetadataRetriever is not available for Android version less than 10
    // but we need to use it in order to get first frame of the video for thumbnail.
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    try {
        retriever.setDataSource(filePath);
        bitmap = retriever.getFrameAtTime(0);
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
            // Ignore failures while cleaning up.
            Log.w("ThumbnailUtils", "MediaMetadataRetriever failed with exception: " + ex);
        }
    }

    if (bitmap == null)
        return null;

    if (kind == Images.Thumbnails.MINI_KIND) {
        // Scale down the bitmap if it's too large.
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int max = Math.max(width, height);
        if (max > 512) {
            float scale = 512f / max;
            int w = Math.round(scale * width);
            int h = Math.round(scale * height);
            bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
        }
    } else if (kind == Images.Thumbnails.MICRO_KIND) {

        bitmap = android.media.ThumbnailUtils.extractThumbnail(bitmap, TARGET_SIZE_MICRO_THUMBNAIL,
                TARGET_SIZE_MICRO_THUMBNAIL, OPTIONS_RECYCLE_INPUT);
    }

    return bitmap;
}

From source file:avreye.mytarotadvisor.utils.Util.java

public static Bitmap getThumbnailfromVideoURL(String videoPath) throws Throwable {
    Bitmap bitmap = null;//from ww  w  .j  ava  2s.co m

    MediaMetadataRetriever mediaMetadataRetriever = null;
    try {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);
        //   mediaMetadataRetriever.setDataSource(videoPath);
        bitmap = mediaMetadataRetriever.getFrameAtTime(100);
    } catch (Exception e) {

        e.printStackTrace();
        throw new Throwable("Exception in retriveVideoFrameFromVideo(String videoPath)" + e.getMessage());

    } finally {
        if (mediaMetadataRetriever != null) {
            mediaMetadataRetriever.release();
        }
    }
    return bitmap;
}

From source file:com.almalence.googsharing.Thumbnail.java

private static Bitmap createVideoThumbnail(String filePath, FileDescriptor fd, int targetWidth) {
    Bitmap bitmap = null;/*from  w  ww  . j  av a2  s.  c o m*/
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        if (filePath != null) {
            retriever.setDataSource(filePath);
        } else {
            retriever.setDataSource(fd);
        }
        bitmap = retriever.getFrameAtTime(-1);
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
            // Ignore failures while cleaning up.
        }
    }
    if (bitmap == null)
        return null;

    // Scale down the bitmap if it is bigger than we need.
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width > targetWidth) {
        float scale = (float) targetWidth / width;
        int w = Math.round(scale * width);
        int h = Math.round(scale * height);
        bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
    }
    return bitmap;
}