public class

TestExoPlayerBuilder

extends java.lang.Object

 java.lang.Object

↳androidx.media3.test.utils.TestExoPlayerBuilder

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-test-utils', version: '1.0.0-alpha03'

  • groupId: androidx.media3
  • artifactId: media3-test-utils
  • version: 1.0.0-alpha03

Artifact androidx.media3:media3-test-utils:1.0.0-alpha03 it located at Google repository (https://maven.google.com/)

Overview

A builder of ExoPlayer instances for testing.

Summary

Constructors
publicTestExoPlayerBuilder(Context context)

Methods
public ExoPlayerbuild()

Builds an ExoPlayer using the provided values or their defaults.

public BandwidthMetergetBandwidthMeter()

Returns the bandwidth meter used by the player.

public ClockgetClock()

Returns the clock used by the player.

public LoadControlgetLoadControl()

Returns the LoadControl that will be used by the player.

public LoopergetLooper()

Returns the that will be used by the player, or null if no has been set yet and no default is available.

public MediaSource.FactorygetMediaSourceFactory()

Returns the that will be used by the player, or null if no has been set yet and no default is available.

public RenderergetRenderers()

Returns the Renderers that have been set with TestExoPlayerBuilder.setRenderers(Renderer...) or null if no Renderers have been explicitly set.

public RenderersFactorygetRenderersFactory()

Returns the RenderersFactory that has been set with TestExoPlayerBuilder.setRenderersFactory(RenderersFactory) or null if no factory has been explicitly set.

public longgetSeekBackIncrementMs()

Returns the seek back increment used by the player.

public longgetSeekForwardIncrementMs()

Returns the seek forward increment used by the player.

public DefaultTrackSelectorgetTrackSelector()

Returns the track selector used by the player.

public booleangetUseLazyPreparation()

Returns whether the player will use lazy preparation.

public TestExoPlayerBuildersetBandwidthMeter(BandwidthMeter bandwidthMeter)

Sets the BandwidthMeter.

public TestExoPlayerBuildersetClock(Clock clock)

Sets the Clock to be used by the player.

public TestExoPlayerBuildersetLoadControl(LoadControl loadControl)

Sets a LoadControl to be used by the player.

public TestExoPlayerBuildersetLooper(Looper looper)

Sets the to be used by the player.

public TestExoPlayerBuildersetMediaSourceFactory(MediaSource.Factory mediaSourceFactory)

Sets the to be used by the player.

public TestExoPlayerBuildersetRenderers(Renderer renderers[])

Sets the Renderers.

public TestExoPlayerBuildersetRenderersFactory(RenderersFactory renderersFactory)

Sets the RenderersFactory.

public TestExoPlayerBuildersetSeekBackIncrementMs(long seekBackIncrementMs)

Sets the seek back increment to be used by the player.

public TestExoPlayerBuildersetSeekForwardIncrementMs(long seekForwardIncrementMs)

Sets the seek forward increment to be used by the player.

public TestExoPlayerBuildersetTrackSelector(DefaultTrackSelector trackSelector)

Sets a DefaultTrackSelector.

public TestExoPlayerBuildersetUseLazyPreparation(boolean useLazyPreparation)

Sets whether to use lazy preparation.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public TestExoPlayerBuilder(Context context)

Methods

public TestExoPlayerBuilder setUseLazyPreparation(boolean useLazyPreparation)

Sets whether to use lazy preparation.

Parameters:

useLazyPreparation: Whether to use lazy preparation.

Returns:

This builder.

public boolean getUseLazyPreparation()

Returns whether the player will use lazy preparation.

public TestExoPlayerBuilder setTrackSelector(DefaultTrackSelector trackSelector)

Sets a DefaultTrackSelector. The default value is a DefaultTrackSelector in its initial configuration.

Parameters:

trackSelector: The DefaultTrackSelector to be used by the player.

Returns:

This builder.

public DefaultTrackSelector getTrackSelector()

Returns the track selector used by the player.

public TestExoPlayerBuilder setLoadControl(LoadControl loadControl)

Sets a LoadControl to be used by the player. The default value is a DefaultLoadControl.

Parameters:

loadControl: The LoadControl to be used by the player.

Returns:

This builder.

public LoadControl getLoadControl()

Returns the LoadControl that will be used by the player.

public TestExoPlayerBuilder setBandwidthMeter(BandwidthMeter bandwidthMeter)

Sets the BandwidthMeter. The default value is a DefaultBandwidthMeter in its default configuration.

Parameters:

bandwidthMeter: The BandwidthMeter to be used by the player.

Returns:

This builder.

public BandwidthMeter getBandwidthMeter()

Returns the bandwidth meter used by the player.

public TestExoPlayerBuilder setRenderers(Renderer renderers[])

Sets the Renderers. If not set, the player will use a FakeVideoRenderer and a FakeAudioRenderer. Setting the renderers is not allowed after a call to TestExoPlayerBuilder.setRenderersFactory(RenderersFactory).

Parameters:

renderers: A list of Renderers to be used by the player.

Returns:

This builder.

public Renderer getRenderers()

Returns the Renderers that have been set with TestExoPlayerBuilder.setRenderers(Renderer...) or null if no Renderers have been explicitly set. Note that these renderers may not be the ones used by the built player, for example if a Renderer factory has been set.

public TestExoPlayerBuilder setRenderersFactory(RenderersFactory renderersFactory)

Sets the RenderersFactory. The default factory creates all renderers set by TestExoPlayerBuilder.setRenderers(Renderer...). Setting the renderer factory is not allowed after a call to TestExoPlayerBuilder.setRenderers(Renderer...).

Parameters:

renderersFactory: A RenderersFactory to be used by the player.

Returns:

This builder.

public RenderersFactory getRenderersFactory()

Returns the RenderersFactory that has been set with TestExoPlayerBuilder.setRenderersFactory(RenderersFactory) or null if no factory has been explicitly set.

public TestExoPlayerBuilder setClock(Clock clock)

Sets the Clock to be used by the player. The default value is an auto-advancing FakeClock.

Parameters:

clock: A Clock to be used by the player.

Returns:

This builder.

public Clock getClock()

Returns the clock used by the player.

public TestExoPlayerBuilder setLooper(Looper looper)

Sets the to be used by the player.

Parameters:

looper: The to be used by the player.

Returns:

This builder.

public Looper getLooper()

Returns the that will be used by the player, or null if no has been set yet and no default is available.

public MediaSource.Factory getMediaSourceFactory()

Returns the that will be used by the player, or null if no has been set yet and no default is available.

public TestExoPlayerBuilder setMediaSourceFactory(MediaSource.Factory mediaSourceFactory)

Sets the to be used by the player.

Parameters:

mediaSourceFactory: The to be used by the player.

Returns:

This builder.

public TestExoPlayerBuilder setSeekBackIncrementMs(long seekBackIncrementMs)

Sets the seek back increment to be used by the player.

Parameters:

seekBackIncrementMs: The seek back increment to be used by the player.

Returns:

This builder.

public long getSeekBackIncrementMs()

Returns the seek back increment used by the player.

public TestExoPlayerBuilder setSeekForwardIncrementMs(long seekForwardIncrementMs)

Sets the seek forward increment to be used by the player.

Parameters:

seekForwardIncrementMs: The seek forward increment to be used by the player.

Returns:

This builder.

public long getSeekForwardIncrementMs()

Returns the seek forward increment used by the player.

public ExoPlayer build()

Builds an ExoPlayer using the provided values or their defaults.

Source

/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.test.utils;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.os.Looper;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Clock;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.DefaultLoadControl;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.LoadControl;
import androidx.media3.exoplayer.Renderer;
import androidx.media3.exoplayer.RenderersFactory;
import androidx.media3.exoplayer.analytics.DefaultAnalyticsCollector;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector;
import androidx.media3.exoplayer.upstream.BandwidthMeter;
import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

/** A builder of {@link ExoPlayer} instances for testing. */
@UnstableApi
public class TestExoPlayerBuilder {

  private final Context context;
  private Clock clock;
  private DefaultTrackSelector trackSelector;
  private LoadControl loadControl;
  private BandwidthMeter bandwidthMeter;
  @Nullable private Renderer[] renderers;
  @Nullable private RenderersFactory renderersFactory;
  @Nullable private MediaSource.Factory mediaSourceFactory;
  private boolean useLazyPreparation;
  private @MonotonicNonNull Looper looper;
  private long seekBackIncrementMs;
  private long seekForwardIncrementMs;

  public TestExoPlayerBuilder(Context context) {
    this.context = context;
    clock = new FakeClock(/* isAutoAdvancing= */ true);
    trackSelector = new DefaultTrackSelector(context);
    loadControl = new DefaultLoadControl();
    bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build();
    @Nullable Looper myLooper = Looper.myLooper();
    if (myLooper != null) {
      looper = myLooper;
    }
    seekBackIncrementMs = C.DEFAULT_SEEK_BACK_INCREMENT_MS;
    seekForwardIncrementMs = C.DEFAULT_SEEK_FORWARD_INCREMENT_MS;
  }

  /**
   * Sets whether to use lazy preparation.
   *
   * @param useLazyPreparation Whether to use lazy preparation.
   * @return This builder.
   */
  public TestExoPlayerBuilder setUseLazyPreparation(boolean useLazyPreparation) {
    this.useLazyPreparation = useLazyPreparation;
    return this;
  }

  /** Returns whether the player will use lazy preparation. */
  public boolean getUseLazyPreparation() {
    return useLazyPreparation;
  }

  /**
   * Sets a {@link DefaultTrackSelector}. The default value is a {@link DefaultTrackSelector} in its
   * initial configuration.
   *
   * @param trackSelector The {@link DefaultTrackSelector} to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setTrackSelector(DefaultTrackSelector trackSelector) {
    Assertions.checkNotNull(trackSelector);
    this.trackSelector = trackSelector;
    return this;
  }

  /** Returns the track selector used by the player. */
  public DefaultTrackSelector getTrackSelector() {
    return trackSelector;
  }

  /**
   * Sets a {@link LoadControl} to be used by the player. The default value is a {@link
   * DefaultLoadControl}.
   *
   * @param loadControl The {@link LoadControl} to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setLoadControl(LoadControl loadControl) {
    this.loadControl = loadControl;
    return this;
  }

  /** Returns the {@link LoadControl} that will be used by the player. */
  public LoadControl getLoadControl() {
    return loadControl;
  }

  /**
   * Sets the {@link BandwidthMeter}. The default value is a {@link DefaultBandwidthMeter} in its
   * default configuration.
   *
   * @param bandwidthMeter The {@link BandwidthMeter} to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setBandwidthMeter(BandwidthMeter bandwidthMeter) {
    Assertions.checkNotNull(bandwidthMeter);
    this.bandwidthMeter = bandwidthMeter;
    return this;
  }

  /** Returns the bandwidth meter used by the player. */
  public BandwidthMeter getBandwidthMeter() {
    return bandwidthMeter;
  }

  /**
   * Sets the {@link Renderer}s. If not set, the player will use a {@link FakeVideoRenderer} and a
   * {@link FakeAudioRenderer}. Setting the renderers is not allowed after a call to {@link
   * #setRenderersFactory(RenderersFactory)}.
   *
   * @param renderers A list of {@link Renderer}s to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setRenderers(Renderer... renderers) {
    assertThat(renderersFactory).isNull();
    this.renderers = renderers;
    return this;
  }

  /**
   * Returns the {@link Renderer Renderers} that have been set with {@link #setRenderers} or null if
   * no {@link Renderer Renderers} have been explicitly set. Note that these renderers may not be
   * the ones used by the built player, for example if a {@link #setRenderersFactory Renderer
   * factory} has been set.
   */
  @Nullable
  public Renderer[] getRenderers() {
    return renderers;
  }

  /**
   * Sets the {@link RenderersFactory}. The default factory creates all renderers set by {@link
   * #setRenderers(Renderer...)}. Setting the renderer factory is not allowed after a call to {@link
   * #setRenderers(Renderer...)}.
   *
   * @param renderersFactory A {@link RenderersFactory} to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setRenderersFactory(RenderersFactory renderersFactory) {
    assertThat(renderers).isNull();
    this.renderersFactory = renderersFactory;
    return this;
  }

  /**
   * Returns the {@link RenderersFactory} that has been set with {@link #setRenderersFactory} or
   * null if no factory has been explicitly set.
   */
  @Nullable
  public RenderersFactory getRenderersFactory() {
    return renderersFactory;
  }

  /**
   * Sets the {@link Clock} to be used by the player. The default value is an auto-advancing {@link
   * FakeClock}.
   *
   * @param clock A {@link Clock} to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setClock(Clock clock) {
    assertThat(clock).isNotNull();
    this.clock = clock;
    return this;
  }

  /** Returns the clock used by the player. */
  public Clock getClock() {
    return clock;
  }

  /**
   * Sets the {@link Looper} to be used by the player.
   *
   * @param looper The {@link Looper} to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setLooper(Looper looper) {
    this.looper = looper;
    return this;
  }

  /**
   * Returns the {@link Looper} that will be used by the player, or null if no {@link Looper} has
   * been set yet and no default is available.
   */
  @Nullable
  public Looper getLooper() {
    return looper;
  }

  /**
   * Returns the {@link MediaSource.Factory} that will be used by the player, or null if no {@link
   * MediaSource.Factory} has been set yet and no default is available.
   */
  @Nullable
  public MediaSource.Factory getMediaSourceFactory() {
    return mediaSourceFactory;
  }

  /**
   * Sets the {@link MediaSource.Factory} to be used by the player.
   *
   * @param mediaSourceFactory The {@link MediaSource.Factory} to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setMediaSourceFactory(MediaSource.Factory mediaSourceFactory) {
    this.mediaSourceFactory = mediaSourceFactory;
    return this;
  }

  /**
   * Sets the seek back increment to be used by the player.
   *
   * @param seekBackIncrementMs The seek back increment to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setSeekBackIncrementMs(long seekBackIncrementMs) {
    this.seekBackIncrementMs = seekBackIncrementMs;
    return this;
  }

  /** Returns the seek back increment used by the player. */
  public long getSeekBackIncrementMs() {
    return seekBackIncrementMs;
  }

  /**
   * Sets the seek forward increment to be used by the player.
   *
   * @param seekForwardIncrementMs The seek forward increment to be used by the player.
   * @return This builder.
   */
  public TestExoPlayerBuilder setSeekForwardIncrementMs(long seekForwardIncrementMs) {
    this.seekForwardIncrementMs = seekForwardIncrementMs;
    return this;
  }

  /** Returns the seek forward increment used by the player. */
  public long getSeekForwardIncrementMs() {
    return seekForwardIncrementMs;
  }

  /** Builds an {@link ExoPlayer} using the provided values or their defaults. */
  public ExoPlayer build() {
    Assertions.checkNotNull(
        looper, "TestExoPlayer builder run on a thread without Looper and no Looper specified.");
    // Do not update renderersFactory and renderers here, otherwise their getters may
    // return different values before and after build() is called, making them confusing.
    RenderersFactory playerRenderersFactory = renderersFactory;
    if (playerRenderersFactory == null) {
      playerRenderersFactory =
          (eventHandler,
              videoRendererEventListener,
              audioRendererEventListener,
              textRendererOutput,
              metadataRendererOutput) ->
              renderers != null
                  ? renderers
                  : new Renderer[] {
                    new FakeVideoRenderer(eventHandler, videoRendererEventListener),
                    new FakeAudioRenderer(eventHandler, audioRendererEventListener)
                  };
    }

    ExoPlayer.Builder builder =
        new ExoPlayer.Builder(context, playerRenderersFactory)
            .setTrackSelector(trackSelector)
            .setLoadControl(loadControl)
            .setBandwidthMeter(bandwidthMeter)
            .setAnalyticsCollector(new DefaultAnalyticsCollector(clock))
            .setClock(clock)
            .setUseLazyPreparation(useLazyPreparation)
            .setLooper(looper)
            .setSeekBackIncrementMs(seekBackIncrementMs)
            .setSeekForwardIncrementMs(seekForwardIncrementMs);
    if (mediaSourceFactory != null) {
      builder.setMediaSourceFactory(mediaSourceFactory);
    }
    return builder.build();
  }
}