Search in sources :

Example 6 with MediaInfo

use of com.google.android.gms.cast.MediaInfo in project Shuttle by timusus.

the class Utils method bundleToMediaInfo.

/**
     * Builds and returns a {@link MediaInfo} that was wrapped in a {@link Bundle} by
     * <code>mediaInfoToBundle</code>. It is assumed that the type of the {@link MediaInfo} is
     * {@code MediaMetaData.MEDIA_TYPE_MOVIE}
     *
     * @see <code>mediaInfoToBundle()</code>
     */
public static MediaInfo bundleToMediaInfo(Bundle wrapper) {
    if (wrapper == null) {
        return null;
    }
    MediaMetadata metaData = new MediaMetadata(wrapper.getInt(KEY_MEDIA_TYPE));
    metaData.putString(MediaMetadata.KEY_SUBTITLE, wrapper.getString(MediaMetadata.KEY_SUBTITLE));
    metaData.putString(MediaMetadata.KEY_TITLE, wrapper.getString(MediaMetadata.KEY_TITLE));
    metaData.putString(MediaMetadata.KEY_STUDIO, wrapper.getString(MediaMetadata.KEY_STUDIO));
    metaData.putString(MediaMetadata.KEY_ALBUM_ARTIST, wrapper.getString(MediaMetadata.KEY_ALBUM_ARTIST));
    metaData.putString(MediaMetadata.KEY_ALBUM_TITLE, wrapper.getString(MediaMetadata.KEY_ALBUM_TITLE));
    metaData.putString(MediaMetadata.KEY_COMPOSER, wrapper.getString(MediaMetadata.KEY_COMPOSER));
    metaData.putString(MediaMetadata.KEY_SERIES_TITLE, wrapper.getString(MediaMetadata.KEY_SERIES_TITLE));
    metaData.putInt(MediaMetadata.KEY_SEASON_NUMBER, wrapper.getInt(MediaMetadata.KEY_SEASON_NUMBER));
    metaData.putInt(MediaMetadata.KEY_EPISODE_NUMBER, wrapper.getInt(MediaMetadata.KEY_EPISODE_NUMBER));
    long releaseDateMillis = wrapper.getLong(MediaMetadata.KEY_RELEASE_DATE, 0);
    if (releaseDateMillis > 0) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(releaseDateMillis);
        metaData.putDate(MediaMetadata.KEY_RELEASE_DATE, calendar);
    }
    ArrayList<String> images = wrapper.getStringArrayList(KEY_IMAGES);
    if (images != null && !images.isEmpty()) {
        for (String url : images) {
            Uri uri = Uri.parse(url);
            metaData.addImage(new WebImage(uri));
        }
    }
    String customDataStr = wrapper.getString(KEY_CUSTOM_DATA);
    JSONObject customData = null;
    if (!TextUtils.isEmpty(customDataStr)) {
        try {
            customData = new JSONObject(customDataStr);
        } catch (JSONException e) {
            LOGE(TAG, "Failed to deserialize the custom data string: custom data= " + customDataStr);
        }
    }
    List<MediaTrack> mediaTracks = null;
    if (wrapper.getString(KEY_TRACKS_DATA) != null) {
        try {
            JSONArray jsonArray = new JSONArray(wrapper.getString(KEY_TRACKS_DATA));
            mediaTracks = new ArrayList<>();
            if (jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = (JSONObject) jsonArray.get(i);
                    MediaTrack.Builder builder = new MediaTrack.Builder(jsonObj.getLong(KEY_TRACK_ID), jsonObj.getInt(KEY_TRACK_TYPE));
                    if (jsonObj.has(KEY_TRACK_NAME)) {
                        builder.setName(jsonObj.getString(KEY_TRACK_NAME));
                    }
                    if (jsonObj.has(KEY_TRACK_SUBTYPE)) {
                        builder.setSubtype(jsonObj.getInt(KEY_TRACK_SUBTYPE));
                    }
                    if (jsonObj.has(KEY_TRACK_CONTENT_ID)) {
                        builder.setContentId(jsonObj.getString(KEY_TRACK_CONTENT_ID));
                    }
                    if (jsonObj.has(KEY_TRACK_CONTENT_TYPE)) {
                        builder.setContentType(jsonObj.getString(KEY_TRACK_CONTENT_TYPE));
                    }
                    if (jsonObj.has(KEY_TRACK_LANGUAGE)) {
                        builder.setLanguage(jsonObj.getString(KEY_TRACK_LANGUAGE));
                    }
                    if (jsonObj.has(KEY_TRACKS_DATA)) {
                        builder.setCustomData(new JSONObject(jsonObj.getString(KEY_TRACKS_DATA)));
                    }
                    mediaTracks.add(builder.build());
                }
            }
        } catch (JSONException e) {
            LOGE(TAG, "Failed to build media tracks from the wrapper bundle", e);
        }
    }
    MediaInfo.Builder mediaBuilder = new MediaInfo.Builder(wrapper.getString(KEY_URL)).setStreamType(wrapper.getInt(KEY_STREAM_TYPE)).setContentType(wrapper.getString(KEY_CONTENT_TYPE)).setMetadata(metaData).setCustomData(customData).setMediaTracks(mediaTracks);
    if (wrapper.containsKey(KEY_STREAM_DURATION) && wrapper.getLong(KEY_STREAM_DURATION) >= 0) {
        mediaBuilder.setStreamDuration(wrapper.getLong(KEY_STREAM_DURATION));
    }
    return mediaBuilder.build();
}
Also used : Calendar(java.util.Calendar) JSONArray(org.json.JSONArray) WebImage(com.google.android.gms.common.images.WebImage) JSONException(org.json.JSONException) Uri(android.net.Uri) Point(android.graphics.Point) MediaTrack(com.google.android.gms.cast.MediaTrack) MediaInfo(com.google.android.gms.cast.MediaInfo) JSONObject(org.json.JSONObject) MediaMetadata(com.google.android.gms.cast.MediaMetadata)

Example 7 with MediaInfo

use of com.google.android.gms.cast.MediaInfo in project android-UniversalMusicPlayer by googlesamples.

the class CastPlayback method loadMedia.

private void loadMedia(String mediaId, boolean autoPlay) throws JSONException {
    String musicId = MediaIDHelper.extractMusicIDFromMediaID(mediaId);
    MediaMetadataCompat track = mMusicProvider.getMusic(musicId);
    if (track == null) {
        throw new IllegalArgumentException("Invalid mediaId " + mediaId);
    }
    if (!TextUtils.equals(mediaId, mCurrentMediaId)) {
        mCurrentMediaId = mediaId;
        mCurrentPosition = 0;
    }
    JSONObject customData = new JSONObject();
    customData.put(ITEM_ID, mediaId);
    MediaInfo media = toCastMediaMetadata(track, customData);
    mRemoteMediaClient.load(media, autoPlay, mCurrentPosition, customData);
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) MediaInfo(com.google.android.gms.cast.MediaInfo) JSONObject(org.json.JSONObject)

Example 8 with MediaInfo

use of com.google.android.gms.cast.MediaInfo in project AntennaPod by AntennaPod.

the class CastUtils method convertFromFeedMedia.

/**
     * Converts {@link FeedMedia} objects into a format suitable for sending to a Cast Device.
     * Before using this method, one should make sure {@link #isCastable(Playable)} returns
     * {@code true}.
     *
     * Unless media.{@link FeedMedia#loadMetadata() loadMetadata()} has already been called,
     * this method should not run on the main thread.
     *
     * @param media The {@link FeedMedia} object to be converted.
     * @return {@link MediaInfo} object in a format proper for casting.
     */
public static MediaInfo convertFromFeedMedia(FeedMedia media) {
    if (media == null) {
        return null;
    }
    MediaMetadata metadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_GENERIC);
    try {
        media.loadMetadata();
    } catch (Playable.PlayableException e) {
        Log.e(TAG, "Unable to load FeedMedia metadata", e);
    }
    FeedItem feedItem = media.getItem();
    if (feedItem != null) {
        metadata.putString(MediaMetadata.KEY_TITLE, media.getEpisodeTitle());
        String subtitle = media.getFeedTitle();
        if (subtitle != null) {
            metadata.putString(MediaMetadata.KEY_SUBTITLE, subtitle);
        }
        FeedImage image = feedItem.getImage();
        if (image != null && !TextUtils.isEmpty(image.getDownload_url())) {
            metadata.addImage(new WebImage(Uri.parse(image.getDownload_url())));
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(media.getItem().getPubDate());
        metadata.putDate(MediaMetadata.KEY_RELEASE_DATE, calendar);
        Feed feed = feedItem.getFeed();
        if (feed != null) {
            if (!TextUtils.isEmpty(feed.getAuthor())) {
                metadata.putString(MediaMetadata.KEY_ARTIST, feed.getAuthor());
            }
            if (!TextUtils.isEmpty(feed.getDownload_url())) {
                metadata.putString(KEY_FEED_URL, feed.getDownload_url());
            }
            if (!TextUtils.isEmpty(feed.getLink())) {
                metadata.putString(KEY_FEED_WEBSITE, feed.getLink());
            }
        }
        if (!TextUtils.isEmpty(feedItem.getItemIdentifier())) {
            metadata.putString(KEY_EPISODE_IDENTIFIER, feedItem.getItemIdentifier());
        } else {
            metadata.putString(KEY_EPISODE_IDENTIFIER, media.getStreamUrl());
        }
        if (!TextUtils.isEmpty(feedItem.getLink())) {
            metadata.putString(KEY_EPISODE_LINK, feedItem.getLink());
        }
    }
    String notes = null;
    try {
        notes = media.loadShownotes().call();
    } catch (Exception e) {
        Log.e(TAG, "Unable to load FeedMedia notes", e);
    }
    if (notes != null) {
        if (notes.length() > EPISODE_NOTES_MAX_LENGTH) {
            notes = notes.substring(0, EPISODE_NOTES_MAX_LENGTH);
        }
        metadata.putString(KEY_EPISODE_NOTES, notes);
    }
    // This field only identifies the id on the device that has the original version.
    // Idea is to perhaps, on a first approach, check if the version on the local DB with the
    // same id matches the remote object, and if not then search for episode and feed identifiers.
    // This at least should make media recognition for a single device much quicker.
    metadata.putInt(KEY_MEDIA_ID, ((Long) media.getIdentifier()).intValue());
    // A way to identify different casting media formats in case we change it in the future and
    // senders with different versions share a casting device.
    metadata.putInt(KEY_FORMAT_VERSION, FORMAT_VERSION_VALUE);
    MediaInfo.Builder builder = new MediaInfo.Builder(media.getStreamUrl()).setContentType(media.getMime_type()).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED).setMetadata(metadata);
    if (media.getDuration() > 0) {
        builder.setStreamDuration(media.getDuration());
    }
    return builder.build();
}
Also used : FeedImage(de.danoeh.antennapod.core.feed.FeedImage) MediaInfo(com.google.android.gms.cast.MediaInfo) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Playable(de.danoeh.antennapod.core.util.playback.Playable) Calendar(java.util.Calendar) MediaMetadata(com.google.android.gms.cast.MediaMetadata) WebImage(com.google.android.gms.common.images.WebImage) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 9 with MediaInfo

use of com.google.android.gms.cast.MediaInfo in project AntennaPod by AntennaPod.

the class RemoteMedia method extractMediaInfo.

public MediaInfo extractMediaInfo() {
    MediaMetadata metadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_GENERIC);
    metadata.putString(MediaMetadata.KEY_TITLE, episodeTitle);
    metadata.putString(MediaMetadata.KEY_SUBTITLE, feedTitle);
    if (!TextUtils.isEmpty(imageUrl)) {
        metadata.addImage(new WebImage(Uri.parse(imageUrl)));
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(pubDate);
    metadata.putDate(MediaMetadata.KEY_RELEASE_DATE, calendar);
    if (!TextUtils.isEmpty(feedAuthor)) {
        metadata.putString(MediaMetadata.KEY_ARTIST, feedAuthor);
    }
    if (!TextUtils.isEmpty(feedUrl)) {
        metadata.putString(CastUtils.KEY_FEED_URL, feedUrl);
    }
    if (!TextUtils.isEmpty(feedLink)) {
        metadata.putString(CastUtils.KEY_FEED_WEBSITE, feedLink);
    }
    if (!TextUtils.isEmpty(itemIdentifier)) {
        metadata.putString(CastUtils.KEY_EPISODE_IDENTIFIER, itemIdentifier);
    } else {
        metadata.putString(CastUtils.KEY_EPISODE_IDENTIFIER, downloadUrl);
    }
    if (!TextUtils.isEmpty(episodeLink)) {
        metadata.putString(CastUtils.KEY_EPISODE_LINK, episodeLink);
    }
    String notes = this.notes;
    if (notes != null) {
        if (notes.length() > CastUtils.EPISODE_NOTES_MAX_LENGTH) {
            notes = notes.substring(0, CastUtils.EPISODE_NOTES_MAX_LENGTH);
        }
        metadata.putString(CastUtils.KEY_EPISODE_NOTES, notes);
    }
    // Default id value
    metadata.putInt(CastUtils.KEY_MEDIA_ID, 0);
    metadata.putInt(CastUtils.KEY_FORMAT_VERSION, CastUtils.FORMAT_VERSION_VALUE);
    MediaInfo.Builder builder = new MediaInfo.Builder(downloadUrl).setContentType(mime_type).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED).setMetadata(metadata);
    if (duration > 0) {
        builder.setStreamDuration(duration);
    }
    return builder.build();
}
Also used : MediaInfo(com.google.android.gms.cast.MediaInfo) Calendar(java.util.Calendar) MediaMetadata(com.google.android.gms.cast.MediaMetadata) WebImage(com.google.android.gms.common.images.WebImage)

Example 10 with MediaInfo

use of com.google.android.gms.cast.MediaInfo in project android-UniversalMusicPlayer by googlesamples.

the class CastPlayback method toCastMediaMetadata.

/**
     * Helper method to convert a {@link android.media.MediaMetadata} to a
     * {@link com.google.android.gms.cast.MediaInfo} used for sending media to the receiver app.
     *
     * @param track {@link com.google.android.gms.cast.MediaMetadata}
     * @param customData custom data specifies the local mediaId used by the player.
     * @return mediaInfo {@link com.google.android.gms.cast.MediaInfo}
     */
private static MediaInfo toCastMediaMetadata(MediaMetadataCompat track, JSONObject customData) {
    MediaMetadata mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);
    mediaMetadata.putString(MediaMetadata.KEY_TITLE, track.getDescription().getTitle() == null ? "" : track.getDescription().getTitle().toString());
    mediaMetadata.putString(MediaMetadata.KEY_SUBTITLE, track.getDescription().getSubtitle() == null ? "" : track.getDescription().getSubtitle().toString());
    mediaMetadata.putString(MediaMetadata.KEY_ALBUM_ARTIST, track.getString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST));
    mediaMetadata.putString(MediaMetadata.KEY_ALBUM_TITLE, track.getString(MediaMetadataCompat.METADATA_KEY_ALBUM));
    WebImage image = new WebImage(new Uri.Builder().encodedPath(track.getString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI)).build());
    // First image is used by the receiver for showing the audio album art.
    mediaMetadata.addImage(image);
    // Second image is used by Cast Companion Library on the full screen activity that is shown
    // when the cast dialog is clicked.
    mediaMetadata.addImage(image);
    //noinspection ResourceType
    return new MediaInfo.Builder(track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE)).setContentType(MIME_TYPE_AUDIO_MPEG).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED).setMetadata(mediaMetadata).setCustomData(customData).build();
}
Also used : MediaInfo(com.google.android.gms.cast.MediaInfo) MediaMetadata(com.google.android.gms.cast.MediaMetadata) WebImage(com.google.android.gms.common.images.WebImage) Uri(android.net.Uri)

Aggregations

MediaInfo (com.google.android.gms.cast.MediaInfo)16 MediaMetadata (com.google.android.gms.cast.MediaMetadata)9 WebImage (com.google.android.gms.common.images.WebImage)5 NoConnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException)5 JSONObject (org.json.JSONObject)4 Uri (android.net.Uri)3 TransientNetworkDisconnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)3 Calendar (java.util.Calendar)3 JSONException (org.json.JSONException)3 Bitmap (android.graphics.Bitmap)2 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)2 NotFoundException (android.content.res.Resources.NotFoundException)1 SQLiteException (android.database.sqlite.SQLiteException)1 Point (android.graphics.Point)1 Drawable (android.graphics.drawable.Drawable)1 Bundle (android.os.Bundle)1 Builder (com.google.android.gms.cast.Cast.CastOptions.Builder)1 MediaTrack (com.google.android.gms.cast.MediaTrack)1 CastException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)1 FetchBitmapTask (com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask)1