Java Code Examples for com.google.android.gms.cast.RemoteMediaPlayer#setOnStatusUpdatedListener()

The following examples show how to use com.google.android.gms.cast.RemoteMediaPlayer#setOnStatusUpdatedListener() . 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: 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 2
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 3
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 4
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 5
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());
}