Example usage for android.media MediaMetadata METADATA_KEY_DURATION

List of usage examples for android.media MediaMetadata METADATA_KEY_DURATION

Introduction

In this page you can find the example usage for android.media MediaMetadata METADATA_KEY_DURATION.

Prototype

String METADATA_KEY_DURATION

To view the source code for android.media MediaMetadata METADATA_KEY_DURATION.

Click Source Link

Document

The duration of the media in ms.

Usage

From source file:com.example.android.mediabrowserservice.model.MusicProvider.java

private MediaMetadata buildFromJSON(JSONObject json, String basePath) throws JSONException {
    String title = json.getString(JSON_TITLE);
    String album = json.getString(JSON_ALBUM);
    String artist = json.getString(JSON_ARTIST);
    String genre = json.getString(JSON_GENRE);
    String source = json.getString(JSON_SOURCE);
    String iconUrl = json.getString(JSON_IMAGE);
    int trackNumber = json.getInt(JSON_TRACK_NUMBER);
    int totalTrackCount = json.getInt(JSON_TOTAL_TRACK_COUNT);
    int duration = json.getInt(JSON_DURATION) * 1000; // ms

    LogHelper.d(TAG, "Found music track: ", json);

    // Media is stored relative to JSON file
    if (!source.startsWith("http")) {
        source = basePath + source;/*w  w w .  j av  a2  s .  c  o  m*/
    }
    if (!iconUrl.startsWith("http")) {
        iconUrl = basePath + iconUrl;
    }
    // Since we don't have a unique ID in the server, we fake one using the hashcode of
    // the music source. In a real world app, this could come from the server.
    String id = String.valueOf(source.hashCode());

    // Adding the music source to the MediaMetadata (and consequently using it in the
    // mediaSession.setMetadata) is not a good idea for a real world music app, because
    // the session metadata can be accessed by notification listeners. This is done in this
    // sample for convenience only.
    return new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_MEDIA_ID, id)
            .putString(CUSTOM_METADATA_TRACK_SOURCE, source).putString(MediaMetadata.METADATA_KEY_ALBUM, album)
            .putString(MediaMetadata.METADATA_KEY_ARTIST, artist)
            .putLong(MediaMetadata.METADATA_KEY_DURATION, duration)
            .putString(MediaMetadata.METADATA_KEY_GENRE, genre)
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, iconUrl)
            .putString(MediaMetadata.METADATA_KEY_TITLE, title)
            .putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, trackNumber)
            .putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, totalTrackCount).build();
}

From source file:org.opensilk.video.playback.PlaybackService.java

void updateMetadata() {
    assertCreated();//  w  w  w.  j  av a2  s  .co  m
    final Media media = mMediaPlayer.getMedia();
    final MediaBrowser.MediaItem mediaItem = mDbClient.getMedia(media.getUri());

    final MediaMetadata.Builder b = new MediaMetadata.Builder();
    CharSequence title;
    Uri artworkUri = null;
    long duration;
    if (mediaItem != null) {
        MediaDescription description = mediaItem.getDescription();
        title = description.getTitle();
        b.putText(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, title);
        b.putText(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, description.getSubtitle());
        if (description.getIconUri() != null) {
            b.putText(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, description.getIconUri().toString());
            artworkUri = description.getIconUri();
        }
        MediaMetaExtras metaExtras = MediaMetaExtras.from(description);
        b.putText(MediaMetadata.METADATA_KEY_TITLE, metaExtras.getMediaTitle());
        duration = metaExtras.getDuration();
    } else {
        title = media.getMeta(Media.Meta.Title);
        b.putText(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, title);
        String artworkUrl = media.getMeta(Media.Meta.ArtworkURL);
        if (!StringUtils.isEmpty(artworkUrl)) {
            b.putText(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, artworkUrl);
            artworkUri = Uri.parse(artworkUrl);
        }
        duration = mMediaPlayer.getLength();
    }
    b.putLong(MediaMetadata.METADATA_KEY_DURATION, duration);
    if (artworkUri != null) {
        RequestOptions options = new RequestOptions().fitCenter(mContext);
        FutureTarget<Bitmap> futureTarget = Glide.with(mContext).asBitmap().apply(options).load(artworkUri)
                .submit();
        try {
            Bitmap bitmap = futureTarget.get(5000, TimeUnit.MILLISECONDS);
            b.putBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON, bitmap);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            //pass
        }
    }
    mMediaSession.setMetadata(b.build());
    mMediaSession.setSessionActivity(makeActivityIntent(mediaItem));
}

From source file:com.chinaftw.music.model.MusicProvider.java

private MediaMetadata buildFromJSON(JSONObject json) throws JSONException {
    String id = String.valueOf(json.getInt(JSON_SONG_ID));
    String title = json.getString(JSON_TITLE);
    String album = json.getJSONObject(JSON_ALBUM).getString(JSON_ALBUM_NAME);
    String albumId = json.getJSONObject(JSON_ALBUM).getString(JSON_ALBUM_ID);
    String artist = json.getJSONArray(JSON_ARTIST).getJSONObject(0).getString(JSON_ARTIST_NAME);
    String artistId = json.getJSONObject(JSON_ALBUM).getString(JSON_ARTIST_ID);
    String imageId = json.getJSONObject(JSON_ALBUM).getString(JSON_ALBUM_IMAGE_ID);
    int duration = json.getInt(JSON_DURATION); // ms

    LogHelper.d(TAG, "Found music track: ", json);

    String source = MusicAPI.getSongUrl(id);
    String imageUri = MusicAPI.getPictureUrl(imageId);

    // Since we don't have a unique ID in the server, we fake one using the hashcode of
    // the music source. In a real world app, this could come from the server.

    // Adding the music source to the MediaMetadata (and consequently using it in the
    // mediaSession.setMetadata) is not a good idea for a real world music app, because
    // the session metadata can be accessed by notification listeners. This is done in this
    // sample for convenience only.
    return new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_MEDIA_ID, id)
            .putString(CUSTOM_METADATA_TRACK_SOURCE, source).putString(MediaMetadata.METADATA_KEY_ALBUM, album)
            .putString(MediaMetadata.METADATA_KEY_ARTIST, artist)
            .putLong(MediaMetadata.METADATA_KEY_DURATION, duration)
            .putString(MediaMetadata.METADATA_KEY_GENRE, "BILLBOARD") //TODO
            .putString(MediaMetadata.METADATA_KEY_TITLE, title)
            .putString(MediaMetadata.METADATA_KEY_ART_URI, imageUri)
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, imageUri)
            .putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, imageUri).build();
}

From source file:robert843.o2.pl.player.model.MusicProvider.java

private MediaMetadata buildFromJSON(String ytId)
        throws JSONException, ParserConfigurationException, IOException {
    String title = "";
    String artist = "";
    String source = "";
    String iconUrl = "";
    String site = "yt";
    int duration = 0; // ms

    LogHelper.d(TAG, "Found music track: ", ytId);

    // Media is stored relative to JSON file
    if (site.contains("yt")) {
        MovieParser parser = new MovieParser(ytId);
        parser.parse();//  ww  w.  ja  v  a  2s .com
        source = "" + parser.getUrl();
        duration = parser.getLength() * 1000;
        iconUrl = parser.getAlbumUrl();
        System.out.println(iconUrl);
        title = parser.getTitle();
        artist = parser.getChanelName();
    }

    // Since we don't have a unique ID in the server, we fake one using the hashcode of
    // the music source. In a real world app, this could come from the server.
    String id = String.valueOf(source.hashCode());

    // Adding the music source to the MediaMetadata (and consequently using it in the
    // mediaSession.setMetadata) is not a good idea for a real world music app, because
    // the session metadata can be accessed by notification listeners. This is done in this
    // sample for convenience only.
    return new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_MEDIA_ID, id)
            // .putString(CUSTOM_METADATA_TRACK_SOURCE, source)
            .putString(MediaMetadata.METADATA_KEY_ARTIST, artist).putString("id", ytId)
            .putLong(MediaMetadata.METADATA_KEY_DURATION, duration)
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, iconUrl)
            .putString(MediaMetadata.METADATA_KEY_TITLE, title).build();
}

From source file:de.rwth_aachen.comsys.audiosync.model.MusicProvider.java

private void createLocal(String file) {
    MediaMetadata item = new MediaMetadata.Builder()
            .putString(MediaMetadata.METADATA_KEY_MEDIA_ID, LOCAL_PREFIX + file)
            .putString(CUSTOM_METADATA_TRACK_SOURCE, "https://dl.dropboxusercontent.com/u/58908793/" + file)
            .putString(CUSTOM_METADATA_ASSET_FILE, file).putString(MediaMetadata.METADATA_KEY_ALBUM, "Demo")
            .putString(MediaMetadata.METADATA_KEY_ARTIST, "Simon G")
            .putLong(MediaMetadata.METADATA_KEY_DURATION, 304000)
            .putString(MediaMetadata.METADATA_KEY_GENRE, "Testing")
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI,
                    "https://upload.wikimedia.org/wikipedia/commons/0/02/Metr%C3%B3nomo_digital_Korg_MA-30.jpg")
            .putString(MediaMetadata.METADATA_KEY_TITLE, file)
            .putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, 1)
            .putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, 1).build();

    String musicId = item.getString(MediaMetadata.METADATA_KEY_MEDIA_ID);
    mMusicListById.put(musicId, new MutableMediaMetadata(musicId, item));
}

From source file:com.appdevper.mediaplayer.activity.FullScreenPlayerActivity.java

private void updateDuration(MediaMetadataCompat metadata) {
    if (metadata == null) {
        return;/* w  w  w  . j a  v  a  2s.c  om*/
    }
    LogHelper.d(TAG, "updateDuration called");
    int duration = (int) metadata.getLong(MediaMetadata.METADATA_KEY_DURATION);
    mSeekbar.setMax(duration);
    mEnd.setText(Utils.formatMillis(duration));
}

From source file:de.kraenksoft.c3tv.ui.PlaybackOverlayFragment.java

private void updateMovieView(MediaMetadata metadata) {
    Video v = new Video.VideoBuilder().buildFromMediaDesc(metadata.getDescription());
    long dur = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION);

    // PlaybackControlsRow doesn't allow you to set the item, so we must create a new one
    // because our Video class is now immutable.
    // TODO(ryanseys): Implement Playback Glue support so this can be mitigated.
    mPlaybackControlsRow = new PlaybackControlsRow(v);
    mPlaybackControlsRow.setTotalTime((int) dur);

    // Show the video card image if there is enough room in the UI for it.
    // If you have many primary actions, you may not have enough room.
    updateVideoImage(v.cardImageUrl);/*from   w  w  w. j  a  v  a2  s .c o m*/

    mRowsAdapter.clear();
    mRowsAdapter.add(mPlaybackControlsRow);

    updatePlaybackRow();

    mPlaybackControlsRow.setPrimaryActionsAdapter(mPrimaryActionsAdapter);
    mPlaybackControlsRow.setSecondaryActionsAdapter(mSecondaryActionsAdapter);

    addOtherRows();
}

From source file:org.mythtv.android.presentation.view.fragment.TvPlaybackOverlayFragment.java

private void updateMovieView(MediaMetadata metadata) {

    VideoModel v = new VideoModel.VideoModelBuilder().buildFromMediaDesc(metadata.getDescription());
    long dur = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION);

    // PlaybackControlsRow doesn't allow you to set the item, so we must create a new one
    // because our Video class is now immutable.
    // TODO(ryanseys): Implement Playback Glue support so this can be mitigated.
    mPlaybackControlsRow = new PlaybackControlsRow(v);
    mPlaybackControlsRow.setTotalTime((int) dur);

    // Show the video card image if there is enough room in the UI for it.
    // If you have many primary actions, you may not have enough room.
    updateVideoImage(v.cardImageUrl);/*from   w  w  w  .j  a  v a 2  s .c o m*/

    mRowsAdapter.clear();
    mRowsAdapter.add(mPlaybackControlsRow);

    updatePlaybackRow();

    mPlaybackControlsRow.setPrimaryActionsAdapter(mPrimaryActionsAdapter);
    mPlaybackControlsRow.setSecondaryActionsAdapter(mSecondaryActionsAdapter);

    addOtherRows();

}

From source file:com.wojtechnology.sunami.TheBrain.java

private void setMetadata(FireMixtape song) {
    new GetArtworkTask().execute(song);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mNotification = getLollipopNotifBuilder(true).setLargeIcon(mThumbnail).setContentTitle(song.title)
                .setContentText(song.artist).build();
        startForeground(534, mNotification);
    } else {// w  w w  .  jav a 2  s. co m
        PlaybackStateCompat state = new PlaybackStateCompat.Builder()
                .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE
                        | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SEEK_TO
                        | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0.0f).build();
        MediaSessionCompatHelper.applyState(mSession, state);
        mSession.setMetadata(
                new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
                        .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artist)
                        .putLong(MediaMetadata.METADATA_KEY_DURATION, Long.parseLong(song.duration)).build());
    }
}

From source file:com.wojtechnology.sunami.TheBrain.java

private void registerAudio() {
    if (mHasAudioFocus || !mIsInit) {
        return;/*from   w w  w .j  ava  2s .com*/
    }
    mHasAudioFocus = true;

    // Add audio focus change listener
    mAFChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                pausePlayback();
                setUI(false);
            } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                // Does nothing cause made me play music at work
            } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                pausePlayback();
                unregisterAudio();
                setUI(false);
            }
        }
    };

    mAudioManager.requestAudioFocus(mAFChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    // Add headphone out listener
    registerReceiver(mNoisyAudioStreamReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));

    // Add notification and transport controls
    ComponentName eventReceiver = new ComponentName(getPackageName(),
            RemoteControlEventReceiver.class.getName());
    mSession = new MediaSessionCompat(this, "FireSession", eventReceiver, null);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC);
    mSession.setMetadata(new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_TITLE, "")
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, "")
            .putLong(MediaMetadata.METADATA_KEY_DURATION, -1).build());

    mSession.setCallback(new MediaSessionCompat.Callback() {

        @Override
        public void onSeekTo(long pos) {
            super.onSeekTo(pos);
            setProgress((int) pos, isPlaying());
        }
    });
    mSession.setActive(true);
}