Android: Background Playback

In this topic, you will learn about enabling Background Playback.

Introduction

The previous Brightcove SDK used to offer background playback on its main core, but due to policy changes in the Google Play Store, this was removed from the SDK core with the new version. You will need to implement the new Playback-Notification plugin to integrate support for background playback. See Android SDK v9.0 Migration Guide for more details.

Playback-Notification plugin implementation

The Android SDK offers a new plugin for creating a notification for playback in the background only.

The purpose of this plugin is to separate logic for background and foreground playback. Here is the list of steps for the implementation:

  1. To add the new dependency, include the new plugin in your build.gradle file:

                    dependencies {
                        implementation "com.brightcove.player:android-playback-notification-plugin:[SDK_VERSION]"
                    }
                    
                
  2. In the activity where Brightcove player lives, you can create the PlaybackNotification through the BackgroundPlaybackNotification class.

    • Here is the Kotlin version:
      
                              // Kotlin 
                              val videoDisplayComponent = baseVideoView.videoDisplay as ExoPlayerVideoDisplayComponent?
                              val myNotification = BackgroundPlaybackNotification.getInstance(this)
                              
                          
    • Here is the Java version:
                              
                                  // Java
                                  ExoPlayerVideoDisplayComponent displayComponent = ((ExoPlayerVideoDisplayComponent) brightcoveVideoView.getVideoDisplay());
                                  PlaybackNotification notification = BackgroundPlaybackNotification.getInstance(this);
                              
                          
  3. The notification requires a config object; the default action is to set a new PlaybackNotificationConfig object. Finally, the notification will require the player that will be attached to it:

    • Here is the Kotlin version:
      
                              // Kotlin 
                              myNotification?.setConfig(PlaybackNotificationConfig(this))
                              myNotification?.playback = videoDisplayComponent?.playback                
                              
                          
    • Here is the Java version:
                              
                                  // Java
                                  myNotification.setConfig(new PlaybackNotificationConfig(this));
                                  myNotification.setPlayback(displayComponent.getPlayback());
                              
                          
  4. Once the notification is created and configured, it is set in the ExoPlayerVideoDisplayComponent class:

    • Here is the Kotlin version:
                              
                              // Kotlin 
                              videoDisplayComponent?.let {
                                  videoDisplayComponent.playbackNotification = myNotification
                              }
                                              
                              
                          
    • Here is the Java version:
                              
                                  // Java
                                  if (videoDisplayComponent != null ) {
                                      videoDisplayComponent.setPlaybackNotification(createPlaybackNotification());
                                   }                     
                              
                          
  5. Here is the complete code:

    • Here is the Kotlin version:
                              
                                  // Kotlin 
                                  val videoDisplayComponent = baseVideoView.videoDisplay as ExoPlayerVideoDisplayComponent?
                                  val myNotification = BackgroundPlaybackNotification.getInstance(this)
                                  myNotification?.setConfig(PlaybackNotificationConfig(this))
                                  myNotification?.playback = videoDisplayComponent?.playback
      
                                  videoDisplayComponent?.let {
                                      videoDisplayComponent.playbackNotification = myNotification
                                  }               
                              
                          
    • Here is the Java version:
                              
                                  // Java
                                  ExoPlayerVideoDisplayComponent displayComponent = ((ExoPlayerVideoDisplayComponent) brightcoveVideoView.getVideoDisplay());
                                  PlaybackNotification notification = BackgroundPlaybackNotification.getInstance(this);
                                  myNotification.setConfig(new PlaybackNotificationConfig(this));
                                  myNotification.setPlayback(displayComponent.getPlayback());
      
                                  if (videoDisplayComponent != null ) {
                                      videoDisplayComponent.setPlaybackNotification(createPlaybackNotification());
                                  }
                       
                              
                          

Customizing the Playback Notification

With the PlaybackNotificationConfig class, you can customize the notification through its Builder class. It offers the options to set the color, icon, buttons to be displayed, channel importance, visibility, etc.

It also supports setting a listener to be notified when the notification is posted and removed.

  • Here is the Kotlin version:
                    
                        // Kotlin 
                        val videoDisplayComponent = baseVideoView.videoDisplay as ExoPlayerVideoDisplayComponent
                        val notification = BackgroundPlaybackNotification.getInstance(context)
                        val config = PlaybackNotificationConfig(context)
                            .setStreamTypes(*PlaybackNotification.StreamType.entries.toTypedArray())
                            .setUseRewindAction(false)
                            .setUseRewindActionInCompactView(false)
                            .setUseNextAction(false)
                            .setUseNextActionInCompactView(false)
                            .setUsePreviousAction(false)
                            .setUsePreviousActionInCompactView(false)
                            .setUseFastForwardAction(false)
                            .setUseFastForwardActionInCompactView(false)
                            .setAdapter(object : PlaybackNotification.MediaDescriptionAdapter {
                                override fun getCurrentContentTitle(playback: MediaPlayback<*>?): CharSequence {
                                    return "My custom title"
                                }
                                override fun createCurrentContentIntent(playback: MediaPlayback<*>?): PendingIntent? {
                                    // Create an intent to launch your activity when the notification is clicked
                                    val intent = Intent(context, MyActivity::class.java)
                                    // Create a PendingIntent from the intent
                                    return PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
                                }
                                override fun getCurrentContentText(playback: MediaPlayback<*>?): CharSequence {
                                    return "My custom text"
                                }
                                override fun getCurrentSubText(playback: MediaPlayback<*>?): CharSequence {
                                    return "My custom subtext"
                                }
                            })
                        notification?.setConfig(config)
                        notification?.playback = videoDisplayComponent.playback
    
                    
                
  • Here is the Java version:
                    
                        // Java
                        ExoPlayerVideoDisplayComponent displayComponent = ((ExoPlayerVideoDisplayComponent) brightcoveVideoView.getVideoDisplay());
                        PlaybackNotification notification = BackgroundPlaybackNotification.getInstance(this);
                        PlaybackNotificationConfig config = new PlaybackNotificationConfig(context)
                            .setStreamTypes(PlaybackNotification.StreamType.values())
                            .setUseRewindAction(false)
                            .setUseRewindActionInCompactView(false)
                            .setUsePreviousAction(false)
                            .setUsePreviousActionInCompactView(false)
                            .setUseNextAction(false)
                            .setUseNextActionInCompactView(false)
                            .setUseFastForwardAction(false)
                            .setUseFastForwardActionInCompactView(false)
                            .setAdapter(new PlaybackNotification.MediaDescriptionAdapter() {
                                @Override
                                public CharSequence getCurrentContentTitle(MediaPlayback playback) {
                                    return "Title blah"; // Replace with actual title logic
                                }
    
                                @Nullable
                                @Override
                                public PendingIntent createCurrentContentIntent(MediaPlayback playback) {
                                    Intent intent = new Intent(context, MyActivity.class);
                                    // Create a PendingIntent from the intent
                                    return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
                                }
    
                                @Nullable
                                @Override
                                public CharSequence getCurrentContentText(MediaPlayback playback) {
                                    return "Text";
                                }
    
                                @Nullable
                                @Override
                                public CharSequence getCurrentSubText(MediaPlayback playback) {
                                    return "Subtext";
                                }
                            });
                            notification.setConfig(config);
                            notification.setPlayback(displayComponent.getPlayback());