Example usage for android.media MediaMetadataRetriever METADATA_KEY_CD_TRACK_NUMBER

List of usage examples for android.media MediaMetadataRetriever METADATA_KEY_CD_TRACK_NUMBER

Introduction

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

Prototype

int METADATA_KEY_CD_TRACK_NUMBER

To view the source code for android.media MediaMetadataRetriever METADATA_KEY_CD_TRACK_NUMBER.

Click Source Link

Document

The metadata key to retrieve the numeric string describing the order of the audio data source on its original recording.

Usage

From source file:org.gateshipone.odyssey.utils.FileExplorerHelper.java

/**
 * create a TrackModel for the given File
 * if no entry in the mediadb is found a dummy TrackModel will be created
 *//*  w w  w  .ja  v  a  2 s.  co  m*/
public TrackModel getTrackModelForFile(Context context, FileModel file) {
    TrackModel track = null;

    String urlString = file.getURLString();

    if (mTrackHash.isEmpty()) {
        // lookup the current file in the media db
        String whereVal[] = { urlString };

        String where = MediaStore.Audio.Media.DATA + "=?";

        Cursor cursor = PermissionHelper.query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                MusicLibraryHelper.projectionTracks, where, whereVal, MediaStore.Audio.Media.TRACK);

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                int no = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.TRACK));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String albumKey = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_KEY));
                long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));

                track = new TrackModel(title, artist, album, albumKey, duration, no, url, id);
            }

            cursor.close();
        }
    } else {
        // use pre built hash to lookup the file
        track = mTrackHash.get(urlString);
    }

    if (track == null) {
        // no entry in the media db was found so create a custom track
        try {
            // try to read the file metadata

            MediaMetadataRetriever retriever = new MediaMetadataRetriever();

            retriever.setDataSource(context, Uri.parse(FormatHelper.encodeFileURI(urlString)));

            String title = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

            if (title == null) {
                title = file.getName();
            }

            String durationString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);

            long duration = 0;

            if (durationString != null) {
                try {
                    duration = Long.valueOf(durationString);
                } catch (NumberFormatException e) {
                    duration = 0;
                }
            }

            String noString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER);

            int no = -1;

            if (noString != null) {
                try {
                    if (noString.contains("/")) {
                        // if string has the format (trackNumber / numberOfTracks)
                        String[] components = noString.split("/");
                        if (components.length > 0) {
                            no = Integer.valueOf(components[0]);
                        }
                    } else {
                        no = Integer.valueOf(noString);
                    }
                } catch (NumberFormatException e) {
                    no = -1;
                }
            }

            String artist = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
            String album = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);

            String albumKey = "" + ((artist == null ? "" : artist) + (album == null ? "" : album)).hashCode();

            track = new TrackModel(title, artist, album, albumKey, duration, no, urlString, -1);
        } catch (Exception e) {
            String albumKey = "" + file.getName().hashCode();
            track = new TrackModel(file.getName(), "", "", albumKey, 0, -1, urlString, -1);
        }
    }

    return track;
}

From source file:com.twistedequations.rotor.MediaMetadataCompat.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private static void fillEditorKeyMapping() {
    EDITOR_KEY_MAPPING = new SparseArray<String>();
    EDITOR_KEY_MAPPING.put(MediaMetadataEditor.BITMAP_KEY_ARTWORK, METADATA_KEY_ART);
    EDITOR_KEY_MAPPING.put(MediaMetadataEditor.RATING_KEY_BY_OTHERS, METADATA_KEY_RATING);
    EDITOR_KEY_MAPPING.put(MediaMetadataEditor.RATING_KEY_BY_USER, METADATA_KEY_USER_RATING);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_ALBUM, METADATA_KEY_ALBUM);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, METADATA_KEY_ALBUM_ARTIST);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_ARTIST, METADATA_KEY_ARTIST);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_AUTHOR, METADATA_KEY_AUTHOR);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, METADATA_KEY_TRACK_NUMBER);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_COMPOSER, METADATA_KEY_COMPOSER);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_COMPILATION, METADATA_KEY_COMPILATION);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_DATE, METADATA_KEY_DATE);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER, METADATA_KEY_DISC_NUMBER);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_DURATION, METADATA_KEY_DURATION);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_GENRE, METADATA_KEY_GENRE);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS, METADATA_KEY_NUM_TRACKS);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_TITLE, METADATA_KEY_TITLE);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_WRITER, METADATA_KEY_WRITER);
    EDITOR_KEY_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_YEAR, METADATA_KEY_YEAR);
}

From source file:com.twistedequations.rotor.MediaMetadataCompat.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private static void fillEditorTypeMapping() {
    EDITOR_KEYS_TYPE = new SparseArray<Integer>(26);
    EDITOR_KEYS_TYPE.put(MediaMetadataEditor.BITMAP_KEY_ARTWORK, METADATA_TYPE_BITMAP);
    EDITOR_KEYS_TYPE.put(MediaMetadataEditor.RATING_KEY_BY_OTHERS, METADATA_TYPE_RATING);
    EDITOR_KEYS_TYPE.put(MediaMetadataEditor.RATING_KEY_BY_USER, METADATA_TYPE_RATING);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_ALBUM, METADATA_TYPE_TEXT);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, METADATA_TYPE_TEXT);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_ARTIST, METADATA_TYPE_TEXT);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_AUTHOR, METADATA_TYPE_TEXT);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, METADATA_TYPE_LONG);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_COMPOSER, METADATA_TYPE_TEXT);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_COMPILATION, METADATA_TYPE_TEXT);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_DATE, METADATA_TYPE_TEXT);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER, METADATA_TYPE_LONG);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_DURATION, METADATA_TYPE_LONG);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_YEAR, METADATA_TYPE_LONG);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_GENRE, METADATA_TYPE_TEXT);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_TITLE, METADATA_TYPE_TEXT);
    EDITOR_KEYS_TYPE.put(MediaMetadataRetriever.METADATA_KEY_WRITER, METADATA_TYPE_TEXT);
}

From source file:com.namelessdev.mpdroid.NotificationService.java

/**
 * Update the remote controls./* ww  w .ja  v  a2s  .  c  o  m*/
 *
 * @param mpdStatus The current server status object.
 */
private void updateRemoteControlClient(final MPDStatus mpdStatus) {
    final int state = getRemoteState(mpdStatus);

    mRemoteControlClient.editMetadata(true)
            .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, mCurrentMusic.getAlbum())
            .putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, mCurrentMusic.getAlbumArtist())
            .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, mCurrentMusic.getArtist())
            .putLong(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, (long) mCurrentMusic.getTrack())
            .putLong(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER, (long) mCurrentMusic.getDisc())
            .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION,
                    mCurrentMusic.getTime() * DateUtils.SECOND_IN_MILLIS)
            .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mCurrentMusic.getTitle())
            .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, mAlbumCover).apply();

    /** Notify of the elapsed time if on 4.3 or higher */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        mRemoteControlClient.setPlaybackState(state, lastKnownElapsed, 1.0f);
    } else {
        mRemoteControlClient.setPlaybackState(state);
    }
    Log.d(TAG, "Updated remote client with state " + state + " for music " + mCurrentMusic);
}