CodeReview - background playback & quick reply

Background play

Function description

On demand and playback support background and lock screen playback.

Realization idea

In fact, our SDK already supports background playback and lock screen playback. The previous method to control background playback is to pause playback when the interface is onPause. This function actually creates a notification in the notification bar and updates the video status in real time.

Concrete implementation

Create notification channel

Starting from Android 8.0, all notifications must be assigned channels, otherwise the notifications will not be displayed

Channelid is the unique identifier of the notification channel. First, query whether the notification channel already exists according to the notification channel ID. if it does not exist, create the channel.

After creating the notification channel, check whether the user turns on the notification switch. If not, jump to the notification setting interface. The method in the above figure is showNotificationDialog()

Create and send notifications when video resources are ready

Step 1: after creating the notification channel, you can publish the notification at any time. We publish the first notification after the initialization of the video information, because you can get all the information of the video at this time, and the callback is videoView.getObservableVideoStatus(). Create notification call

You can see that the notification is created to assign values to the attributes in the class, and the final method called is:

private void createNotification() {
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY |
                    PlaybackStateCompat.ACTION_PLAY_PAUSE |
            .setState(PlaybackStateCompat.STATE_PLAYING, currentDuration, 1f, 0);
    mediaSession.setMetadata(new MediaMetadataCompat.Builder()
            .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, BitmapFactory.decodeResource(getResources(), largeIcon))
    Intent contentIntent = new Intent(this, toActivity);
    contentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentPendingIntent = PendingIntent.getActivity(this, 0, contentIntent, 0);
    Intent controlIntent = new Intent(UIEventKey.ACTION_NOTIFICATION_CONTROL_PLAY);
    PendingIntent controlPendingIntent = PendingIntent.getBroadcast(this, 0, controlIntent, 0);
    notification = new NotificationCompat.Builder(this, channelId)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), largeIcon))
            .addAction(new NotificationCompat.Action(playIcon, "pause_or_play", controlPendingIntent))
    notification.flags = NotificationCompat.FLAG_NO_CLEAR;
    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(notificationId, notification);

Update notification on playback

When the player plays, we need to update the playback progress in real time. Its essence is to change the attribute value and create a notification like cnannelId, so as to complete the notification update.

Destroy the notification after playback.

Destruction notice is required at the time of final exit.

Chat quick reply

Function description

Add a line of quick reply words above the input box of sending message and slide them. Click the quick reply words to directly input them into the input box. The quick reply words are sorted according to the number of clicks and stored locally.

Realization idea

  1. Creating a View is to request data.
  2. The returned data is received and merged with the local data.
  3. Click to update the number of clicks and save.

Concrete implementation

Read local quick reply

Get remote quick reply and merge with local quick reply)

Update times when clicked

Save data on Destruction

Tags: Java Android

Posted on Thu, 21 Oct 2021 22:59:04 -0400 by Twister1004