Example usage for android.media MediaMetadataRetriever setDataSource

List of usage examples for android.media MediaMetadataRetriever setDataSource

Introduction

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

Prototype

public void setDataSource(MediaDataSource dataSource) throws IllegalArgumentException 

Source Link

Document

Sets the data source (MediaDataSource) to use.

Usage

From source file:Main.java

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

From source file:Main.java

public static long getVideoDurationInMillis(String videoFile) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(videoFile);
    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    long timeInmillisec = Long.parseLong(time);
    retriever.release();/* w w w.j  ava  2  s . c  om*/
    return timeInmillisec;
}

From source file:Main.java

public static Bitmap getVideoScreenshotBitmap(Context context, String videoPath) {
    if (TextUtils.isEmpty(videoPath) || !new File(videoPath).exists()) {
        return null;
    }//from  w w  w . j av a 2 s .co m
    Bitmap bitmap = null;
    MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
    mediaMetadataRetriever.setDataSource(videoPath);
    bitmap = mediaMetadataRetriever.getFrameAtTime(0);
    return bitmap;
}

From source file:Main.java

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

    try {//from   w  w  w  .j  a  va  2 s .co 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

public static Bitmap getVideoThumbnail(String filePath) {
    Bitmap bitmap = null;/*from  w  w w.  j  a  va  2s .co 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;//from www .  jav a 2s.  c  om
    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 getAudioImage(String audioPath) {
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    Bitmap bitmap = null;/*  w  w w  .  j  a v  a  2s.  co  m*/
    try {
        mmr.setDataSource(audioPath);
        byte[] bb = mmr.getEmbeddedPicture();
        bitmap = BitmapFactory.decodeByteArray(bb, 0, bb.length);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static MediaMetadataRetriever initMetadataRetriever(String URI) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    if (!URI.contains("http:") && URI.endsWith("mp3")) {
        try {/*  ww  w  .jav a 2  s.c  o  m*/
            retriever.setDataSource(URI);
        } catch (Exception e) {
            Log.d(LOG_TAG, "Failed: " + URI + " " + e.toString());
            e.printStackTrace();
            return null;
        }
    } else {
        return null;
    }
    return retriever;
}

From source file:Main.java

@SuppressLint("NewApi")
public static Bitmap createVideoThumbnail(String filePath, int kind) {
    Bitmap bitmap = null;//from ww w.  j  a v  a 2 s  .  c o  m
    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:com.nostra13.universalimageloader.core.decode.ContentImageDecoder.java

private static MediaMetadataRetriever getMediaMetadataRetriever(String filePath) {
    if (TextUtils.isEmpty(filePath))
        return null;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(filePath);
    return retriever;
}