Java Code Examples for android.media.MediaMetadataRetriever#getEmbeddedPicture()

The following examples show how to use android.media.MediaMetadataRetriever#getEmbeddedPicture() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlayActivity.java    From AudioAnchor with GNU General Public License v3.0 7 votes vote down vote up
void setAlbumCover() {
    // Set the album cover to the ImageView and the album title to the TextView
    Point size = new Point();
    WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    Display display;
    int reqSize = getResources().getDimensionPixelSize(R.dimen.default_device_width);
    if (wm != null) {
        display = wm.getDefaultDisplay();
        display.getSize(size);
        reqSize = java.lang.Math.max(size.x, size.y);
    }

    if (mCoverFromMetadata) {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(mAudioFile.getPath());

        byte[] coverData = mmr.getEmbeddedPicture();

        // Convert the byte array to a bitmap
        if (coverData != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(coverData, 0, coverData.length);
            mCoverIV.setImageBitmap(bitmap);
        } else {
            BitmapUtils.setImage(mCoverIV, mAudioFile.getCoverPath(), reqSize);
        }
    } else {
        BitmapUtils.setImage(mCoverIV, mAudioFile.getCoverPath(), reqSize);
    }

    mCoverIV.setAdjustViewBounds(true);

}
 
Example 2
Source File: AudioFileCoverFetcher.java    From Phonograph with GNU General Public License v3.0 7 votes vote down vote up
@Override
public InputStream loadData(final Priority priority) throws Exception {

    final MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(model.filePath);
        byte[] picture = retriever.getEmbeddedPicture();
        if (picture != null) {
            stream = new ByteArrayInputStream(picture);
        } else {
            stream = AudioFileCoverUtils.fallback(model.filePath);
        }
    } finally {
        retriever.release();
    }

    return stream;
}
 
Example 3
Source File: Playable.java    From AntennaPodSP with MIT License 6 votes vote down vote up
@Override
public InputStream openImageInputStream() {
    if (playable.localFileAvailable()
            && playable.getLocalMediaUrl() != null) {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        try {
            mmr.setDataSource(playable.getLocalMediaUrl());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return null;
        }
        byte[] imgData = mmr.getEmbeddedPicture();
        if (imgData != null) {
            return new PublicByteArrayInputStream(imgData);

        }
    }
    return null;
}
 
Example 4
Source File: PreviewMediaFragment.java    From Cirrus_depricated with GNU General Public License v2.0 6 votes vote down vote up
/**
 * tries to read the cover art from the audio file and sets it as cover art.
 *
 * @param file audio file with potential cover art
 */
private void extractAndSetCoverArt(OCFile file) {
    if (file.isAudio()) {
        try {
            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            mmr.setDataSource(file.getStoragePath());
            byte[] data = mmr.getEmbeddedPicture();
            if (data != null) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                mImagePreview.setImageBitmap(bitmap); //associated cover art in bitmap
            } else {
                mImagePreview.setImageResource(R.drawable.logo);
            }
        } catch (Throwable t) {
            mImagePreview.setImageResource(R.drawable.logo);
        }
    }
}
 
Example 5
Source File: AudioCoverUtil.java    From MediaLoader with Apache License 2.0 6 votes vote down vote up
/**
 * get audio cover
 * @param filePath
 * @return
 */
public static Bitmap createAlbumArt(String filePath) {
    Bitmap bitmap = null;
    //能够获取多媒体文件元数据的类
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(filePath); //设置数据源
        byte[] bytes = retriever.getEmbeddedPicture(); //得到字节型数据
        if(bytes!=null){
            bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); //转换为图片
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return bitmap;
}
 
Example 6
Source File: AudioFileCoverFetcher.java    From MusicPlayer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(model.filePath);
        byte[] picture = retriever.getEmbeddedPicture();
        if (picture != null) {
            callback.onDataReady(new ByteArrayInputStream(picture));
        } else {
            try {
                callback.onDataReady(fallback(model.filePath));
            } catch (FileNotFoundException e) {
                callback.onLoadFailed(e);
            }
        }
    } finally {
        retriever.release();
    }
}
 
Example 7
Source File: AudioFileCoverFetcher.java    From Music-Player with GNU General Public License v3.0 6 votes vote down vote up
@Override
public InputStream loadData(final Priority priority) throws Exception {

    final MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(model.filePath);
        byte[] picture = retriever.getEmbeddedPicture();
        if (picture != null) {
            stream = new ByteArrayInputStream(picture);
        } else {
            stream = AudioFileCoverUtils.fallback(model.filePath);
        }
    } finally {
        retriever.release();
    }
    return stream;
}
 
Example 8
Source File: FilmstripItemUtils.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the thumbnail of a video.
 *
 * @param path The path to the video file.
 * @return {@code null} if the loading failed.
 */
public static Bitmap loadVideoThumbnail(String path)
{
    Bitmap bitmap = null;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try
    {
        retriever.setDataSource(path);
        byte[] data = retriever.getEmbeddedPicture();
        if (data != null)
        {
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        }
        if (bitmap == null)
        {
            bitmap = retriever.getFrameAtTime();
        }
    } catch (IllegalArgumentException e)
    {
        Log.e(TAG, "MediaMetadataRetriever.setDataSource() fail:" + e.getMessage());
    }
    retriever.release();
    return bitmap;
}
 
Example 9
Source File: SongBroadCast.java    From YTPlayer with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Void doInBackground(Void... voids) {
    File f = new File(MusicService.videoID);
    Uri uri = Uri.fromFile(f);
    try {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(context,uri);
        String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        String title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

        byte [] data = mmr.getEmbeddedPicture();

        if(data != null)
            MusicService.bitmapIcon = BitmapFactory.decodeByteArray(data, 0, data.length);
        else
            MusicService.bitmapIcon = YTutils.drawableToBitmap(ContextCompat.getDrawable(context,R.drawable.ic_pulse));

        if (artist==null) artist ="Unknown artist";
        if (title==null) title = YTutils.getVideoTitle(f.getName());

        if (title.contains("."))
            title = title.split("\\.")[0];

        MusicService.videoTitle = title;
        MusicService.channelTitle = artist;
        MusicService.likeCounts = -1; MusicService.dislikeCounts = -1;
        MusicService.viewCounts = "-1";

        MusicService.total_seconds = Integer.parseInt(durationStr);

    }catch (Exception e) {
        // TODO: Do something when cannot played...
    }
    return null;
}
 
Example 10
Source File: AudioFileCoverFetcher.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
@Override
public InputStream loadData(Priority priority) throws Exception {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(model.filePath);
        byte[] picture = retriever.getEmbeddedPicture();
        if (picture != null) {
            return new ByteArrayInputStream(picture);
        } else {
            return fallback(model.filePath);
        }
    } finally {
        retriever.release();
    }
}
 
Example 11
Source File: AudioFileCoverFetcher.java    From RetroMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public InputStream loadData(Priority priority) throws Exception {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(model.filePath);
        byte[] picture = retriever.getEmbeddedPicture();
        if (picture != null) {
            return new ByteArrayInputStream(picture);
        } else {
            return fallback(model.filePath);
        }
    } finally {
        retriever.release();
    }
}
 
Example 12
Source File: PlayerActivity2.java    From YTPlayer with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Void doInBackground(Void... voids) {
    File f = new File(filePath);
    Uri uri = Uri.fromFile(f);
    try {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(activity,uri);
        String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        String title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

        byte [] data = mmr.getEmbeddedPicture();

        if (artist==null) artist ="Unknown artist";
        if (title==null) title = YTutils.getVideoTitle(f.getName());

        if (title.contains("."))
            title = title.split("\\.")[0];

        MusicService.videoTitle = title;
        MusicService.channelTitle = artist;
        MusicService.likeCounts = -1; MusicService.dislikeCounts = -1;
        MusicService.viewCounts = "-1";

        MusicService.total_seconds = Integer.parseInt(durationStr);

    }catch (Exception e) {
        // TODO: Do something when cannot played...
    }
    return null;
}
 
Example 13
Source File: YTutils.java    From YTPlayer with GNU General Public License v3.0 5 votes vote down vote up
public static Bitmap getArtworkFromFile(Activity activity, File f) {
    try {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(activity, Uri.fromFile(f));

        byte[] data = mmr.getEmbeddedPicture();

        if (data != null) {
            return BitmapFactory.decodeByteArray(data, 0, data.length);
        }
    } catch (Exception e) {
        e.getMessage();
    }
    return null;
}
 
Example 14
Source File: AudioCoverFetcher.java    From glide-support with The Unlicense 5 votes vote down vote up
@Override public InputStream loadData(Priority priority) throws Exception {
	MediaMetadataRetriever retriever = new MediaMetadataRetriever();
	try {
		retriever.setDataSource(model.path);
		byte[] picture = retriever.getEmbeddedPicture();
		if (picture != null) {
			return new ByteArrayInputStream(picture);
		} else {
			return fallback(model.path);
		}
	} finally {
		retriever.release();
	}
}
 
Example 15
Source File: MediaUtil.java    From Aria with Apache License 2.0 5 votes vote down vote up
/**
 * 获取音频封面
 */
public static Bitmap getArtwork(Context context, String url) {
  Uri selectedAudio = Uri.parse(url);
  MediaMetadataRetriever myRetriever = new MediaMetadataRetriever();
  myRetriever.setDataSource(context, selectedAudio); // the URI of audio file
  byte[] artwork;
  artwork = myRetriever.getEmbeddedPicture();
  if (artwork != null) {
    return BitmapFactory.decodeByteArray(artwork, 0, artwork.length);
  }
  return null;
}
 
Example 16
Source File: MediaUtil.java    From Aria with Apache License 2.0 5 votes vote down vote up
public static byte[] getArtworkAsByte(Context context, String url) {
  Uri selectedAudio = Uri.parse(url);
  MediaMetadataRetriever myRetriever = new MediaMetadataRetriever();
  myRetriever.setDataSource(context, selectedAudio); // the URI of audio file
  byte[] artwork;
  artwork = myRetriever.getEmbeddedPicture();
  if (artwork != null) {
    return artwork;
  }
  return null;
}
 
Example 17
Source File: MusicDetailFragment.java    From FlowingPager with Apache License 2.0 5 votes vote down vote up
private void loadingCover() {
    MediaMetadataRetriever mediaMetadataRetriever=new MediaMetadataRetriever();
    mediaMetadataRetriever.setDataSource(fd.getFileDescriptor(),
            fd.getStartOffset(),fd.getLength());
    byte[] picture = mediaMetadataRetriever.getEmbeddedPicture();
    Bitmap resource= BitmapFactory.decodeByteArray(picture,0,picture.length);
    albumImage.setImageBitmap(resource);
}
 
Example 18
Source File: NPlaylistActivity.java    From YTPlayer with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Void doInBackground(Void... voids) {
    if (MusicService.localPlayBack) {
        int i=0;
        final int totalSize = MusicService.yturls.size();
        for (String url: MusicService.yturls) {
            File f = new File(url);
            Uri uri = Uri.fromFile(f);
            try {
                MediaMetadataRetriever mmr = new MediaMetadataRetriever();
                mmr.setDataSource(NPlaylistActivity.this,uri);
                String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
                String title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

                byte [] data = mmr.getEmbeddedPicture();

                Bitmap icon;

                if(data != null)
                    icon = BitmapFactory.decodeByteArray(data, 0, data.length);
                else
                    icon = YTutils.drawableToBitmap(ContextCompat.getDrawable(NPlaylistActivity.this,
                            R.drawable.ic_pulse));

                if (artist==null) artist ="Unknown artist";
                if (title==null) title = YTutils.getVideoTitle(f.getName());

                if (title.contains("."))
                    title = title.split("\\.")[0];

                MetaModel model = new MetaModel(url,title,artist,null);
                YTMeta meta = new YTMeta(model);
                if (MusicService.videoID.equals(url))
                    models.add(new NPlayModel(url,meta,true));
                else  models.add(new NPlayModel(url,meta,false));

                publishProgress((float)((float)i*(float)100.0/(float)(totalSize)));

                models.get(models.size()-1).setIcon(icon);
                i++;

            }catch (Exception e) {
                // TODO: Do something when cannot played...
            }
        }
    }
    return null;
}