com.google.android.gms.cast.framework.media.RemoteMediaClient Java Examples

The following examples show how to use com.google.android.gms.cast.framework.media.RemoteMediaClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VinylCastService.java    From vinyl-cast with MIT License 6 votes vote down vote up
private void updateCastSession() {
    if (((VinylCastApplication)getApplication()).getCastSessionManager().getCurrentCastSession() == null) {
        return;
    }
    if (((VinylCastApplication)getApplication()).getCastSessionManager().getCurrentCastSession().getRemoteMediaClient() == null) {
        return;
    }

    RemoteMediaClient remoteMediaClient = ((VinylCastApplication)getApplication()).getCastSessionManager().getCurrentCastSession().getRemoteMediaClient();
    if (isRecording() && httpStreamServer != null) {
        MediaMetadata audioMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);
        String url = httpStreamServer.getStreamUrl();
        MediaInfo mediaInfo = new MediaInfo.Builder(url)
                .setContentType(httpStreamServer.getContentType())
                .setStreamType(MediaInfo.STREAM_TYPE_LIVE)
                .setStreamDuration(MediaInfo.UNKNOWN_DURATION)
                .setMetadata(audioMetadata)
                .build();
        Timber.d("Cast MediaInfo: " + mediaInfo);
        MediaLoadRequestData mediaLoadRequestData = new MediaLoadRequestData.Builder().setMediaInfo(mediaInfo).build();
        remoteMediaClient.load(mediaLoadRequestData);
    } else {
        remoteMediaClient.stop();
    }
}
 
Example #2
Source File: GoogleCastModule.java    From react-native-google-cast with MIT License 6 votes vote down vote up
@ReactMethod
public void setVolume(final double volume) {
    if (mCastSession != null) {
        getReactApplicationContext().runOnUiQueueThread(new Runnable() {
            @Override
            public void run() {
              RemoteMediaClient client = mCastSession.getRemoteMediaClient();
              if (client == null) {
                return;
              }

              client.setStreamVolume(volume);
            }
        });
    }
}
 
Example #3
Source File: GoogleCastModule.java    From react-native-google-cast with MIT License 6 votes vote down vote up
@ReactMethod
public void seek(final int position) {
    if (mCastSession != null) {
        getReactApplicationContext().runOnUiQueueThread(new Runnable() {
            @Override
            public void run() {
                RemoteMediaClient client = mCastSession.getRemoteMediaClient();
                if (client == null) {
                    return;
                }

                client.seek(position * 1000);
            }
        });
    }
}
 
Example #4
Source File: GoogleCastModule.java    From react-native-google-cast with MIT License 6 votes vote down vote up
@ReactMethod
public void stop() {
    if (mCastSession != null) {
        getReactApplicationContext().runOnUiQueueThread(new Runnable() {
            @Override
            public void run() {
                RemoteMediaClient client = mCastSession.getRemoteMediaClient();
                if (client == null) {
                    return;
                }

                client.stop();
            }
        });
    }
}
 
Example #5
Source File: GoogleCastModule.java    From react-native-google-cast with MIT License 6 votes vote down vote up
@ReactMethod
public void pause() {
    if (mCastSession != null) {
        getReactApplicationContext().runOnUiQueueThread(new Runnable() {
            @Override
            public void run() {
                RemoteMediaClient client = mCastSession.getRemoteMediaClient();
                if (client == null) {
                    return;
                }

                client.pause();
            }
        });
    }
}
 
Example #6
Source File: GoogleCastModule.java    From react-native-google-cast with MIT License 6 votes vote down vote up
@ReactMethod
public void play() {
    if (mCastSession != null) {
        getReactApplicationContext().runOnUiQueueThread(new Runnable() {
            @Override
            public void run() {
                RemoteMediaClient client = mCastSession.getRemoteMediaClient();
                if (client == null) {
                    return;
                }

                client.play();
            }
        });
    }
}
 
Example #7
Source File: GoogleCastSessionManagerListener.java    From react-native-google-cast with MIT License 6 votes vote down vote up
private void onApplicationConnected(final CastSession castSession) {
  module.setCastSession(castSession);
  module.runOnUiQueueThread(new Runnable() {
    @Override
    public void run() {
      RemoteMediaClient client = castSession.getRemoteMediaClient();
      if (client == null) {
        return;
      }

      remoteMediaClientListener = new GoogleCastRemoteMediaClientListener(module);
      client.addListener(remoteMediaClientListener);
      client.addProgressListener(remoteMediaClientListener, 1000);
    }
  });
}
 
Example #8
Source File: QueueListAdapter.java    From CastVideos-android with Apache License 2.0 6 votes vote down vote up
private void updatePlayPauseButtonImageResource(ImageButton button) {
    CastSession castSession = CastContext.getSharedInstance(mAppContext)
            .getSessionManager().getCurrentCastSession();
    RemoteMediaClient remoteMediaClient =
            (castSession == null) ? null : castSession.getRemoteMediaClient();
    if (remoteMediaClient == null) {
        button.setVisibility(View.GONE);
        return;
    }
    int status = remoteMediaClient.getPlayerState();
    switch (status) {
        case MediaStatus.PLAYER_STATE_PLAYING:
            button.setImageResource(PAUSE_RESOURCE);
            break;
        case MediaStatus.PLAYER_STATE_PAUSED:
            button.setImageResource(PLAY_RESOURCE);
            break;
        default:
            button.setVisibility(View.GONE);
    }
}
 
Example #9
Source File: ChromecastMiniControllerFragment.java    From SkyTube with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void init(RemoteMediaClient client, MediaInfo media, int position) {
	super.init(client, media, position);

	videoTitle.setText(video.getTitle());
	channelName.setText(video.getChannelName());

	// We just either started playback of a video, or resumed the cast session. In the latter case, if there's a video playing, let the activity
	// know so that the panel will appear.
	if(currentPlayerState != MediaStatus.PLAYER_STATE_IDLE) {
		activityListener.onPlayStarted();
	}
	chromecastMiniControllerLeftContainer.setAlpha(1);
	chromecastMiniControllerRightContainer.setAlpha(1);
	setDuration((int)client.getStreamDuration());
}
 
Example #10
Source File: QueueDataProvider.java    From CastVideos-android with Apache License 2.0 6 votes vote down vote up
private void updateMediaQueue() {
    RemoteMediaClient remoteMediaClient = getRemoteMediaClient();
    MediaStatus mediaStatus;
    List<MediaQueueItem> queueItems = null;
    if (remoteMediaClient != null) {
        mediaStatus = remoteMediaClient.getMediaStatus();
        if (mediaStatus != null) {
            queueItems = mediaStatus.getQueueItems();
            mRepeatMode = mediaStatus.getQueueRepeatMode();
            mCurrentIem = mediaStatus.getQueueItemById(mediaStatus.getCurrentItemId());
        }
    }
    mQueue.clear();
    if (queueItems == null) {
        Log.d(TAG, "Queue is cleared");
    } else {
        Log.d(TAG, "Queue is updated with a list of size: " + queueItems.size());
        if (queueItems.size() > 0) {
            mQueue.addAll(queueItems);
            mDetachedQueue = false;
        } else {
            mDetachedQueue = true;
        }
    }
}
 
Example #11
Source File: QueueDataProvider.java    From CastVideos-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onPreloadStatusUpdated() {
    RemoteMediaClient remoteMediaClient = getRemoteMediaClient();
    if (remoteMediaClient == null) {
        return;
    }
    MediaStatus mediaStatus = remoteMediaClient.getMediaStatus();
    if (mediaStatus == null) {
        return;
    }
    mUpcomingItem = mediaStatus.getQueueItemById(mediaStatus.getPreloadedItemId());
    Log.d(TAG, "onRemoteMediaPreloadStatusUpdated() with item=" + mUpcomingItem);
    if (mListener != null) {
        mListener.onQueueDataChanged();
    }
}
 
Example #12
Source File: QueueDataProvider.java    From CastVideos-android with Apache License 2.0 6 votes vote down vote up
private void syncWithRemoteQueue() {
    RemoteMediaClient remoteMediaClient = getRemoteMediaClient();
    if (remoteMediaClient != null) {
        remoteMediaClient.registerCallback(mRemoteMediaClientCallback);
        MediaStatus mediaStatus = remoteMediaClient.getMediaStatus();
        if (mediaStatus != null) {
            List<MediaQueueItem> items = mediaStatus.getQueueItems();
            if (items != null && !items.isEmpty()) {
                mQueue.clear();
                mQueue.addAll(items);
                mRepeatMode = mediaStatus.getQueueRepeatMode();
                mCurrentIem = mediaStatus.getQueueItemById(mediaStatus.getCurrentItemId());
                mDetachedQueue = false;
                mUpcomingItem = mediaStatus.getQueueItemById(mediaStatus.getPreloadedItemId());
            }
        }
    }
}
 
Example #13
Source File: QueueDataProvider.java    From CastVideos-android with Apache License 2.0 6 votes vote down vote up
public void removeAll() {
    synchronized (mLock) {
        if (mQueue.isEmpty()) {
            return;
        }
        RemoteMediaClient remoteMediaClient = getRemoteMediaClient();
        if (remoteMediaClient == null) {
            return;
        }
        int[] itemIds = new int[mQueue.size()];
        for (int i = 0; i < mQueue.size(); i++) {
            itemIds[i] = mQueue.get(i).getItemId();
        }
        remoteMediaClient.queueRemoveItems(itemIds, null);
        mQueue.clear();
    }
}
 
Example #14
Source File: LocalPlayerActivity.java    From CastVideos-android with Apache License 2.0 6 votes vote down vote up
private void loadRemoteMedia(int position, boolean autoPlay) {
    if (mCastSession == null) {
        return;
    }
    final RemoteMediaClient remoteMediaClient = mCastSession.getRemoteMediaClient();
    if (remoteMediaClient == null) {
        return;
    }
    remoteMediaClient.registerCallback(new RemoteMediaClient.Callback() {
        @Override
        public void onStatusUpdated() {
            Intent intent = new Intent(LocalPlayerActivity.this, ExpandedControlsActivity.class);
            startActivity(intent);
            remoteMediaClient.unregisterCallback(this);
        }
    });
    remoteMediaClient.load(new MediaLoadRequestData.Builder()
            .setMediaInfo(mSelectedMedia)
            .setAutoplay(autoPlay)
            .setCurrentTime(position).build());
}
 
Example #15
Source File: VideoDetailsFragment.java    From Loop with Apache License 2.0 5 votes vote down vote up
private boolean isRemoteVideoPlaying(){
    if(castSession != null){
        RemoteMediaClient remoteMediaClient = castSession.getRemoteMediaClient();
        return remoteMediaClient.isPlaying();
    }
    return false;
}
 
Example #16
Source File: VideoDetailsFragment.java    From Loop with Apache License 2.0 5 votes vote down vote up
private void resumeRemoteVideo(){
    if(castSession != null){
        RemoteMediaClient remoteMediaClient = castSession.getRemoteMediaClient();
        if(remoteMediaClient != null){
            remoteMediaClient.play();
        }
    }
}
 
Example #17
Source File: ChromecastBaseControllerFragment.java    From SkyTube with GNU General Public License v3.0 5 votes vote down vote up
public void init(RemoteMediaClient client, MediaInfo media, int position) {
	remoteMediaClient = client;
	currentPlayingMedia = media;
	currentPlayerState = remoteMediaClient.getPlayerState();

	Gson gson = new Gson();
	this.video = gson.fromJson(currentPlayingMedia.getMetadata().getString(BaseActivity.KEY_VIDEO), new TypeToken<YouTubeVideo>(){}.getType());

	if(currentPlayerState != MediaStatus.PLAYER_STATE_IDLE) {
		updateButtons();
	}
	remoteMediaClient.unregisterCallback(mediaCallback);
	remoteMediaClient.registerCallback(mediaCallback);
	setProgressBarUpdater();
}
 
Example #18
Source File: ChromecastControllerFragment.java    From SkyTube with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void init(RemoteMediaClient client, MediaInfo media, int position) {
	super.init(client, media, position);
	if(video != null) {
		setupDescription();
	}
	durationTextView.setMilliseconds(chromecastPlaybackProgressBar.getMax());
	if(media.getMetadata().getImages().size() > 0) {
		Picasso.with(getActivity().getApplicationContext())
						.load(media.getMetadata().getImages().get(0).getUrl().toString())
						.placeholder(R.drawable.thumbnail_default)
						.into(videoImage);
	}
}
 
Example #19
Source File: LocalPlayerActivity.java    From cast-videos-android with Apache License 2.0 5 votes vote down vote up
private void loadRemoteMedia(int position, boolean autoPlay) {
    if (mCastSession == null) {
        return;
    }
    final RemoteMediaClient remoteMediaClient = mCastSession.getRemoteMediaClient();
    if (remoteMediaClient == null) {
        return;
    }
    remoteMediaClient.addListener(new RemoteMediaClient.Listener() {
        @Override
        public void onStatusUpdated() {
            Intent intent = new Intent(LocalPlayerActivity.this, ExpandedControlsActivity.class);
            startActivity(intent);
            remoteMediaClient.removeListener(this);
        }

        @Override
        public void onMetadataUpdated() {
        }

        @Override
        public void onQueueStatusUpdated() {
        }

        @Override
        public void onPreloadStatusUpdated() {
        }

        @Override
        public void onSendingRemoteMediaRequest() {
        }

        @Override
        public void onAdBreakStatusUpdated() {

        }
    });
    remoteMediaClient.load(buildMediaInfo(), autoPlay, position);
}
 
Example #20
Source File: VideoDetailsFragment.java    From Loop with Apache License 2.0 5 votes vote down vote up
private void pauseRemoteVideo(){
    if(castSession != null){
        RemoteMediaClient remoteMediaClient = castSession.getRemoteMediaClient();
        if(remoteMediaClient != null){
            remoteMediaClient.pause();
        }
    }
}
 
Example #21
Source File: GoogleCastModule.java    From react-native-google-cast with MIT License 5 votes vote down vote up
protected @Nullable MediaStatus getMediaStatus() {
    if (mCastSession == null) {
        return null;
    }

    RemoteMediaClient client = mCastSession.getRemoteMediaClient();
    if (client == null) {
        return null;
    }

    return client.getMediaStatus();
}
 
Example #22
Source File: GoogleCastDelegate.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
public void loadRemoteMedia(@NonNull Activity activity, @NonNull DownloadEntry videoEntry, long position, boolean autoPlay) {
    if (castSession == null || !castSession.isConnected()) {
        return;
    }
    final String videoUrl = videoEntry.getBestEncodingUrl(activity);
    final RemoteMediaClient remoteMediaClient = castSession.getRemoteMediaClient();
    // If remote media player is not idle and playing the same video, don't do anything
    if (remoteMediaClient == null ||
            (remoteMediaClient.getIdleReason() != MediaStatus.IDLE_REASON_FINISHED &&
                    remoteMediaClient.getMediaInfo() != null &&
                    remoteMediaClient.getMediaInfo().getContentId().equals(videoUrl))) {
        return;
    }
    // open expanded controls when start the video casting.
    remoteMediaClient.registerCallback(new RemoteMediaClient.Callback() {
        @Override
        public void onStatusUpdated() {
            final Intent intent = new Intent(activity, ExpandedControlsActivity.class);
            activity.startActivity(intent);
            remoteMediaClient.unregisterCallback(this);
            // Track video is casted on casting device.
            double currentTime = position / AppConstants.MILLISECONDS_PER_SECOND;
            analyticsRegistry.trackVideoPlaying(videoEntry.videoId, currentTime, videoEntry.eid,
                    videoEntry.lmsUrl, Analytics.Values.GOOGLE_CAST);
        }
    });
    // load video media on remote client / media player.
    remoteMediaClient.load(buildMediaInfo(videoEntry, videoUrl),
            new MediaLoadOptions.Builder()
                    .setAutoplay(autoPlay)
                    .setPlayPosition(position).build());
}
 
Example #23
Source File: GoogleCastDelegate.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onStatusUpdated() {
    super.onStatusUpdated();
    if (sessionListener != null && castSession != null) {
        final RemoteMediaClient remoteMediaPlayer = castSession.getRemoteMediaClient();
        if (remoteMediaPlayer != null) {
            if (remoteMediaPlayer.getPlayerState() == MediaStatus.PLAYER_STATE_IDLE &&
                    remoteMediaPlayer.getIdleReason() == MediaStatus.IDLE_REASON_FINISHED) {
                sessionListener.onVideoComplete();
            } else if (remoteMediaPlayer.getPlayerState() == MediaStatus.PLAYER_STATE_PLAYING) {
                sessionListener.onVideoPlaying();
            }
        }
    }
}
 
Example #24
Source File: MainActivity.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
    if( CastContext.getSharedInstance(this).getSessionManager().getCurrentCastSession() != null
            && CastContext.getSharedInstance(this).getSessionManager().getCurrentCastSession().getRemoteMediaClient() != null ) {

        RemoteMediaClient remoteMediaClient = CastContext.getSharedInstance(this).getSessionManager().getCurrentCastSession().getRemoteMediaClient();


        if( remoteMediaClient.getMediaInfo() != null &&
                remoteMediaClient.getMediaInfo().getMetadata() != null
                && mAdapter.getItem(position).equalsIgnoreCase(
                    remoteMediaClient.getMediaInfo().getMetadata().getString(MediaMetadata.KEY_TITLE))) {

            startActivity(new Intent(this, ExpandedControlsActivity.class));

        } else {
            MediaMetadata metadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);

            metadata.putString(MediaMetadata.KEY_TITLE, mAdapter.getItem(position));
            metadata.putString(MediaMetadata.KEY_SUBTITLE, "Subtitle");
            metadata.addImage(new WebImage(Uri.parse(getString(R.string.movie_poster))));
            metadata.addImage(new WebImage(Uri.parse(getString(R.string.movie_poster))));

            MediaInfo mediaInfo = new MediaInfo.Builder(getString(R.string.movie_link))
                    .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
                    .setContentType("videos/mp4")
                    .setMetadata(metadata)
                    .build();


            remoteMediaClient.load(mediaInfo, true, 0);
        }
    } else {
        startActivity(new Intent(this, MovieDetailActivity.class));
    }
}
 
Example #25
Source File: CastApplication.java    From googleads-ima-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onSessionEnding(CastSession castSession) {
  if (mCastSession != null && !mCastAdPlaying) {
    RemoteMediaClient remoteMediaClient = mCastSession.getRemoteMediaClient();
    if (remoteMediaClient != null) {
      double videoPosition = remoteMediaClient.getApproximateStreamPosition();
      mCastContentTime = videoPosition / 1000.0;
    }
  }
}
 
Example #26
Source File: GoogleCastModule.java    From react-native-google-cast with MIT License 5 votes vote down vote up
@ReactMethod
public void castMedia(final ReadableMap params) {
    if (mCastSession == null) {
        return;
    }

    getReactApplicationContext().runOnUiQueueThread(new Runnable() {
        @Override
        public void run() {
            RemoteMediaClient remoteMediaClient = mCastSession.getRemoteMediaClient();
            if (remoteMediaClient == null) {
                return;
            }

            Integer seconds = null;
            if (params.hasKey("playPosition")) {
                seconds = params.getInt("playPosition");
            }
            if (seconds == null) {
                seconds = 0;
            }

            MediaInfo mediaInfo = MediaInfoBuilder.buildMediaInfo(params);
            remoteMediaClient.load(mediaInfo, true, seconds * 1000);

            Log.e(REACT_CLASS, "Casting media... ");
        }
    });
}
 
Example #27
Source File: QueueListViewFragment.java    From CastVideos-android with Apache License 2.0 5 votes vote down vote up
private RemoteMediaClient getRemoteMediaClient() {
    CastSession castSession =
            CastContext.getSharedInstance(getContext()).getSessionManager()
                    .getCurrentCastSession();
    if (castSession != null && castSession.isConnected()) {
        return castSession.getRemoteMediaClient();
    }
    return null;
}
 
Example #28
Source File: CastyPlayer.java    From Casty with MIT License 5 votes vote down vote up
private RemoteMediaClient.Listener createRemoteMediaClientListener() {
    return new RemoteMediaClient.Listener() {
        @Override
        public void onStatusUpdated() {
            onMediaLoadedListener.onMediaLoaded();
            remoteMediaClient.removeListener(this);
        }

        @Override
        public void onMetadataUpdated() {
            //no-op
        }

        @Override
        public void onQueueStatusUpdated() {
            //no-op
        }

        @Override
        public void onPreloadStatusUpdated() {
            //no-op
        }

        @Override
        public void onSendingRemoteMediaRequest() {
            //no-op
        }

        @Override
        public void onAdBreakStatusUpdated() {
            //no-op
        }
    };
}
 
Example #29
Source File: QueueListViewFragment.java    From CastVideos-android with Apache License 2.0 5 votes vote down vote up
private void onContainerClicked(View view) {
    RemoteMediaClient remoteMediaClient = getRemoteMediaClient();
    if (remoteMediaClient == null) {
        return;
    }
    MediaQueueItem item = (MediaQueueItem) view.getTag(R.string.queue_tag_item);
    if (mProvider.isQueueDetached()) {
        Log.d(TAG, "Is detached: itemId = " + item.getItemId());

        int currentPosition = mProvider.getPositionByItemId(item.getItemId());
        MediaQueueItem[] items = Utils.rebuildQueue(mProvider.getItems());
        remoteMediaClient.queueLoad(items, currentPosition,
                MediaStatus.REPEAT_MODE_REPEAT_OFF, null);
    } else {
        int currentItemId = mProvider.getCurrentItemId();
        if (currentItemId == item.getItemId()) {
            // We selected the one that is currently playing so we take the user to the
            // full screen controller
            CastSession castSession = CastContext.getSharedInstance(
                    getContext().getApplicationContext())
                    .getSessionManager().getCurrentCastSession();
            if (castSession != null) {
                Intent intent = new Intent(getActivity(), ExpandedControlsActivity.class);
                startActivity(intent);
            }
        } else {
            // a different item in the queue was selected so we jump there
            remoteMediaClient.queueJumpToItem(item.getItemId(), null);
        }
    }
}
 
Example #30
Source File: QueueDataProvider.java    From CastVideos-android with Apache License 2.0 5 votes vote down vote up
public void onUpcomingStopClicked(View view, MediaQueueItem upcomingItem) {
    RemoteMediaClient remoteMediaClient = getRemoteMediaClient();
    if (remoteMediaClient == null) {
        return;
    }
    // need to truncate the queue on the remote device so that we can complete the playback of
    // the current item but not go any further. Alternatively, one could just stop the playback
    // here, if that was acceptable.
    int position = getPositionByItemId(upcomingItem.getItemId());
    int[] itemIds = new int[getCount() - position];
    for (int i = 0; i < itemIds.length; i++) {
        itemIds[i] = mQueue.get(i + position).getItemId();
    }
    remoteMediaClient.queueRemoveItems(itemIds, null);
}