Example usage for android.media MediaMetadataRetriever getEmbeddedPicture

List of usage examples for android.media MediaMetadataRetriever getEmbeddedPicture

Introduction

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

Prototype

public byte[] getEmbeddedPicture() 

Source Link

Document

Call this method after setDataSource().

Usage

From source file:Main.java

public static Bitmap getAudioImage(String audioPath) {
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    Bitmap bitmap = null;//from   w w w  .j  a  v  a2  s .  c  om
    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:com.appdevper.mediaplayer.ui.PlaybackControlsFragment.java

private Bitmap downloadBitmap(String mediaId) {
    String url = MusicProvider.getInstance().getMusic(mediaId)
            .getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
    final MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
    metaRetriever.setDataSource(url, new HashMap<String, String>());
    try {//from  www. j  ava  2 s  .  c o  m
        final byte[] art = metaRetriever.getEmbeddedPicture();
        return BitmapFactory.decodeByteArray(art, 0, art.length);
    } catch (Exception e) {
        Log.d(TAG, "Couldn't create album art: " + e.getMessage());
        return BitmapFactory.decodeResource(getResources(), R.drawable.ic_default_art);
    }
}

From source file:com.nostra13.universalimageloader.core.decode.ContentImageDecoder.java

private Bitmap makeVideoThumbnailFromMediaMetadataRetriever(MediaMetadataRetriever retriever) {
    if (retriever == null)
        return null;

    Bitmap thumbnail = null;/*w  ww.j av  a  2s . c  om*/
    byte[] picture = retriever.getEmbeddedPicture();

    if (picture != null) {
        Log8.d();
        thumbnail = BitmapFactory.decodeByteArray(picture, 0, picture.length);
        if (thumbnail != null) {
            Log8.d(thumbnail.getWidth(), thumbnail.getHeight());
        }
    } else {
        Log8.d();
    }

    if (thumbnail == null) {
        Log8.d();
        thumbnail = retriever.getFrameAtTime();
        if (thumbnail != null) {
            Log8.d(thumbnail.getWidth(), thumbnail.getHeight());
        }
    }

    if (thumbnail == null) {
        Log8.d();
        return null;
    }

    return thumbnail;
}

From source file:com.appdevper.mediaplayer.app.MediaNotificationManager.java

private Bitmap downloadBitmap(String mediaId) {
    String url = MusicProvider.getInstance().getMusic(mediaId)
            .getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
    final MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
    metaRetriever.setDataSource(url, new HashMap<String, String>());
    try {//  w  ww.  j a v  a2s  . c  o  m
        final byte[] art = metaRetriever.getEmbeddedPicture();
        return BitmapFactory.decodeByteArray(art, 0, art.length);
    } catch (Exception e) {
        Log.d(TAG, "Couldn't create album art: " + e.getMessage());
        return BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_default_art);
    }
}

From source file:com.xperia64.timidityae.MusicService.java

@SuppressLint("NewApi")
public void play() {
    if (playList != null && currSongNumber >= 0) {
        shouldAdvance = false;//  w  w w. ja  v  a2 s.c om
        death = true;
        fullStop = false;
        stop();
        death = false;
        Globals.shouldRestore = true;
        while (!death && ((Globals.isPlaying == 0 || JNIHandler.alternativeCheck == 333333))) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (!death) {
            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            String tmpTitle;
            Globals.currArt = null;
            final int songIndex;
            if (shuffleMode == 1) {
                songIndex = realSongNumber = shuffledIndices.get(currSongNumber);
            } else {
                songIndex = realSongNumber = currSongNumber;
            }
            try {
                mmr.setDataSource(playList.get(songIndex));
                tmpTitle = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
                if (tmpTitle != null) {
                    if (TextUtils.isEmpty(tmpTitle))
                        tmpTitle = playList.get(songIndex)
                                .substring(playList.get(songIndex).lastIndexOf('/') + 1);
                } else {
                    tmpTitle = playList.get(songIndex).substring(playList.get(songIndex).lastIndexOf('/') + 1);
                }

            } catch (RuntimeException e) {
                tmpTitle = playList.get(songIndex).substring(playList.get(songIndex).lastIndexOf('/') + 1);
            }
            boolean goodart = false;
            if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) // Please work
            {
                try {

                    byte[] art = mmr.getEmbeddedPicture();
                    if (art != null) {
                        Globals.currArt = BitmapFactory.decodeByteArray(art, 0, art.length);
                        goodart = Globals.currArt != null;
                    }
                } catch (Exception e) {
                }
            }
            if (!goodart) {
                String goodPath = playList.get(songIndex).substring(0,
                        playList.get(songIndex).lastIndexOf('/') + 1) + "folder.jpg";
                if (new File(goodPath).exists()) {
                    try {
                        Globals.currArt = BitmapFactory.decodeFile(goodPath);
                    } catch (RuntimeException e) {
                    }
                } else {
                    // Try albumart.jpg
                    goodPath = playList.get(songIndex).substring(0,
                            playList.get(songIndex).lastIndexOf('/') + 1) + "AlbumArt.jpg";
                    if (new File(goodPath).exists()) {
                        try {
                            Globals.currArt = BitmapFactory.decodeFile(goodPath);
                        } catch (RuntimeException e) {
                            // 
                        }
                    }
                }
            }
            if (shouldDoWidget) {
                Intent intent = new Intent(this, TimidityAEWidgetProvider.class);
                intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
                // since it seems the onUpdate() is only fired on that:
                ids = AppWidgetManager.getInstance(getApplication())
                        .getAppWidgetIds(new ComponentName(getApplication(), TimidityAEWidgetProvider.class));

                //intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
                intent.putExtra("com.xperia64.timidityae.timidityaewidgetprovider.onlyart", true);
                sendBroadcast(intent);
            }
            Intent new_intent = new Intent();
            new_intent.setAction(getResources().getString(R.string.ta_rec));
            new_intent.putExtra(getResources().getString(R.string.ta_cmd), 6);
            sendBroadcast(new_intent);

            currTitle = tmpTitle;
            shouldAdvance = true;
            paused = false;

            final int x = JNIHandler.play(playList.get(songIndex));
            if (x != 0) {
                switch (x) {
                case -1:
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), getResources().getString(R.string.srv_fnf),
                                    Toast.LENGTH_SHORT).show();
                        }
                    });

                    break;
                case -3:
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Error initializing AudioTrack. Try decreasing the buffer size.",
                                    Toast.LENGTH_LONG).show();
                        }
                    });

                    break;
                case -9:
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    getResources().getString(R.string.srv_loading), Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                default:
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    String.format(getResources().getString(R.string.srv_unk), x),
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                }

                Globals.isPlaying = 1;
                JNIHandler.type = true;
                shouldAdvance = false;
                JNIHandler.paused = false;
                stop();
            } else {
                updateNotification(currTitle, paused);
                new Thread(new Runnable() {
                    public void run() {
                        while (!death && ((Globals.isPlaying == 1 && shouldAdvance))) {
                            if (JNIHandler.alternativeCheck == 555555)
                                death = true;

                            //System.out.println(String.format("alt check: %d death: %s isplaying: %d shouldAdvance: %s seekBarReady: %s",JNIHandler.alternativeCheck,death?"true":"false",Globals.isPlaying,shouldAdvance?"true":"false",JNIHandler.seekbarReady?"true":"false"));
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
                            }
                        }
                        if (!death) {
                            Intent new_intent = new Intent();
                            new_intent.setAction(getResources().getString(R.string.ta_rec));
                            new_intent.putExtra(getResources().getString(R.string.ta_cmd), 0);
                            new_intent.putExtra(getResources().getString(R.string.ta_startt),
                                    JNIHandler.maxTime);
                            new_intent.putExtra(getResources().getString(R.string.ta_songttl), currTitle);
                            new_intent.putExtra(getResources().getString(R.string.ta_filename),
                                    playList.get(songIndex));
                            new_intent.putExtra("stupidNumber", songIndex);
                            sendBroadcast(new_intent);
                        }
                        if (new File(playList.get(songIndex) + ".def.tcf").exists()
                                || new File(playList.get(songIndex) + ".def.tzf").exists()) {
                            String suffix;
                            if (new File(playList.get(songIndex) + ".def.tcf").exists()
                                    && new File(playList.get(songIndex) + ".def.tzf").exists()) {
                                suffix = (Globals.compressCfg ? ".def.tzf" : ".def.tcf");
                            } else if (new File(playList.get(songIndex) + ".def.tcf").exists()) {
                                suffix = ".def.tcf";
                            } else {
                                suffix = ".def.tzf";
                            }
                            JNIHandler.shouldPlayNow = false;
                            JNIHandler.currTime = 0;
                            while (Globals.isPlaying == 0 && !death && shouldAdvance
                                    && !JNIHandler.dataWritten) {
                                try {
                                    Thread.sleep(25);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            Intent new_intent = new Intent(); // silly, but should be done async. I think.
                            new_intent.setAction(getResources().getString(R.string.msrv_rec));
                            new_intent.putExtra(getResources().getString(R.string.msrv_cmd), 17);
                            new_intent.putExtra(getResources().getString(R.string.msrv_infile),
                                    playList.get(songIndex) + suffix);
                            new_intent.putExtra(getResources().getString(R.string.msrv_reset), true);
                            sendBroadcast(new_intent);
                        }
                        while (!death && (((Globals.isPlaying == 0 || JNIHandler.alternativeCheck == 333333)
                                && shouldAdvance))) {
                            try {
                                Thread.sleep(25);
                            } catch (InterruptedException e) {
                            }
                        }
                        if (shouldAdvance && !death) {
                            shouldAdvance = false;
                            new Thread(new Runnable() {
                                public void run() {
                                    if (playList.size() > 1
                                            && (((songIndex + 1 < playList.size() && loopMode == 0))
                                                    || loopMode == 1)) {
                                        next();
                                    } else if (loopMode == 2 || playList.size() == 1) {
                                        play();
                                    } else if (loopMode == 0) {
                                        Globals.hardStop = true;
                                        Intent new_intent = new Intent();
                                        new_intent.setAction(getResources().getString(R.string.ta_rec));
                                        new_intent.putExtra(getResources().getString(R.string.ta_cmd), 5);
                                        new_intent.putExtra(getResources().getString(R.string.ta_pause), false);
                                        sendBroadcast(new_intent);
                                    }
                                }
                            }).start();

                        }

                    }
                }).start();
            }
        }
    }
}