Monitor ExoPlayer | Mux

Monitor ExoPlayer

Connect Mux Data to ExoPlayer to collect playback performance metrics. Use this guide when your Android application plays video with ExoPlayer 2.x.

In this guide:

Features

The following data can be collected by the Mux Data SDK when you use the ExoPlayer SDK, as described below.

Supported Features:

Notes:

Request Latency is not available.

1 Add a dependency on the Mux Data SDK

Add Gradle dependency on the Mux ExoPlayer SDK

Add the Mux Maven repository to your Gradle file:

// Your repository configuration here

Next, add a dependency on the Mux Data ExoPlayer SDK. Supported versions of ExoPlayer are:

There is typically API compatibility within an ExoPlayer major-minor version, so you should be able to pair one of the versions listed above with any player sharing the same major-minor version (e.g., the ExoPlayer r2.12.1 version of the Mux ExoPlayer SDK works with ExoPlayer r2.12.0 and r2.12.2 equally well).

Add a dependency to your Gradle file using the Mux SDK version and an ExoPlayer version listed above in the following format:

implementation 'com.mux.stats:stats-sdk-exoplayer:VERSION'

Configure ProGuard/R8

If you're using ProGuard or R8, you'll need to add the following line to your app's proguard rules file (eg, proguard-rules.pro). This won't change anything about your app binary, it just suppresses a known warning

-keep class com.mux.** { *; }

Amazon ExoPlayer Port

In addition to the versions above, the Mux Data ExoPlayer SDK also supports Amazon's official ExoPlayer port for Amazon Devices. If you are monitoring ExoPlayer on an Amazon device, you can get that version with the following line:

// Amazon ExoPlayer port dependency

2 Initialize the monitor with your ExoPlayer instance

Get your ENV_KEY from the Mux environments dashboard.

Env Key is different than your API token

ENV_KEY is a client-side key used for Mux Data monitoring. These are not to be confused with API tokens which are created in the admin settings dashboard and meant to access the Mux API from a trusted server.

First, create the CustomerPlayerData and CustomerVideoData objects as appropriate for your current playback:

CustomerPlayerData playerData = new CustomerPlayerData();
CustomerVideoData videoData = new CustomerVideoData();

Next, create the MuxStatsExoPlayer object by passing your Context (typically your Activity), your ENV_KEY, the ExoPlayer instance, and the customer data object you just created:

MuxStatsExoPlayer muxStatsExoPlayer = new MuxStatsExoPlayer(context, ENV_KEY, exoPlayer, playerData, videoData);

If you haven't set your playerView already, do so now. We recommend this in order to determine a number of viewer context values as well as track the size of the video player:

muxStatsExoPlayer.setPlayerView(playerView);

Finally, when you are destroying the player, call the MuxStatsExoPlayer.release() function:

muxStatsExoPlayer.release();

After you've integrated, start playing a video in your player. A few minutes after you stop watching, you'll see the results in your Mux data dashboard. Login to the dashboard and find the environment that corresponds to your env_key and look for video views.

Note For ExoPlayer v2.15 and below

On older supported versions of ExoPlayer, Mux prefers that you pass an instance of SimpleExoPlayer specifically, instead of any ExoPlayer. In the latter case, however, some metrics and errors may not be available, such as upscaling metrics. Updating to ExoPlayer r2.16.0 or higher will remove this limitation.

3 Add Metadata

Options are provided to this SDK via the objects within the CustomerData object.

All metadata details are optional, however you'll be able to compare and see more interesting results as you include more details. This gives you more metrics and metadata about video streaming, and allows you to search and filter on important fields like the player version, CDN, and video title.

There is one caveat with ExoPlayer; ExoPlayer does not provide an API to retrieve the current source URL from the player. Due to this, CustomerVideoData has a method allowing you to set via CustomerVideoData.setVideoSourceUrl(String url). Setting this value will allow you to see the source URL as well as the dimension Source Hostname within the dashboard.

For more information, see the Metadata Guide.

4 Advanced

Changing the video

There are two cases where the underlying tracking of the video view need to be reset. First, when you load a new source URL into an existing player, and second when the program within a singular stream changes (such as a program within a live stream).

New source

When you change to a new video (in the same player) you need to update the information that Mux knows about the current video. Examples of when this is needed are:

This is done by calling muxStatsExoPlayer.videoChange(CustomerVideoData) which will remove all previous video data and reset all metrics for the video view.

New program (in single stream)

In some cases, you may have the program change within a stream, and you may want to track each program as a view on its own. An example of this is a live stream that streams multiple programs back to back, with no interruptions.

In this case, call muxStatsExoPlayer.programChange(CustomerVideoData).

Manually set when a video is being played full-screen

For most use cases, the SDK is capable of detecting whether or not a video is being played full-screen. For other uses cases, you may need to tell the SDK when the user switches to full-screen.

If you are using SimplePlayerView or a similar ExoPlayer UI component, you can set the full-screen flag from the OnFullScreenModeChangedListener:

muxStatsExoPlayer.setFullScreen(true);

Error tracking

By default, Mux's integration with ExoPlayer automatically tracks fatal errors as thrown by ExoPlayer. If a fatal error happens outside the context of ExoPlayer and you want to track it with Mux, you can call muxStatsExoPlayer.error like this:

muxStatsExoPlayer.error(MuxErrorException e);

Usage with Google Interactive Media Ads (IMA)

If you are using Google's IMA SDK to play back ads within your Android application, you can configure Mux to monitor the ad performance by passing your instance of AdsLoader to muxStatsExoPlayer.monitorImaAdsLoader(adsLoader).

ExoPlayer Compatibility

As of version 1.3.0 and later, the Mux SDK for ExoPlayer supports firing an event when the playback orientation changes. You can trigger this by calling muxStatsExoPlayer.orientationChange(MuxSDKViewOrientation orientation), passing either MuxSDKViewOrientation.LANDSCAPE or MuxSDKViewOrientation.PORTRAIT depending on the current orientation of the player.

Java Build Compatibility

Starting with version 2.6.0, the Mux SDK for ExoPlayer requires JDK 11 and version 7.0 or greater of the Android Gradle Plugin. This is only a requirement for build compatibility. The Mux SDK for ExoPlayer will remain bytecode-compatible with Java 1.8.

If you are updating from version 2.5.9 or lower, you may need to: