Setting Playback Speed on Android Devices

In this topic, you will learn how to add variable speed playback functionality to apps using the Brightcove Native SDK for Android.

Introduction

For some of your apps, you may want to provide the ability for the user to change the playback speed while they are watching videos (ie, eLearning environments). You can add this functionality with the Native SDK for Android.

In your player Activity class, you can create a dialog menu to select from a desired list of playback speeds.

Implementation

To add variable speed playback to your Android app, follow these steps:

  1. First, create the menu where you can select a speed. Here we will use a TextView which, when tapped, will open the menu that contains the speed values. Other menu options include Spinners.

    private TextView playbackSpeed;

    Then, add the TextView to your Activity layout:

    <com.brightcove.player.view.BrightcoveExoPlayerVideoView
       android:id="@+id/brightcove_video_view"
       android:layout_width="match_parent"
       android:layout_height="280dp"
       android:layout_gravity="center_horizontal|top"/>
    
    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:layout_gravity="center_vertical"
       android:layout_below="@id/brightcove_video_view"
       android:padding="20dp">
    
       <TextView
           android:id="@+id/playbackSpeed"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:gravity="end"
           android:padding="20dp"
           android:text="Change Speed"
           android:textAlignment="textEnd" />
    
    </LinearLayout>
  2. Add the playback speed control to your player Activity:

    playbackSpeed = (TextView) findViewById(R.id.playbackSpeed);
    playbackSpeed.setVisibility(View.VISIBLE);
  3. Set an onClick listener on the TextView:

    playbackSpeed.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           showPlayerSpeedDialog();
       }
    });
    
  4. Add a method to show the player speed dialog:

    private void showPlayerSpeedDialog() {
       String[] playerSpeedArrayLabels = {"0.8x", "1.0x", "1.2x", "1.5x", "1.8x", "2.0x"};
    
       PopupMenu popupMenu = new PopupMenu(MainActivity.this, playbackSpeed);
       for (int i = 0; i < playerSpeedArrayLabels.length; i++) {
           popupMenu.getMenu().add(i, i, i, playerSpeedArrayLabels[i]);
       }
       popupMenu.setOnMenuItemClickListener(item -> {
           int id = item.getItemId();
           CharSequence itemTitle = item.getTitle();
           float playbackSpeed = Float.parseFloat(itemTitle.subSequence(0, 3).toString());
           changePlayerSpeed(playbackSpeed, itemTitle.subSequence(0, 3).toString());
           return false;
       });
       popupMenu.show();
    }
    
  5. Add a method to change the player speed to the value selected from the menu. Additionally, the method can set the selected value into the label for the TextView:

    private void changePlayerSpeed(float speed, String speedLabel) {
       // Set playback speed
       ((ExoPlayerVideoDisplayComponent) brightcoveVideoView.getVideoDisplay()).getExoPlayer().setPlaybackParameters(new PlaybackParameters(speed, 1.0f));
       // Set playback speed label
       playbackSpeed.setText("Speed: " + speedLabel + "x");
    }
    

Handling playback problems

If you encounter problems with setting higher or lower playback speeds, consider restricting whether to show the playback speed control, or restricting the speeds available, based on:

  • OS level using:

    Build.VERSION_CODES.<API_LEVEL>
  • Manufacturer using: (reliability is not guaranteed)

    Build.MANUFACTURER
    or
    Build.BRAND
    
  • Hardware profile (TV or phone/mobile) by checking the boolean value of:

    BrightcoveMediaController.checkTvMode(Context context)

Limitations

It is important to note that while it is possible to set playback speed with the ExoPlayer and the Brightcove Native SDK for Android, it does not guarantee that setting a given playback speed will provide a seamless viewing and listening experience.

Testing has revealed that it is possible to set a playback speed that will be too high for older OS versions and lower-spec devices to handle without an unacceptable degradation in audio and video quality.

if you want to implement playback speed control, it is recommended that you consider the following:

  • What is the oldest Android OS level that my application is intended for?
    • Older OSes may not have the proper codec support for higher or lower playback speeds than normal
    • Older OSes may have older chip sets that can not process the video and audio data in a performant manner
  • Is my content DRM-protected?
    • Since testing has revealed some DRM-related issues with playback speed, we do not want to rule out the possibility of decryption issues when playing at higher or lower speeds than normal
  • Does my content use closed captions or multiple audio tracks?
    • Enabling or disabling captions, or switching between captions, especially on older devices or OS levels, is a circumstance to be considered.