use of android.media.MediaMetadataRetriever in project android_frameworks_base by ParanoidAndroid.
the class ThumbnailUtils method createVideoThumbnail.
/**
* Create a video thumbnail for a video. May return null if the video is
* corrupt or the format is not supported.
*
* @param filePath the path of video file
* @param kind could be MINI_KIND or MICRO_KIND
*/
public static Bitmap createVideoThumbnail(String filePath, int kind) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(filePath);
bitmap = retriever.getFrameAtTime(-1);
} catch (OutOfMemoryError e) {
Log.e(TAG, "Got OOM error", e);
} 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;
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 = extractThumbnail(bitmap, TARGET_SIZE_MICRO_THUMBNAIL, TARGET_SIZE_MICRO_THUMBNAIL, OPTIONS_RECYCLE_INPUT);
}
return bitmap;
}
use of android.media.MediaMetadataRetriever in project android_frameworks_base by ParanoidAndroid.
the class EffectsVideoCapture method verify.
// Verify result code, result data, and the duration.
private void verify(CameraEffectsRecordingSample activity, Uri uri) throws Exception {
assertNotNull(uri);
// Verify the video file
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(activity, uri);
String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
assertNotNull(duration);
int durationValue = Integer.parseInt(duration);
Log.v(TAG, "Video duration is " + durationValue);
assertTrue(durationValue > 0);
}
use of android.media.MediaMetadataRetriever in project android_frameworks_base by ParanoidAndroid.
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");
}
use of android.media.MediaMetadataRetriever in project android_frameworks_base by ParanoidAndroid.
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);
}
use of android.media.MediaMetadataRetriever in project android_frameworks_base by ParanoidAndroid.
the class MediaMetadataRetrieverTest method testGetEmbeddedPicture.
// Test album art extraction.
@MediumTest
public static void testGetEmbeddedPicture() throws Exception {
Log.v(TAG, "testGetEmbeddedPicture starts.");
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
boolean supportWMA = MediaProfileReader.getWMAEnable();
boolean hasFailed = false;
boolean supportWMV = MediaProfileReader.getWMVEnable();
for (int i = 0, n = MediaNames.ALBUMART_TEST_FILES.length; i < n; ++i) {
try {
Log.v(TAG, "File " + i + ": " + MediaNames.ALBUMART_TEST_FILES[i]);
if ((MediaNames.ALBUMART_TEST_FILES[i].endsWith(".wma") && !supportWMA) || (MediaNames.ALBUMART_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.ALBUMART_TEST_FILES[i]);
byte[] albumArt = retriever.getEmbeddedPicture();
// known result.
if (albumArt == null) {
// Do we have expect in JUnit?
Log.e(TAG, "Fails to get embedded picture for " + MediaNames.ALBUMART_TEST_FILES[i]);
hasFailed = true;
}
} catch (Exception e) {
Log.e(TAG, "Fails to setDataSource for " + MediaNames.ALBUMART_TEST_FILES[i]);
hasFailed = true;
}
// Don't be evil
Thread.yield();
}
retriever.release();
Log.v(TAG, "testGetEmbeddedPicture completes.");
assertTrue(!hasFailed);
}
Aggregations