use of com.google.android.gms.common.images.WebImage in project AntennaPod by AntennaPod.
the class MediaInfoCreator method from.
/**
* Converts {@link FeedMedia} objects into a format suitable for sending to a Cast Device.
* Before using this method, one should make sure isCastable(Playable) returns
* {@code true}. 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 from(FeedMedia media) {
if (media == null) {
return null;
}
MediaMetadata metadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_GENERIC);
if (media.getItem() == null) {
throw new IllegalStateException("item is null");
// media.setItem(DBReader.getFeedItem(media.getItemId()));
}
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);
}
// Manual because cast does not support embedded images
String url = feedItem.getImageUrl() == null ? feedItem.getFeed().getImageUrl() : feedItem.getImageUrl();
if (!TextUtils.isEmpty(url)) {
metadata.addImage(new WebImage(Uri.parse(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(CastUtils.KEY_FEED_URL, feed.getDownload_url());
}
if (!TextUtils.isEmpty(feed.getLink())) {
metadata.putString(CastUtils.KEY_FEED_WEBSITE, feed.getLink());
}
}
if (!TextUtils.isEmpty(feedItem.getItemIdentifier())) {
metadata.putString(CastUtils.KEY_EPISODE_IDENTIFIER, feedItem.getItemIdentifier());
} else {
metadata.putString(CastUtils.KEY_EPISODE_IDENTIFIER, media.getStreamUrl());
}
if (!TextUtils.isEmpty(feedItem.getLink())) {
metadata.putString(CastUtils.KEY_EPISODE_LINK, feedItem.getLink());
}
}
// 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(CastUtils.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(CastUtils.KEY_FORMAT_VERSION, CastUtils.FORMAT_VERSION_VALUE);
metadata.putString(CastUtils.KEY_STREAM_URL, media.getStreamUrl());
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();
}
use of com.google.android.gms.common.images.WebImage in project Shuttle by timusus.
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_ART, bm).build());
} else {
if (mLockScreenFetchTask != null) {
mLockScreenFetchTask.cancel(true);
}
Point screenSize = Utils.getDisplaySize(mContext);
mLockScreenFetchTask = new FetchBitmapTask(screenSize.x, screenSize.y, false) {
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && mMediaSessionCompat != 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_ART, bitmap).build());
}
mLockScreenFetchTask = null;
}
};
mLockScreenFetchTask.execute(imgUrl);
}
}
use of com.google.android.gms.common.images.WebImage in project AntennaPod by AntennaPod.
the class CastUtils method getPlayable.
//TODO make unit tests for all the conversion methods
/**
* Converts {@link MediaInfo} objects into the appropriate implementation of {@link Playable}.
*
* Unless <code>searchFeedMedia</code> is set to <code>false</code>, this method should not run
* on the GUI thread.
*
* @param media The {@link MediaInfo} object to be converted.
* @param searchFeedMedia If set to <code>true</code>, the database will be queried to find a
* {@link FeedMedia} instance that matches {@param media}.
* @return {@link Playable} object in a format proper for casting.
*/
public static Playable getPlayable(MediaInfo media, boolean searchFeedMedia) {
Log.d(TAG, "getPlayable called with searchFeedMedia=" + searchFeedMedia);
if (media == null) {
Log.d(TAG, "MediaInfo object provided is null, not converting to any Playable instance");
return null;
}
MediaMetadata metadata = media.getMetadata();
int version = metadata.getInt(KEY_FORMAT_VERSION);
if (version <= 0 || version > MAX_VERSION_FORWARD_COMPATIBILITY) {
Log.w(TAG, "MediaInfo object obtained from the cast device is not compatible with this" + "version of AntennaPod CastUtils, curVer=" + FORMAT_VERSION_VALUE + ", object version=" + version);
return null;
}
Playable result = null;
if (searchFeedMedia) {
long mediaId = metadata.getInt(KEY_MEDIA_ID);
if (mediaId > 0) {
FeedMedia fMedia = DBReader.getFeedMedia(mediaId);
if (fMedia != null) {
try {
fMedia.loadMetadata();
if (matches(media, fMedia)) {
result = fMedia;
Log.d(TAG, "FeedMedia object obtained matches the MediaInfo provided. id=" + mediaId);
} else {
Log.d(TAG, "FeedMedia object obtained does NOT match the MediaInfo provided. id=" + mediaId);
}
} catch (Playable.PlayableException e) {
Log.e(TAG, "Unable to load FeedMedia metadata to compare with MediaInfo", e);
}
} else {
Log.d(TAG, "Unable to find in database a FeedMedia with id=" + mediaId);
}
}
if (result == null) {
FeedItem feedItem = DBReader.getFeedItem(metadata.getString(KEY_FEED_URL), metadata.getString(KEY_EPISODE_IDENTIFIER));
if (feedItem != null) {
result = feedItem.getMedia();
Log.d(TAG, "Found episode that matches the MediaInfo provided. Using its media, if existing.");
}
}
}
if (result == null) {
List<WebImage> imageList = metadata.getImages();
String imageUrl = null;
if (!imageList.isEmpty()) {
imageUrl = imageList.get(0).getUrl().toString();
}
result = new RemoteMedia(media.getContentId(), metadata.getString(KEY_EPISODE_IDENTIFIER), metadata.getString(KEY_FEED_URL), metadata.getString(MediaMetadata.KEY_SUBTITLE), metadata.getString(MediaMetadata.KEY_TITLE), metadata.getString(KEY_EPISODE_LINK), metadata.getString(MediaMetadata.KEY_ARTIST), imageUrl, metadata.getString(KEY_FEED_WEBSITE), media.getContentType(), metadata.getDate(MediaMetadata.KEY_RELEASE_DATE).getTime());
String notes = metadata.getString(KEY_EPISODE_NOTES);
if (!TextUtils.isEmpty(notes)) {
((RemoteMedia) result).setNotes(notes);
}
Log.d(TAG, "Converted MediaInfo into RemoteMedia");
}
if (result.getDuration() == 0 && media.getStreamDuration() > 0) {
result.setDuration((int) media.getStreamDuration());
}
return result;
}
use of com.google.android.gms.common.images.WebImage 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();
}
use of com.google.android.gms.common.images.WebImage in project zype-android by zype.
the class Utils method mediaInfoToBundle.
/**
* Builds and returns a {@link Bundle} which contains a select subset of data in the
* {@link MediaInfo}. Since {@link MediaInfo} is not {@link Parcelable}, one can use this
* container bundle to pass around from one activity to another.
*
* @see <code>bundleToMediaInfo()</code>
*/
public static Bundle mediaInfoToBundle(MediaInfo info) {
if (info == null) {
return null;
}
MediaMetadata md = info.getMetadata();
Bundle wrapper = new Bundle();
wrapper.putString(MediaMetadata.KEY_TITLE, md.getString(MediaMetadata.KEY_TITLE));
wrapper.putString(MediaMetadata.KEY_SUBTITLE, md.getString(MediaMetadata.KEY_SUBTITLE));
wrapper.putString(KEY_URL, info.getContentId());
wrapper.putString(MediaMetadata.KEY_STUDIO, md.getString(MediaMetadata.KEY_STUDIO));
wrapper.putString(KEY_CONTENT_TYPE, info.getContentType());
wrapper.putInt(KEY_STREAM_TYPE, info.getStreamType());
wrapper.putLong(KEY_STREAM_DURATION, info.getStreamDuration());
if (!md.getImages().isEmpty()) {
ArrayList<String> urls = new ArrayList<>();
for (WebImage img : md.getImages()) {
urls.add(img.getUrl().toString());
}
wrapper.putStringArrayList(KEY_IMAGES, urls);
}
JSONObject customData = info.getCustomData();
if (customData != null) {
wrapper.putString(KEY_CUSTOM_DATA, customData.toString());
}
if (info.getMediaTracks() != null && !info.getMediaTracks().isEmpty()) {
try {
JSONArray jsonArray = new JSONArray();
for (MediaTrack mt : info.getMediaTracks()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(KEY_TRACK_NAME, mt.getName());
jsonObject.put(KEY_TRACK_CONTENT_ID, mt.getContentId());
jsonObject.put(KEY_TRACK_ID, mt.getId());
jsonObject.put(KEY_TRACK_LANGUAGE, mt.getLanguage());
jsonObject.put(KEY_TRACK_TYPE, mt.getType());
if (mt.getSubtype() != MediaTrack.SUBTYPE_UNKNOWN) {
jsonObject.put(KEY_TRACK_SUBTYPE, mt.getSubtype());
}
if (mt.getCustomData() != null) {
jsonObject.put(KEY_TRACK_CUSTOM_DATA, mt.getCustomData().toString());
}
jsonArray.put(jsonObject);
}
wrapper.putString(KEY_TRACKS_DATA, jsonArray.toString());
} catch (JSONException e) {
LOGE(TAG, "mediaInfoToBundle(): Failed to convert Tracks data to json", e);
}
}
return wrapper;
}
Aggregations