use of android.media.MediaMetadataRetriever in project android_frameworks_base by DirtyUnicorns.
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 J2ME-Loader by nikita36078.
the class MicroPlayer method prefetch.
@Override
public void prefetch() throws MediaException {
checkClosed();
if (state == UNREALIZED) {
realize();
}
if (state == REALIZED) {
try {
player.prepare();
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(source.getLocator());
metadata.updateMetaData(retriever);
retriever.release();
state = PREFETCHED;
} catch (IOException e) {
throw new MediaException(e);
}
}
}
use of android.media.MediaMetadataRetriever in project MusicDNA by harjot-oberai.
the class ImageLoader method getBitmap.
private Bitmap getBitmap(String url) {
if (url == null) {
return BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.ic_default);
} else if (url.contains("all_playlist") || url.contains("folder")) {
return null;
} else if (url.contains("https")) {
File f = fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
//from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
} else {
File f = fileCache.getFile(url);
Bitmap b = decodeFile(f);
if (b != null)
return b;
try {
android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(url);
Bitmap bitmap = null;
byte[] data = mmr.getEmbeddedPicture();
if (data != null) {
OutputStream os = new FileOutputStream(f);
os.write(data);
os.close();
// bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
bitmap = decodeFile(f);
return bitmap;
} else {
return null;
}
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
}
use of android.media.MediaMetadataRetriever in project StylishMusicPlayer by ryanhoo.
the class FileUtils method fileToMusic.
public static Song fileToMusic(File file) {
if (file.length() == 0)
return null;
MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
metadataRetriever.setDataSource(file.getAbsolutePath());
final int duration;
String keyDuration = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
// ensure the duration is a digit, otherwise return null song
if (keyDuration == null || !keyDuration.matches("\\d+"))
return null;
duration = Integer.parseInt(keyDuration);
final String title = extractMetadata(metadataRetriever, MediaMetadataRetriever.METADATA_KEY_TITLE, file.getName());
final String displayName = extractMetadata(metadataRetriever, MediaMetadataRetriever.METADATA_KEY_TITLE, file.getName());
final String artist = extractMetadata(metadataRetriever, MediaMetadataRetriever.METADATA_KEY_ARTIST, UNKNOWN);
final String album = extractMetadata(metadataRetriever, MediaMetadataRetriever.METADATA_KEY_ALBUM, UNKNOWN);
final Song song = new Song();
song.setTitle(title);
song.setDisplayName(displayName);
song.setArtist(artist);
song.setPath(file.getAbsolutePath());
song.setAlbum(album);
song.setDuration(duration);
song.setSize((int) file.length());
return song;
}
use of android.media.MediaMetadataRetriever in project JamsMusicPlayer by psaravan.
the class AudioPlaybackService method enqueuePlaylistCursor.
/**
* This method combines the current cursor with the specified playlist cursor.
* @param newCursor
*/
public void enqueuePlaylistCursor(Cursor newCursor) {
String[] matrixCursorColumns = { DBAccessHelper.SONG_ARTIST, DBAccessHelper.SONG_ALBUM, DBAccessHelper.SONG_TITLE, DBAccessHelper.SONG_FILE_PATH, DBAccessHelper.SONG_DURATION, DBAccessHelper.SONG_GENRE, DBAccessHelper.SONG_ID, DBAccessHelper.SONG_ALBUM_ART_PATH, DBAccessHelper.SONG_SOURCE };
//Create an empty matrix getCursor() with the specified columns.
MatrixCursor mMatrixCursor = new MatrixCursor(matrixCursorColumns);
//Make a copy of the old getCursor() and copy it's contents over to the matrix getCursor().
Cursor tempCursor = getCursor();
tempCursor.moveToFirst();
MediaMetadataRetriever mMMDR = new MediaMetadataRetriever();
for (int i = 0; i < tempCursor.getCount(); i++) {
tempCursor.moveToPosition(i);
//Check which type of getCursor() the service currently has.
if (getCursor().getColumnIndex(DBAccessHelper.SONG_FILE_PATH) == -1) {
//We'll have to manually extract the info from the audio file.
/* String songFilePath = tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.PLAYLIST_SONG_FILE_PATH));
try {
mMMDR.setDataSource(songFilePath);
} catch (Exception e) {
//Skip the song if there's a problem with reading it.
continue;
}*/
String songArtist = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String songAlbum = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
String songTitle = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
String songDuration = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
String songGenre = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
mMatrixCursor.addRow(new Object[] { songArtist, songAlbum, songTitle, "", songDuration, songGenre });
} else {
mMatrixCursor.addRow(new Object[] { tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ARTIST)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ALBUM)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_TITLE)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_FILE_PATH)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_DURATION)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_GENRE)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ID)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ALBUM_ART_PATH)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_SOURCE)) });
}
}
tempCursor.close();
//Copy the contents of the new getCursor() over to the MatrixCursor.
if (newCursor.getCount() > 0) {
String songArtist = "";
String songAlbum = "";
String songTitle = "";
String filePath = "";
String songDuration = "";
for (int j = 0; j < newCursor.getCount(); j++) {
/* newCursor.moveToPosition(j);
filePath = newCursor.getString(newCursor.getColumnIndex(DBAccessHelper.PLAYLIST_SONG_FILE_PATH));
try {
mMMDR.setDataSource(filePath);
} catch (Exception e) {
continue;
}*/
//Get the metadata from the song file.
songArtist = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
songAlbum = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
songTitle = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
songDuration = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
String songGenre = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
mMatrixCursor.addRow(new Object[] { songArtist, songAlbum, songTitle, filePath, songDuration, songGenre });
}
}
mEnqueuePerformed = true;
newCursor.close();
mCursor = (Cursor) mMatrixCursor;
mMatrixCursor.close();
}
Aggregations