Search in sources :

Example 31 with MediaMetadataRetriever

use of android.media.MediaMetadataRetriever in project android_frameworks_base by ResurrectionRemix.

the class MediaMetadataRetrieverTest method testMetadataRetrieval.

@LargeTest
public static void testMetadataRetrieval() throws Exception {
    boolean supportWMA = MediaProfileReader.getWMAEnable();
    boolean supportWMV = MediaProfileReader.getWMVEnable();
    boolean hasFailed = false;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    for (int i = 0, n = MediaNames.THUMBNAIL_METADATA_TEST_FILES.length; i < n; ++i) {
        try {
            Log.v(TAG, "File " + i + ": " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
            if ((MediaNames.THUMBNAIL_METADATA_TEST_FILES[i].endsWith(".wma") && !supportWMA) || (MediaNames.THUMBNAIL_METADATA_TEST_FILES[i].endsWith(".wmv") && !supportWMV)) {
                Log.v(TAG, "windows media is not supported and thus we will skip the test for this file");
                continue;
            }
            retriever.setDataSource(MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
            extractAllSupportedMetadataValues(retriever);
        } catch (Exception e) {
            Log.e(TAG, "Fails to setDataSource for file " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
            hasFailed = true;
        }
        // Don't be evil
        Thread.yield();
    }
    retriever.release();
    assertTrue(!hasFailed);
}
Also used : MediaMetadataRetriever(android.media.MediaMetadataRetriever)

Example 32 with MediaMetadataRetriever

use of android.media.MediaMetadataRetriever in project android_frameworks_base by ResurrectionRemix.

the class MediaMetadataRetrieverTest method testThumbnailCapture.

// Test frame capture
@LargeTest
public static void testThumbnailCapture() throws Exception {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    boolean supportWMA = MediaProfileReader.getWMAEnable();
    boolean supportWMV = MediaProfileReader.getWMVEnable();
    boolean hasFailed = false;
    Log.v(TAG, "Thumbnail processing starts");
    long startedAt = System.currentTimeMillis();
    for (int i = 0, n = MediaNames.THUMBNAIL_METADATA_TEST_FILES.length; i < n; ++i) {
        try {
            Log.v(TAG, "File " + i + ": " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
            if ((MediaNames.THUMBNAIL_METADATA_TEST_FILES[i].endsWith(".wma") && !supportWMA) || (MediaNames.THUMBNAIL_METADATA_TEST_FILES[i].endsWith(".wmv") && !supportWMV)) {
                Log.v(TAG, "windows media is not supported and thus we will skip the test for this file");
                continue;
            }
            retriever.setDataSource(MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
            Bitmap bitmap = retriever.getFrameAtTime(-1);
            assertTrue(bitmap != null);
            try {
                java.io.OutputStream stream = new FileOutputStream(MediaNames.THUMBNAIL_METADATA_TEST_FILES[i] + ".jpg");
                bitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
                stream.close();
            } catch (Exception e) {
                Log.e(TAG, "Fails to convert the bitmap to a JPEG file for " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
                hasFailed = true;
                Log.e(TAG, e.toString());
            }
        } catch (Exception e) {
            Log.e(TAG, "Fails to setDataSource for file " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
            hasFailed = true;
        }
        // Don't be evil
        Thread.yield();
    }
    long endedAt = System.currentTimeMillis();
    retriever.release();
    assertTrue(!hasFailed);
    Log.v(TAG, "Average processing time per thumbnail: " + (endedAt - startedAt) / MediaNames.THUMBNAIL_METADATA_TEST_FILES.length + " ms");
}
Also used : Bitmap(android.graphics.Bitmap) MediaMetadataRetriever(android.media.MediaMetadataRetriever) FileOutputStream(java.io.FileOutputStream)

Example 33 with MediaMetadataRetriever

use of android.media.MediaMetadataRetriever in project android_frameworks_base by ResurrectionRemix.

the class MediaMetadataRetrieverTest method testSetDataSource.

// Test setDataSource()
@MediumTest
public static void testSetDataSource() {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    boolean hasFailed = false;
    // Null pointer argument
    try {
        String path = null;
        retriever.setDataSource(path);
        Log.e(TAG, "IllegalArgumentException failed to be thrown.");
        hasFailed = true;
    } catch (Exception e) {
        if (!(e instanceof IllegalArgumentException)) {
            Log.e(TAG, "Expected a IllegalArgumentException, but got a different exception");
            hasFailed = true;
        }
    }
    // Use mem:// path
    try {
        retriever.setDataSource(MediaNames.TEST_PATH_5);
        Log.e(TAG, "IllegalArgumentException failed to be thrown.");
        hasFailed = true;
    } catch (Exception e) {
        if (!(e instanceof IllegalArgumentException)) {
            Log.e(TAG, "Expected a IllegalArgumentException, but got a different exception");
            hasFailed = true;
        }
    }
    // The pathname does not correspond to any existing file
    try {
        retriever.setDataSource(MediaNames.TEST_PATH_4);
        Log.e(TAG, "RuntimeException failed to be thrown.");
        hasFailed = true;
    } catch (Exception e) {
        if (!(e instanceof RuntimeException)) {
            Log.e(TAG, "Expected a RuntimeException, but got a different exception");
            hasFailed = true;
        }
    }
    // is not a valid media file
    try {
        retriever.setDataSource(MediaNames.TEST_PATH_3);
        Log.e(TAG, "RuntimeException failed to be thrown.");
        hasFailed = true;
    } catch (Exception e) {
        if (!(e instanceof RuntimeException)) {
            Log.e(TAG, "Expected a RuntimeException, but got a different exception");
            hasFailed = true;
        }
    }
    retriever.release();
    assertTrue(!hasFailed);
}
Also used : MediaMetadataRetriever(android.media.MediaMetadataRetriever)

Example 34 with MediaMetadataRetriever

use of android.media.MediaMetadataRetriever in project android_frameworks_base by ResurrectionRemix.

the class MediaMetadataRetrieverTest method testBasicNormalMethodCallSequence.

// If the specified call order and valid media file is used, no exception
// should be thrown.
@MediumTest
public static void testBasicNormalMethodCallSequence() throws Exception {
    boolean hasFailed = false;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(MediaNames.TEST_PATH_1);
        Bitmap bitmap = retriever.getFrameAtTime(-1);
        assertTrue(bitmap != null);
        try {
            java.io.OutputStream stream = new FileOutputStream("/sdcard/thumbnailout.jpg");
            bitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
            stream.close();
        } catch (Exception e) {
            throw new Exception("Fails to convert the bitmap to a JPEG file for " + MediaNames.TEST_PATH_1, e);
        }
        extractAllSupportedMetadataValues(retriever);
    } catch (Exception e) {
        Log.e(TAG, "Fails to setDataSource for " + MediaNames.TEST_PATH_1, e);
        hasFailed = true;
    }
    retriever.release();
    assertTrue(!hasFailed);
}
Also used : Bitmap(android.graphics.Bitmap) MediaMetadataRetriever(android.media.MediaMetadataRetriever) FileOutputStream(java.io.FileOutputStream)

Example 35 with MediaMetadataRetriever

use of android.media.MediaMetadataRetriever in project react-native-android-video-editor by RZulfikri.

the class VideoTimeline method getBitmap.

private void getBitmap(final int width) {
    layoutWidth = width;
    BackgroundExecutor.execute(new BackgroundExecutor.Task("", 0L, "") {

        @Override
        public void execute() {
            try {
                LongSparseArray<Bitmap> thumbnailList = new LongSparseArray<>();
                MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
                mediaMetadataRetriever.setDataSource(mVideoUri);
                int videoWidth = Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
                int videoHeight = Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
                int rotation = Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
                Boolean isHorizontal = false;
                if (rotation == 90 || rotation == 360) {
                    isHorizontal = false;
                }
                if (rotation == 0 || rotation == 180) {
                    if (videoHeight > videoWidth) {
                        isHorizontal = false;
                    } else {
                        isHorizontal = true;
                    }
                }
                // Retrieve media data
                long videoLengthInMs = Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) * 1000;
                mVideoLength = videoLengthInMs / 1000;
                // Set thumbnail properties (Thumbs are squares)
                final int thumbWidth = getThumbWidth(videoLengthInMs, width);
                final int thumbHeight = layoutHeight - 40;
                final long interval = getInterval(videoLengthInMs);
                final int count = getCount(videoLengthInMs);
                for (int i = 0; i < count; i++) {
                    Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime(((i * interval) + (mStartVideo * 1000L)), MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
                    try {
                        if (isHorizontal) {
                            if (count > 10 && 1 == count - 1) {
                                int minuteDurationI = (int) mVideoLength / 60000;
                                float minuteDurationF = (float) mVideoLength / 60000.0f;
                                float imageWidthLeft = minuteDurationF - minuteDurationI;
                                int xOffset = (bitmap.getWidth() - bitmap.getHeight()) / 2;
                                bitmap = Bitmap.createBitmap(bitmap, xOffset, 0, bitmap.getHeight(), bitmap.getHeight());
                                bitmap = Bitmap.createScaledBitmap(bitmap, (int) (thumbWidth * imageWidthLeft), thumbHeight, false);
                            } else {
                                int xOffset = (bitmap.getWidth() - bitmap.getHeight()) / 2;
                                bitmap = Bitmap.createBitmap(bitmap, xOffset, 0, bitmap.getHeight(), bitmap.getHeight());
                                bitmap = Bitmap.createScaledBitmap(bitmap, thumbWidth, thumbHeight, false);
                            }
                        } else {
                            if (count > 10 && 1 == count - 1) {
                                int minuteDurationI = (int) mVideoLength / 60000;
                                float minuteDurationF = (float) mVideoLength / 60000.0f;
                                float imageWidthLeft = minuteDurationF - minuteDurationI;
                                int yOffset = (bitmap.getHeight() - bitmap.getWidth()) / 2;
                                bitmap = Bitmap.createBitmap(bitmap, 0, yOffset, bitmap.getWidth(), bitmap.getWidth());
                                bitmap = Bitmap.createScaledBitmap(bitmap, (int) (thumbWidth * imageWidthLeft), thumbHeight, false);
                            } else {
                                int yOffset = (bitmap.getHeight() - bitmap.getWidth()) / 2;
                                bitmap = Bitmap.createBitmap(bitmap, 0, yOffset, bitmap.getWidth(), bitmap.getWidth());
                                bitmap = Bitmap.createScaledBitmap(bitmap, thumbWidth, thumbHeight, false);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    thumbnailList.put(i, bitmap);
                }
                mediaMetadataRetriever.release();
                returnBitmaps(thumbnailList);
            } catch (final Throwable e) {
                Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
            }
        }
    });
}
Also used : LongSparseArray(android.util.LongSparseArray) Bitmap(android.graphics.Bitmap) MediaMetadataRetriever(android.media.MediaMetadataRetriever)

Aggregations

MediaMetadataRetriever (android.media.MediaMetadataRetriever)106 Bitmap (android.graphics.Bitmap)40 IOException (java.io.IOException)16 FileOutputStream (java.io.FileOutputStream)15 BitmapFactory (android.graphics.BitmapFactory)7 TargetApi (android.annotation.TargetApi)5 File (java.io.File)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Intent (android.content.Intent)2 Paint (android.graphics.Paint)2 MediaItem (android.media.videoeditor.MediaItem)2 WritableMap (com.facebook.react.bridge.WritableMap)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 SuppressLint (android.annotation.SuppressLint)1 Notification (android.app.Notification)1 PendingIntent (android.app.PendingIntent)1 ContentResolver (android.content.ContentResolver)1 Cursor (android.database.Cursor)1