Skip to main content
Skip table of contents

DASH stream playback example code

To test this feature and view the example code, please see the (5.33.x) Android SDK 5 Example Code Quick Start guide.

CONNECT-protected DASH playback is implemented in the connect-dash example code as follows.

  1. In Android Studio, add the OpVault to the application build project as a project resource.
    The usual SDK initialisation precedes any other SDK access. In MainActivity:

    JAVA
    OTVSDK.load(this);
  2. An OTVConnectMediaDrmCallback must be instantiated to be passed to the player:

    JAVA
    // Adjust string below to match your licence server
    String CONNECT_LICENSE_SERVER = "https://licenseserverurl.com/TENANT_ID/prmls/contentlicenseservice/v1/licenses/";
     
    OTVConnectMediaDrmCallback connectDrmCallback = new OTVConnectMediaDrmCallback(CONNECT_LICENSE_SERVER);
  3. An instance of OTVConnectMediaDrmCallback needs to be created with the following configuration (which translate to HTTP request header properties:

    JAVA
    connectDrmCallback.setKeyRequestProperty("Accept", "application/json");
    connectDrmCallback.setKeyRequestProperty("Content-Type", "application/json");
    // Replace the value with the real data.
    connectDrmCallback.setKeyRequestProperty("nv-tenant-id", "--tenant_id--");
  4. The OpVault must be read from its file, so the following method is provided.

    JAVA
    private byte[] loadOpVault() {
      // Loading connect_opvault in src/main/res/raw
      try {
        byte[] buffer = new byte[1024 * 4];
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int bytesRead;
        InputStream opvaultStream = getResources().openRawResource(R.raw.connect_opvault);
        while ((bytesRead = opvaultStream.read(buffer)) != -1) {
          outputStream.write(buffer, 0, bytesRead);
        }
        return outputStream.toByteArray();
      } catch (IOException e) {
        OTVLog.e(TAG, e.getMessage());
        finish();
      }
     return new byte[0];
    }
  5. The OpVault data is read as a byte array and passed to the callback as a "nagraOpVault" DRM property:

    JAVA
    byte[] opVault = loadOpVault();
    connectDrmCallback.setDrmPropertyByteArray("nagraOpVault", opVault);
  6. An OTVVideoView object must be instantiated and attached to a UI frame.

    JAVA
    setContentView(R.layout.activity_main);
    FrameLayout frame = findViewById(R.id.frame);
    OTVVideoView otvVideoView = new OTVVideoView(this);
    frame.addView(otvVideoView);

    For improved functionality, you can add listeners here for player events coming from the OTVVideoView instance (see the example code).

  7. Before starting playback, the OTVConnectMediaDrmCallback instance must be configured with the stream's encryption properties; for example:

    JAVA
    // Replace the values with the real data.
    connectDrmCallback.setKeyRequestProperty("nv-authorizations", "--stream_token--");
    // Set application data if necessary
    connectDrmCallback.setKeyRequestOption("clientData", "thisIsClientData");
  8. Provide a method for starting playback on the UI thread. In this example, it will be called immediately.

    JAVA
    private static final String STREAM_URI = "https://cdnurl.com/mystream.m3u8";
     
    private void startPlayback() {
     runOnUiThread(() -> {
       otvVideoView.setVideoPath(STREAM_URI);
       otvVideoView.start();
     });
    }
  9. Provide the player with the connectDrmCallback  that has been created and configured, and playback can start.

    JAVA
    otvVVideoView.setMediaDrmCallback(connectDrmCallback);
    startPlayback();
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.