Package com.castlabs.sdk.mediasession

# Class MediaSessionPlugin

java.lang.Object
com.castlabs.android.Plugin
com.castlabs.sdk.mediasession.MediaSessionPlugin

public final class MediaSessionPlugin extends Plugin
MediaSessionPlugin integrates castLabs Player SDK with AndroidX Media3 session APIs. The plugin needs to be enabled before SDK initialization:

       public class MyApp extends Application {
         @Override
         public void onCreate() {
              super.onCreate();

              PlayerSDK.register(new MediaSessionPlugin());
              PlayerSDK.init(getApplicationContext());
          }
       }

Registering the plugin does not create Media Session yet and the application will need to explicitly request Media Session by calling `enableMediaSession(PlayerController)`. Media Session is linked to the `PlayerController` and therefore the application has to provide `PlayerController` instance when enabling Media Session. The code below enables Media Session when `PlayerController` is created:

         public class SimplePlaybackDemo extends Activity {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                    // ...
              PlayerView playerView = (PlayerView) findViewById(R.id.player_view);

              MediaSessionPlugin.enableMediaSession(playerView.getPlayerController());

              // ...
              PlayerConfig playerConfig;
              // playerConfig = ...
              playerController.open(playerConfig);
            }

            @Override
            protected void onResume() {
                        super.onResume();
                        // ...
                        PlayerView playerView = (PlayerView) findViewById(R.id.player_view);

                        MediaSessionPlugin.enableMediaSession(playerView.getPlayerController());
                        playerView.getLifecycleDelegate().resume();
                      }
         }

Media Session lifecycle follows `PlayerController`'s one and is closed automatically upon `PlayerController.destroy()`. However, it is also possible to close Media Session explicitly:

         // ...
         PlayerView playerView = (PlayerView) findViewById(R.id.player_view);
               MediaSessionPlugin.disableMediaSession(playerView.getPlayerController());
         // ...

Since:
  4.2.47

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static boolean`
  `enableArtworkUriPlaceholder`
  If set to `true`, a placeholder artwork URI will be used when no artwork URI is provided in metadata.
  `static final String`
  `METADATA_KEY_ARTIST`
  Metadata key used to set `MediaMetadata.artist`
  `static final String`
  `METADATA_KEY_ARTWORK_URI`
  Metadata key used to set `MediaMetadata.artworkUri`
  `static final String`
  `METADATA_KEY_TITLE`
  Metadata key used to set `MediaMetadata.title`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `MediaSessionPlugin()`
  Constructs default MediaSessionPlugin

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `disableMediaSession(PlayerController playerController)`
  Closes Media Session
  `static void`
  `enableMediaSession(PlayerController playerController)`
  Creates and enables Media Session with default configuration.
  `static void`
  `enableMediaSession(PlayerController playerController, MediaSessionBuilder sessionBuilder)`
  Creates and enables Media Session with custom configuration.
  `String`
  `getId()`
  `static androidx.media3.session.MediaSession`
  `getMediaSession(PlayerController playerController)`
  Gets Media Session instance e.g.
  `static boolean`
  `isEnabled(PlayerController playerController)`
  Checks if Media Session is enabled

  ### Methods inherited from class com.castlabs.android.Plugin

  `init, onRemotePlay`

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### METADATA_KEY_TITLE

    public static final String METADATA_KEY_TITLE
    Metadata key used to set `MediaMetadata.title`
    See Also:
      - Constant Field Values

  - ### METADATA_KEY_ARTIST

    public static final String METADATA_KEY_ARTIST
    Metadata key used to set `MediaMetadata.artist`
    See Also:
      - Constant Field Values

  - ### METADATA_KEY_ARTWORK_URI

    public static final String METADATA_KEY_ARTWORK_URI
    Metadata key used to set `MediaMetadata.artworkUri`
    See Also:
      - Constant Field Values

  - ### enableArtworkUriPlaceholder

    public static boolean enableArtworkUriPlaceholder
    If set to `true`, a placeholder artwork URI will be used when no artwork URI is provided in metadata.

- ## Constructor Details

  - ### MediaSessionPlugin

    public MediaSessionPlugin()
    Constructs default MediaSessionPlugin

- ## Method Details

  - ### enableMediaSession

    public static void enableMediaSession(@NonNull PlayerController playerController)
    Creates and enables Media Session with default configuration. Media Session is then linked to the provided `PlayerController`
    Parameters:
      `playerController` - The `PlayerController` to link the Media Session to

  - ### enableMediaSession

    public static void enableMediaSession(@NonNull PlayerController playerController, @Nullable MediaSessionBuilder sessionBuilder)
    Creates and enables Media Session with custom configuration. Media Sessionand is then linked to the provided `PlayerController`
    Parameters:
      `playerController` - The `PlayerController` to link the Media Session to
      `sessionBuilder` - The session builder to customize Media Session or `null` to use defaults

  - ### disableMediaSession

    public static void disableMediaSession(@NonNull PlayerController playerController)
    Closes Media Session
    Parameters:
      `playerController` - The `PlayerController` to which Media Session is linked to

  - ### isEnabled

    public static boolean isEnabled(@NonNull PlayerController playerController)
    Checks if Media Session is enabled
    Parameters:
      `playerController` - The `PlayerController` to which Media Session is linked to
    Returns:
      `true` if Media Session is enabled and `false` otherwise

  - ### getMediaSession

    @Nullable public static androidx.media3.session.MediaSession getMediaSession(@NonNull PlayerController playerController)
    Gets Media Session instance e.g. when access to Media Session Compat Token etc is needed. The code below links Media Session with system media notification:

             PlayerView playerView = (PlayerView) findViewById(R.id.player_view);

             NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID);
             builder.setSmallIcon(R.drawable.notifications_icon);

             MediaSession mediaSession = MediaSessionPlugin.getMediaSession(playerView.getPlayerController());
                   if (mediaSession != null) {
                          builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(mediaSession.getSessionCompatToken()));
             }

             builder.build();

    Parameters:
      `playerController` - The `PlayerController` to which Media Session is linked to
    Returns:
      Media Session instance or `null` if not available

  - ### getId

    @NonNull public String getId()
    Specified by:
      `getId` in class `Plugin`