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 manager = PlayerNotificationManager.Builder(
                            config.context, config.notificationId, config.channelId
                        ).setChannelImportance(config.channelImportance)
                        .setChannelNameResourceId(config.channelNameResourceId)
                        .setMediaDescriptionAdapter(createAdapter(config))
                        .setNotificationListener(listener)
                        .build()
                        manager.setSmallIcon(config.smallIconResourceId)
                        manager.setUseNextAction(config.useNextAction)
                        manager.setUsePreviousAction(config.usePreviousAction)
                        manager.setUseNextActionInCompactView(config.useNextActionInCompactView)
                        manager.setUsePreviousActionInCompactView(config.usePreviousActionInCompactView)
                        manager.setColor(config.color)
                        manager.setColorized(config.colorized)
                        manager.setUseFastForwardAction(config.useFastForwardAction)
                        manager.setUseFastForwardActionInCompactView(config.useFastForwardActionInCompactView)
                        manager.setUsePlayPauseActions(config.usePlayPauseActions)
                        manager.setUseRewindAction(config.useRewindAction)
                        manager.setUseRewindActionInCompactView(config.useRewindActionInCompactView)
                        manager.setUseStopAction(false)
                        manager.setVisibility(config.visibility)
                        manager.setPriority(config.priority)
                        manager.setUseChronometer(true)
    
                    
                
  • Here is the Java version:
                    
                        // Java
                        PlayerNotificationManager manager = new PlayerNotificationManager.Builder(
                            config.context, config.notificationId, config.channelId
                        ).setChannelImportance(config.channelImportance)
                            .setChannelNameResourceId(config.channelNameResourceId)
                            .setMediaDescriptionAdapter(createAdapter(config))
                            .setNotificationListener(listener)
                            .build()
                        manager.setSmallIcon(config.smallIconResourceId)
                        manager.setUseNextAction(config.useNextAction)
                        manager.setUsePreviousAction(config.usePreviousAction)
                        manager.setUseNextActionInCompactView(config.useNextActionInCompactView)    
                        manager.setUsePreviousActionInCompactView(config.usePreviousActionInCompactView)
                        manager.setColor(config.color)
                        manager.setColorized(config.colorized)
                        manager.setUseFastForwardAction(config.useFastForwardAction)
                        manager.setUseFastForwardActionInCompactView(config.useFastForwardActionInCompactView)
                        manager.setUsePlayPauseActions(config.usePlayPauseActions)
                        manager.setUseRewindAction(config.useRewindAction)       
                        manager.setUseRewindActionInCompactView(config.useRewindActionInCompactView)
                        manager.setUseStopAction(false)
                        manager.setVisibility(config.visibility)
                        manager.setPriority(config.priority)
                        manager.setUseChronometer(true)