use of com.google.android.gms.cast.MediaMetadata 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();
}
use of com.google.android.gms.cast.MediaMetadata 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();
}
use of com.google.android.gms.cast.MediaMetadata in project Shuttle by timusus.
the class VideoMediaRouteControllerDialog method updateMetadata.
private void updateMetadata() {
MediaInfo info;
try {
info = mCastManager.getRemoteMediaInformation();
} catch (TransientNetworkDisconnectionException | NoConnectionException e) {
hideControls(true, R.string.ccl_failed_no_connection_short);
return;
}
if (info == null) {
hideControls(true, R.string.ccl_no_media_info);
return;
}
mStreamType = info.getStreamType();
hideControls(false, 0);
MediaMetadata mm = info.getMetadata();
mTitle.setText(mm.getString(MediaMetadata.KEY_TITLE));
mSubTitle.setText(mm.getString(MediaMetadata.KEY_SUBTITLE));
setIcon(mm.hasImages() ? mm.getImages().get(0).getUrl() : null);
}
use of com.google.android.gms.cast.MediaMetadata in project Shuttle by timusus.
the class VideoCastManager method updateMediaSessionMetadata.
/*
* On ICS and JB, lock screen metadata is one liner: Title - Album Artist - Album. On KitKat, it
* has two lines: Title , Album Artist - Album
*/
private void updateMediaSessionMetadata() {
if ((mMediaSessionCompat == null) || !isFeatureEnabled(CastConfiguration.FEATURE_LOCKSCREEN)) {
return;
}
try {
MediaInfo info = getRemoteMediaInformation();
if (info == null) {
return;
}
final MediaMetadata mm = info.getMetadata();
MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
MediaMetadataCompat.Builder newBuilder = currentMetadata == null ? new MediaMetadataCompat.Builder() : new MediaMetadataCompat.Builder(currentMetadata);
MediaMetadataCompat metadata = newBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, mm.getString(MediaMetadata.KEY_TITLE)).putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, mContext.getResources().getString(R.string.ccl_casting_to_device, getDeviceName())).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mm.getString(MediaMetadata.KEY_TITLE)).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, mm.getString(MediaMetadata.KEY_SUBTITLE)).putLong(MediaMetadataCompat.METADATA_KEY_DURATION, info.getStreamDuration()).build();
mMediaSessionCompat.setMetadata(metadata);
Uri iconUri = mm.hasImages() ? mm.getImages().get(0).getUrl() : null;
if (iconUri == null) {
Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.album_art_placeholder);
mMediaSessionCompat.setMetadata(newBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, bm).build());
} else {
if (mMediaSessionIconFetchTask != null) {
mMediaSessionIconFetchTask.cancel(true);
}
mMediaSessionIconFetchTask = new FetchBitmapTask() {
@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_DISPLAY_ICON, bitmap).build());
}
mMediaSessionIconFetchTask = null;
}
};
mMediaSessionIconFetchTask.execute(iconUri);
}
} catch (NotFoundException e) {
LOGE(TAG, "Failed to update Media Session due to resource not found", e);
} catch (TransientNetworkDisconnectionException | NoConnectionException e) {
LOGE(TAG, "Failed to update Media Session due to network issues", e);
}
}
use of com.google.android.gms.cast.MediaMetadata 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();
}
Aggregations