com.google.android.gms.cast.RemoteMediaPlayer Java Examples

The following examples show how to use com.google.android.gms.cast.RemoteMediaPlayer. 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: VideoCastManager.java    From android with Apache License 2.0 6 votes vote down vote up
/**
 * Loads a media. For this to succeed, you need to have successfully launched the application.
 *
 * @param media
 * @param autoPlay   If <code>true</code>, playback starts after load
 * @param position   Where to start the playback (only used if autoPlay is <code>true</code>).
 *                   Units is milliseconds.
 * @param customData Optional {@link JSONObject} data to be passed to the cast device
 * @throws NoConnectionException
 * @throws TransientNetworkDisconnectionException
 */
public void loadMedia(MediaInfo media, boolean autoPlay, int position, JSONObject customData)
        throws TransientNetworkDisconnectionException, NoConnectionException {
    LOGD(TAG, "loadMedia: " + media);
    checkConnectivity();
    if (media == null) {
        return;
    }
    if (mRemoteMediaPlayer == null) {
        LOGE(TAG, "Trying to load a video with no active media session");
        throw new NoConnectionException();
    }

    mRemoteMediaPlayer.load(mApiClient, media, autoPlay, position, customData)
            .setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {

                @Override
                public void onResult(MediaChannelResult result) {
                    if (!result.getStatus().isSuccess()) {
                        onFailed(R.string.failed_load, result.getStatus().getStatusCode());
                    }

                }
            });
}
 
Example #2
Source File: VideoCastManager.java    From android with Apache License 2.0 6 votes vote down vote up
/**
 * Seeks to the given point without changing the state of the player, i.e. after seek is
 * completed, it resumes what it was doing before the start of seek.
 *
 * @param position in milliseconds
 * @throws NoConnectionException
 * @throws TransientNetworkDisconnectionException
 */
public void seek(int position) throws TransientNetworkDisconnectionException,
        NoConnectionException {
    LOGD(TAG, "attempting to seek media");
    checkConnectivity();
    if (mRemoteMediaPlayer == null) {
        LOGE(TAG, "Trying to seek a video with no active media session");
        throw new NoConnectionException();
    }
    mRemoteMediaPlayer.seek(mApiClient,
            position,
            RemoteMediaPlayer.RESUME_STATE_UNCHANGED).
            setResultCallback(new ResultCallback<MediaChannelResult>() {

                @Override
                public void onResult(MediaChannelResult result) {
                    if (!result.getStatus().isSuccess()) {
                        onFailed(R.string.failed_seek, result.getStatus().getStatusCode());
                    }
                }

            });
}
 
Example #3
Source File: VideoCastManager.java    From android with Apache License 2.0 6 votes vote down vote up
/**
 * Seeks to the given point and starts playback regardless of the starting state.
 *
 * @param position in milliseconds
 * @throws NoConnectionException
 * @throws TransientNetworkDisconnectionException
 * @throws CastException
 */
public void seekAndPlay(int position) throws TransientNetworkDisconnectionException,
        NoConnectionException {
    LOGD(TAG, "attempting to seek media");
    checkConnectivity();
    if (mRemoteMediaPlayer == null) {
        LOGE(TAG, "Trying to seekAndPlay a video with no active media session");
        throw new NoConnectionException();
    }
    ResultCallback<MediaChannelResult> resultCallback =
            new ResultCallback<MediaChannelResult>() {

                @Override
                public void onResult(MediaChannelResult result) {
                    if (!result.getStatus().isSuccess()) {
                        onFailed(R.string.failed_seek, result.getStatus().getStatusCode());
                    }
                }

            };
    mRemoteMediaPlayer.seek(mApiClient,
            position,
            RemoteMediaPlayer.RESUME_STATE_PLAY).setResultCallback(resultCallback);
}
 
Example #4
Source File: ChromeCastMediaPlayer.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void onAttach() {
    mRemoteMediaPlayer = new RemoteMediaPlayer();
    mRemoteMediaPlayer.setOnStatusUpdatedListener(() -> {
        if (mRemoteMediaPlayer.getMediaStatus() == null) {
            return;
        }
        mCallbacks.onChromeCastMediaPlayerStatusUpdate(mRemoteMediaPlayer.getMediaStatus());
    });
    mRemoteMediaPlayer.setOnMetadataUpdatedListener(() -> {
    });

    try {
        Cast.CastApi.setMessageReceivedCallbacks(mController.getGoogleApiClient(),
                mRemoteMediaPlayer.getNamespace(), mRemoteMediaPlayer);
    } catch (Exception e) {
        if (BuildConfig.DEBUG) {
            e.printStackTrace();
        }
    }
}
 
Example #5
Source File: MainActivity.java    From AndroidDemoProjects with Apache License 2.0 6 votes vote down vote up
private void initRemoteMediaPlayer() {
    mRemoteMediaPlayer = new RemoteMediaPlayer();
    mRemoteMediaPlayer.setOnStatusUpdatedListener( new RemoteMediaPlayer.OnStatusUpdatedListener() {
        @Override
        public void onStatusUpdated() {
            MediaStatus mediaStatus = mRemoteMediaPlayer.getMediaStatus();
            mIsPlaying = mediaStatus.getPlayerState() == MediaStatus.PLAYER_STATE_PLAYING;
        }
    });

    mRemoteMediaPlayer.setOnMetadataUpdatedListener( new RemoteMediaPlayer.OnMetadataUpdatedListener() {
        @Override
        public void onMetadataUpdated() {
        }
    });
}
 
Example #6
Source File: MainActivity.java    From AndroidDemoProjects with Apache License 2.0 6 votes vote down vote up
private void startVideo() {
    MediaMetadata mediaMetadata = new MediaMetadata( MediaMetadata.MEDIA_TYPE_MOVIE );
    mediaMetadata.putString( MediaMetadata.KEY_TITLE, getString( R.string.video_title ) );

    MediaInfo mediaInfo = new MediaInfo.Builder( getString( R.string.video_url ) )
            .setContentType( getString( R.string.content_type_mp4 ) )
            .setStreamType( MediaInfo.STREAM_TYPE_BUFFERED )
            .setMetadata( mediaMetadata )
            .build();
    try {
        mRemoteMediaPlayer.load( mApiClient, mediaInfo, true )
                .setResultCallback( new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
                    @Override
                    public void onResult( RemoteMediaPlayer.MediaChannelResult mediaChannelResult ) {
                        if( mediaChannelResult.getStatus().isSuccess() ) {
                            mVideoIsLoaded = true;
                            mButton.setText( getString( R.string.pause_video ) );
                        }
                    }
                } );
    } catch( Exception e ) {
    }
}
 
Example #7
Source File: VideoCastManager.java    From UTubeTV with The Unlicense 6 votes vote down vote up
/**
 * Loads a media. For this to succeed, you need to have successfully launched the application.
 *
 * @param media
 * @param autoPlay   If <code>true</code>, playback starts after load
 * @param position   Where to start the playback (only used if autoPlay is <code>true</code>.
 *                   Units is milliseconds.
 * @param customData Optional JSONObject data to be passed to the cast device
 * @throws NoConnectionException
 * @throws TransientNetworkDisconnectionException
 */
public void loadMedia(MediaInfo media, boolean autoPlay, int position, JSONObject customData) throws TransientNetworkDisconnectionException, NoConnectionException {
  CastUtils.LOGD(TAG, "loadMedia: " + media);
  checkConnectivity();
  if (media == null) {
    return;
  }
  if (mRemoteMediaPlayer == null) {
    CastUtils.LOGE(TAG, "Trying to load a video with no active media session");
    throw new NoConnectionException();
  }

  mRemoteMediaPlayer.load(mApiClient, media, autoPlay, position, customData)
      .setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {

        @Override
        public void onResult(MediaChannelResult result) {
          if (!result.getStatus().isSuccess()) {
            onFailed(R.string.failed_load, result.getStatus().getStatusCode());
          }

        }
      });
}
 
Example #8
Source File: VideoCastManager.java    From UTubeTV with The Unlicense 6 votes vote down vote up
/**
 * Seeks to the given point without changing the state of the player, i.e. after seek is
 * completed, it resumes what it was doing before the start of seek.
 * <p/>
 * position in milliseconds
 */
public void seek(int position) throws TransientNetworkDisconnectionException, NoConnectionException {
  CastUtils.LOGD(TAG, "attempting to seek media");
  checkConnectivity();
  if (mRemoteMediaPlayer == null) {
    CastUtils.LOGE(TAG, "Trying to seek a video with no active media session");
    throw new NoConnectionException();
  }
  mRemoteMediaPlayer.seek(mApiClient, position, RemoteMediaPlayer.RESUME_STATE_UNCHANGED).
      setResultCallback(new ResultCallback<MediaChannelResult>() {

        @Override
        public void onResult(MediaChannelResult result) {
          if (!result.getStatus().isSuccess()) {
            onFailed(R.string.failed_seek, result.getStatus().getStatusCode());
          }
        }

      });
}
 
Example #9
Source File: VideoCastManager.java    From UTubeTV with The Unlicense 6 votes vote down vote up
/**
 * Seeks to the given point and starts playback regardless of the starting state.
 * <p/>
 * position in milliseconds
 */
public void seekAndPlay(int position) throws TransientNetworkDisconnectionException, NoConnectionException {
  CastUtils.LOGD(TAG, "attempting to seek media");
  checkConnectivity();
  if (mRemoteMediaPlayer == null) {
    CastUtils.LOGE(TAG, "Trying to seekAndPlay a video with no active media session");
    throw new NoConnectionException();
  }
  ResultCallback<MediaChannelResult> resultCallback = new ResultCallback<MediaChannelResult>() {

    @Override
    public void onResult(MediaChannelResult result) {
      if (!result.getStatus().isSuccess()) {
        onFailed(R.string.failed_seek, result.getStatus().getStatusCode());
      }
    }

  };
  mRemoteMediaPlayer.seek(mApiClient, position, RemoteMediaPlayer.RESUME_STATE_PLAY)
      .setResultCallback(resultCallback);
}
 
Example #10
Source File: CastSessionImpl.java    From delion with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes a new {@link CastSessionImpl} instance.
 * @param apiClient The Google Play Services client used to create the session.
 * @param sessionId The session identifier to use with the Cast SDK.
 * @param origin The origin of the frame requesting the route.
 * @param tabId The id of the tab containing the frame requesting the route.
 * @param isIncognito Whether the route is beging requested from an Incognito profile.
 * @param source The {@link MediaSource} corresponding to this session.
 * @param routeProvider The {@link CastMediaRouteProvider} instance managing this session.
 */
public CastSessionImpl(
        GoogleApiClient apiClient,
        String sessionId,
        ApplicationMetadata metadata,
        String applicationStatus,
        CastDevice castDevice,
        String origin,
        int tabId,
        boolean isIncognito,
        MediaSource source,
        CastMediaRouteProvider routeProvider) {
    mSessionId = sessionId;
    mRouteProvider = routeProvider;
    mApiClient = apiClient;
    mSource = source;
    mApplicationMetadata = metadata;
    mApplicationStatus = applicationStatus;
    mCastDevice = castDevice;
    mMessageHandler = mRouteProvider.getMessageHandler();
    mMessageChannel = new CastMessagingChannel(this);
    updateNamespaces();

    final Context context = ContextUtils.getApplicationContext();

    if (mNamespaces.contains(CastMessageHandler.MEDIA_NAMESPACE)) {
        mMediaPlayer = new RemoteMediaPlayer();
        mMediaPlayer.setOnStatusUpdatedListener(
                new RemoteMediaPlayer.OnStatusUpdatedListener() {
                    @Override
                    public void onStatusUpdated() {
                        MediaStatus mediaStatus = mMediaPlayer.getMediaStatus();
                        if (mediaStatus == null) return;

                        int playerState = mediaStatus.getPlayerState();
                        if (playerState == MediaStatus.PLAYER_STATE_PAUSED
                                || playerState == MediaStatus.PLAYER_STATE_PLAYING) {
                            mNotificationBuilder.setPaused(
                                    playerState != MediaStatus.PLAYER_STATE_PLAYING);
                            mNotificationBuilder.setActions(MediaNotificationInfo.ACTION_STOP
                                    | MediaNotificationInfo.ACTION_PLAY_PAUSE);
                        } else {
                            mNotificationBuilder.setActions(MediaNotificationInfo.ACTION_STOP);
                        }
                        MediaNotificationManager.show(context, mNotificationBuilder.build());
                    }
                });
        mMediaPlayer.setOnMetadataUpdatedListener(
                new RemoteMediaPlayer.OnMetadataUpdatedListener() {
                    @Override
                    public void onMetadataUpdated() {
                        setNotificationMetadata(mNotificationBuilder);
                        MediaNotificationManager.show(context, mNotificationBuilder.build());
                    }
                });
    }

    Intent contentIntent = Tab.createBringTabToFrontIntent(tabId);
    if (contentIntent != null) {
        contentIntent.putExtra(MediaNotificationUma.INTENT_EXTRA_NAME,
                MediaNotificationUma.SOURCE_PRESENTATION);
    }
    mNotificationBuilder = new MediaNotificationInfo.Builder()
            .setPaused(false)
            .setOrigin(origin)
            // TODO(avayvod): the same session might have more than one tab id. Should we track
            // the last foreground alive tab and update the notification with it?
            .setTabId(tabId)
            .setPrivate(isIncognito)
            .setActions(MediaNotificationInfo.ACTION_STOP)
            .setContentIntent(contentIntent)
            .setIcon(R.drawable.ic_notification_media_route)
            .setDefaultLargeIcon(R.drawable.cast_playing_square)
            .setId(R.id.presentation_notification)
            .setListener(this);
    setNotificationMetadata(mNotificationBuilder);
    MediaNotificationManager.show(context, mNotificationBuilder.build());
}
 
Example #11
Source File: CastSessionImpl.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes a new {@link CastSessionImpl} instance.
 * @param apiClient The Google Play Services client used to create the session.
 * @param sessionId The session identifier to use with the Cast SDK.
 * @param origin The origin of the frame requesting the route.
 * @param tabId The id of the tab containing the frame requesting the route.
 * @param isIncognito Whether the route is beging requested from an Incognito profile.
 * @param source The {@link MediaSource} corresponding to this session.
 * @param routeProvider The {@link CastMediaRouteProvider} instance managing this session.
 */
public CastSessionImpl(
        GoogleApiClient apiClient,
        String sessionId,
        ApplicationMetadata metadata,
        String applicationStatus,
        CastDevice castDevice,
        String origin,
        int tabId,
        boolean isIncognito,
        MediaSource source,
        CastMediaRouteProvider routeProvider) {
    mSessionId = sessionId;
    mRouteProvider = routeProvider;
    mApiClient = apiClient;
    mSource = source;
    mApplicationMetadata = metadata;
    mApplicationStatus = applicationStatus;
    mCastDevice = castDevice;
    mMessageHandler = mRouteProvider.getMessageHandler();
    mMessageChannel = new CastMessagingChannel(this);
    updateNamespaces();

    final Context context = ContextUtils.getApplicationContext();

    if (mNamespaces.contains(CastMessageHandler.MEDIA_NAMESPACE)) {
        mMediaPlayer = new RemoteMediaPlayer();
        mMediaPlayer.setOnStatusUpdatedListener(
                new RemoteMediaPlayer.OnStatusUpdatedListener() {
                    @Override
                    public void onStatusUpdated() {
                        MediaStatus mediaStatus = mMediaPlayer.getMediaStatus();
                        if (mediaStatus == null) return;

                        int playerState = mediaStatus.getPlayerState();
                        if (playerState == MediaStatus.PLAYER_STATE_PAUSED
                                || playerState == MediaStatus.PLAYER_STATE_PLAYING) {
                            mNotificationBuilder.setPaused(
                                    playerState != MediaStatus.PLAYER_STATE_PLAYING);
                            mNotificationBuilder.setActions(MediaNotificationInfo.ACTION_STOP
                                    | MediaNotificationInfo.ACTION_PLAY_PAUSE);
                        } else {
                            mNotificationBuilder.setActions(MediaNotificationInfo.ACTION_STOP);
                        }
                        MediaNotificationManager.show(context, mNotificationBuilder.build());
                    }
                });
        mMediaPlayer.setOnMetadataUpdatedListener(
                new RemoteMediaPlayer.OnMetadataUpdatedListener() {
                    @Override
                    public void onMetadataUpdated() {
                        setNotificationMetadata(mNotificationBuilder);
                        MediaNotificationManager.show(context, mNotificationBuilder.build());
                    }
                });
    }

    Intent contentIntent = Tab.createBringTabToFrontIntent(tabId);
    if (contentIntent != null) {
        contentIntent.putExtra(MediaNotificationUma.INTENT_EXTRA_NAME,
                MediaNotificationUma.SOURCE_PRESENTATION);
    }
    mNotificationBuilder = new MediaNotificationInfo.Builder()
            .setPaused(false)
            .setOrigin(origin)
            // TODO(avayvod): the same session might have more than one tab id. Should we track
            // the last foreground alive tab and update the notification with it?
            .setTabId(tabId)
            .setPrivate(isIncognito)
            .setActions(MediaNotificationInfo.ACTION_STOP)
            .setContentIntent(contentIntent)
            .setIcon(R.drawable.ic_notification_media_route)
            .setDefaultLargeIcon(R.drawable.cast_playing_square)
            .setId(R.id.presentation_notification)
            .setListener(this);
    setNotificationMetadata(mNotificationBuilder);
    MediaNotificationManager.show(context, mNotificationBuilder.build());
}
 
Example #12
Source File: CastSessionImpl.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes a new {@link CastSessionImpl} instance.
 * @param apiClient The Google Play Services client used to create the session.
 * @param sessionId The session identifier to use with the Cast SDK.
 * @param origin The origin of the frame requesting the route.
 * @param tabId The id of the tab containing the frame requesting the route.
 * @param isIncognito Whether the route is beging requested from an Incognito profile.
 * @param source The {@link MediaSource} corresponding to this session.
 * @param routeProvider The {@link CastMediaRouteProvider} instance managing this session.
 */
public CastSessionImpl(
        GoogleApiClient apiClient,
        String sessionId,
        ApplicationMetadata metadata,
        String applicationStatus,
        CastDevice castDevice,
        String origin,
        int tabId,
        boolean isIncognito,
        MediaSource source,
        CastMediaRouteProvider routeProvider) {
    mSessionId = sessionId;
    mRouteProvider = routeProvider;
    mApiClient = apiClient;
    mSource = source;
    mApplicationMetadata = metadata;
    mApplicationStatus = applicationStatus;
    mCastDevice = castDevice;
    mMessageHandler = mRouteProvider.getMessageHandler();
    mMessageChannel = new CastMessagingChannel(this);
    updateNamespaces();

    if (mNamespaces.contains(CastMessageHandler.MEDIA_NAMESPACE)) {
        mMediaPlayer = new RemoteMediaPlayer();
        mMediaPlayer.setOnStatusUpdatedListener(
                new RemoteMediaPlayer.OnStatusUpdatedListener() {
                    @Override
                    public void onStatusUpdated() {
                        MediaStatus mediaStatus = mMediaPlayer.getMediaStatus();
                        if (mediaStatus == null) return;

                        int playerState = mediaStatus.getPlayerState();
                        if (playerState == MediaStatus.PLAYER_STATE_PAUSED
                                || playerState == MediaStatus.PLAYER_STATE_PLAYING) {
                            mNotificationBuilder.setPaused(
                                    playerState != MediaStatus.PLAYER_STATE_PLAYING);
                            mNotificationBuilder.setActions(MediaNotificationInfo.ACTION_STOP
                                    | MediaNotificationInfo.ACTION_PLAY_PAUSE);
                        } else {
                            mNotificationBuilder.setActions(MediaNotificationInfo.ACTION_STOP);
                        }
                        MediaNotificationManager.show(mNotificationBuilder.build());
                    }
                });
        mMediaPlayer.setOnMetadataUpdatedListener(
                new RemoteMediaPlayer.OnMetadataUpdatedListener() {
                    @Override
                    public void onMetadataUpdated() {
                        setNotificationMetadata(mNotificationBuilder);
                        MediaNotificationManager.show(mNotificationBuilder.build());
                    }
                });
    }

    Intent contentIntent = Tab.createBringTabToFrontIntent(tabId);
    if (contentIntent != null) {
        contentIntent.putExtra(MediaNotificationUma.INTENT_EXTRA_NAME,
                MediaNotificationUma.SOURCE_PRESENTATION);
    }
    mNotificationBuilder = new MediaNotificationInfo.Builder()
            .setPaused(false)
            .setOrigin(origin)
            // TODO(avayvod): the same session might have more than one tab id. Should we track
            // the last foreground alive tab and update the notification with it?
            .setTabId(tabId)
            .setPrivate(isIncognito)
            .setActions(MediaNotificationInfo.ACTION_STOP)
            .setContentIntent(contentIntent)
            .setNotificationSmallIcon(R.drawable.ic_notification_media_route)
            .setDefaultNotificationLargeIcon(R.drawable.cast_playing_square)
            .setId(R.id.presentation_notification)
            .setListener(this);
    setNotificationMetadata(mNotificationBuilder);
    MediaNotificationManager.show(mNotificationBuilder.build());
}
 
Example #13
Source File: VideoCastManager.java    From android with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the active {@link RemoteMediaPlayer} instance. Since there are a number of media
 * control APIs that this library do not provide a wrapper for, client applications can call
 * those methods directly after obtaining an instance of the active {@link RemoteMediaPlayer}.
 *
 * @return
 */
public final RemoteMediaPlayer getRemoteMediaPlayer() {
    return mRemoteMediaPlayer;
}
 
Example #14
Source File: VideoCastManager.java    From UTubeTV with The Unlicense 2 votes vote down vote up
/**
 * Returns the active {@link RemoteMediaPlayer} instance. Since there are a number of media
 * control APIs that this library do not provide a wrapper for, client applications can call
 * those methods directly after obtaining an instance of the active {@link RemoteMediaPlayer}.
 *
 * @return
 */
public final RemoteMediaPlayer getRemoteMediaPlayer() {
  return mRemoteMediaPlayer;
}