use of com.google.android.gms.cast.MediaMetadata 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.cast.MediaMetadata in project Shuttle by timusus.
the class MusicService method prepareChromeCastLoad.
void prepareChromeCastLoad(int position, boolean autoPlay) {
if (currentSong == null) {
return;
}
if (TextUtils.isEmpty(currentSong.path)) {
return;
}
MediaMetadata metadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);
metadata.putString(MediaMetadata.KEY_ALBUM_ARTIST, getAlbumArtistName());
metadata.putString(MediaMetadata.KEY_ALBUM_TITLE, getAlbumName());
metadata.putString(MediaMetadata.KEY_TITLE, getSongName());
metadata.addImage(new WebImage(Uri.parse("http://" + ShuttleUtils.getIpAddr() + ":5000" + "/image/" + getSongId())));
MediaInfo selectedMedia = new MediaInfo.Builder("http://" + ShuttleUtils.getIpAddr() + ":5000" + "/audio/" + getSongId()).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED).setContentType("audio/*").setMetadata(metadata).build();
if (ShuttleUtils.isUpgraded() && mCastManager != null) {
doOnMainThread(() -> {
Glide.with(MusicService.this).load(getSong()).asBitmap().override(1024, 1024).placeholder(GlideUtils.getLargePlaceHolderResId()).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
loadRemoteMedia(selectedMedia, position, autoPlay, resource, null);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
loadRemoteMedia(selectedMedia, position, autoPlay, null, errorDrawable);
}
});
});
}
}
use of com.google.android.gms.cast.MediaMetadata in project Shuttle by timusus.
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(MediaMetadata.KEY_ALBUM_TITLE, md.getString(MediaMetadata.KEY_ALBUM_TITLE));
wrapper.putString(MediaMetadata.KEY_ALBUM_ARTIST, md.getString(MediaMetadata.KEY_ALBUM_ARTIST));
wrapper.putString(MediaMetadata.KEY_COMPOSER, md.getString(MediaMetadata.KEY_COMPOSER));
wrapper.putString(MediaMetadata.KEY_SERIES_TITLE, md.getString(MediaMetadata.KEY_SERIES_TITLE));
wrapper.putInt(MediaMetadata.KEY_SEASON_NUMBER, md.getInt(MediaMetadata.KEY_SEASON_NUMBER));
wrapper.putInt(MediaMetadata.KEY_EPISODE_NUMBER, md.getInt(MediaMetadata.KEY_EPISODE_NUMBER));
Calendar releaseCalendar = md.getDate(MediaMetadata.KEY_RELEASE_DATE);
if (releaseCalendar != null) {
long releaseMillis = releaseCalendar.getTimeInMillis();
wrapper.putLong(MediaMetadata.KEY_RELEASE_DATE, releaseMillis);
}
wrapper.putInt(KEY_MEDIA_TYPE, info.getMetadata().getMediaType());
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());
jsonObject.put(KEY_TRACK_CONTENT_TYPE, mt.getContentType());
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;
}
use of com.google.android.gms.cast.MediaMetadata in project Shuttle by timusus.
the class VideoCastManager method updateMiniController.
/**
* Updates the information and state of a MiniController.
*
* @throws TransientNetworkDisconnectionException
* @throws NoConnectionException
*/
private void updateMiniController(IMiniController controller) throws TransientNetworkDisconnectionException, NoConnectionException {
checkConnectivity();
checkRemoteMediaPlayerAvailable();
if (mRemoteMediaPlayer.getStreamDuration() > 0 || isRemoteStreamLive()) {
MediaInfo mediaInfo = getRemoteMediaInformation();
MediaMetadata mm = mediaInfo.getMetadata();
controller.setStreamType(mediaInfo.getStreamType());
controller.setPlaybackStatus(mState, mIdleReason);
controller.setSubtitle(mContext.getResources().getString(R.string.ccl_casting_to_device, mDeviceName));
controller.setTitle(mm.getString(MediaMetadata.KEY_TITLE));
controller.setIcon(Utils.getImageUri(mediaInfo, 0));
}
}
use of com.google.android.gms.cast.MediaMetadata in project Shuttle by timusus.
the class VideoCastNotificationService method build.
/**
* Build the MediaStyle notification. The action that are added to this notification are
* selected by the client application from a pre-defined set of actions
*
* @see CastConfiguration.Builder#addNotificationAction(int, boolean)
**/
protected void build(MediaInfo info, Bitmap bitmap, boolean isPlaying) throws CastException, TransientNetworkDisconnectionException, NoConnectionException {
// Media metadata
MediaMetadata metadata = info.getMetadata();
String castingTo = getResources().getString(R.string.ccl_casting_to_device, mCastManager.getDeviceName());
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_action_notification).setContentTitle(metadata.getString(MediaMetadata.KEY_TITLE)).setContentText(castingTo).setContentIntent(getContentIntent(info)).setLargeIcon(bitmap).setStyle(new NotificationCompat.MediaStyle().setShowActionsInCompactView(mNotificationCompactActionsArray).setMediaSession(mCastManager.getMediaSessionCompatToken())).setOngoing(true).setShowWhen(false).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
for (Integer notificationType : mNotificationActions) {
switch(notificationType) {
case CastConfiguration.NOTIFICATION_ACTION_DISCONNECT:
builder.addAction(getDisconnectAction());
break;
case CastConfiguration.NOTIFICATION_ACTION_PLAY_PAUSE:
builder.addAction(getPlayPauseAction(info, isPlaying));
break;
case CastConfiguration.NOTIFICATION_ACTION_SKIP_NEXT:
builder.addAction(getSkipNextAction());
break;
case CastConfiguration.NOTIFICATION_ACTION_SKIP_PREVIOUS:
builder.addAction(getSkipPreviousAction());
break;
case CastConfiguration.NOTIFICATION_ACTION_FORWARD:
builder.addAction(getForwardAction(mForwardTimeInMillis));
break;
case CastConfiguration.NOTIFICATION_ACTION_REWIND:
builder.addAction(getRewindAction(mForwardTimeInMillis));
break;
}
}
mNotification = builder.build();
}
Aggregations