Search in sources :

Example 1 with WebImage

use of com.google.android.gms.common.images.WebImage 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 2 with WebImage

use of com.google.android.gms.common.images.WebImage 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 3 with WebImage

use of com.google.android.gms.common.images.WebImage in project zype-android by zype.

the class VideoCastManager method setBitmapForLockScreen.

/*
     * Sets the appropriate {@link Bitmap} for the right size image for lock screen. In ICS and
     * JB, the image shown on the lock screen is a small size bitmap but for KitKat, the image is a
     * full-screen image so we need to separately handle these two cases.
     */
private void setBitmapForLockScreen(MediaInfo video) {
    if (video == null || mMediaSessionCompat == null) {
        return;
    }
    Uri imgUrl = null;
    Bitmap bm = null;
    List<WebImage> images = video.getMetadata().getImages();
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
        if (images.size() > 1) {
            imgUrl = images.get(1).getUrl();
        } else if (images.size() == 1) {
            imgUrl = images.get(0).getUrl();
        } else if (mContext != null) {
            // we don't have a url for image so get a placeholder image from resources
            bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.album_art_placeholder_large);
        }
    } else if (!images.isEmpty()) {
        imgUrl = images.get(0).getUrl();
    } else {
        // we don't have a url for image so get a placeholder image from resources
        bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.album_art_placeholder);
    }
    if (bm != null) {
        MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
        MediaMetadataCompat.Builder newBuilder = currentMetadata == null ? new MediaMetadataCompat.Builder() : new MediaMetadataCompat.Builder(currentMetadata);
        mMediaSessionCompat.setMetadata(newBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bm).build());
    } else {
        new FetchBitmapTask() {

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
                MediaMetadataCompat.Builder newBuilder = currentMetadata == null ? new MediaMetadataCompat.Builder() : new MediaMetadataCompat.Builder(currentMetadata);
                mMediaSessionCompat.setMetadata(newBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap).build());
            }
        }.execute(imgUrl);
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Bitmap(android.graphics.Bitmap) Builder(com.google.android.gms.cast.Cast.CastOptions.Builder) WebImage(com.google.android.gms.common.images.WebImage) FetchBitmapTask(com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask) Uri(android.net.Uri)

Example 4 with WebImage

use of com.google.android.gms.common.images.WebImage in project zype-android by zype.

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(MediaMetadata.MEDIA_TYPE_MOVIE);
    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));
    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<MediaTrack>();
            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_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 : JSONArray(org.json.JSONArray) WebImage(com.google.android.gms.common.images.WebImage) JSONException(org.json.JSONException) Uri(android.net.Uri) 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 5 with WebImage

use of com.google.android.gms.common.images.WebImage 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)

Aggregations

WebImage (com.google.android.gms.common.images.WebImage)19 MediaMetadata (com.google.android.gms.cast.MediaMetadata)17 MediaInfo (com.google.android.gms.cast.MediaInfo)11 Uri (android.net.Uri)7 Calendar (java.util.Calendar)6 MediaTrack (com.google.android.gms.cast.MediaTrack)5 JSONArray (org.json.JSONArray)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 Bitmap (android.graphics.Bitmap)3 ArrayList (java.util.ArrayList)3 Point (android.graphics.Point)2 Bundle (android.os.Bundle)2 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)2 Builder (com.google.android.gms.cast.Cast.CastOptions.Builder)2 FetchBitmapTask (com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask)2 FeedItem (de.danoeh.antennapod.core.feed.FeedItem)2 Playable (de.danoeh.antennapod.core.util.playback.Playable)2 SQLiteException (android.database.sqlite.SQLiteException)1 Drawable (android.graphics.drawable.Drawable)1