MediaHelper.java


Overview

`MediaHelper.java` is a utility class designed to aid media playback components by providing standardized factory methods for constructing ExoPlayer data source factories. These factories are essential for fetching media streams efficiently and reliably over HTTP with integrated bandwidth metering and user-agent identification.

The class primarily focuses on:

While the file previously contained commented-out code related to managing a list of media models and concatenated media sources, its current active functionality is limited to these static factory methods.

This utility class simplifies and centralizes the creation of ExoPlayer-compatible data sources, ensuring consistent network behavior and facilitating adaptive streaming.


Classes and Methods

Class: MediaHelper

A utility class that provides static methods for creating ExoPlayer data source factories required for media playback.


Method: buildDataSourceFactory

public static @NonNull DataSource.Factory buildDataSourceFactory(
        @NonNull Context context,
        @Nullable DefaultBandwidthMeter bandwidthMeter)

Description

Creates a `DataSource.Factory` that combines local and HTTP data sources wrapped with bandwidth metering support. This factory is used by ExoPlayer to instantiate media sources capable of adaptive streaming.

Parameters

Returns

Usage Example

// Create a bandwidth meter instance (optional)
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build();

// Build the data source factory
DataSource.Factory dataSourceFactory = MediaHelper.buildDataSourceFactory(context, bandwidthMeter);

// Use the factory to create media sources for ExoPlayer

Method: buildHttpDataSourceFactory

public static @NonNull HttpDataSource.Factory buildHttpDataSourceFactory(
        @NonNull Context context,
        @NonNull DefaultBandwidthMeter bandwidthMeter)

Description

Constructs an HTTP data source factory embedding a user-agent string and bandwidth metering capabilities. This factory is responsible for creating HTTP connections used to fetch media data over the network.

Parameters

Returns

Important Notes

Usage Example

DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build();
HttpDataSource.Factory httpDataSourceFactory = MediaHelper.buildHttpDataSourceFactory(context, bandwidthMeter);

Implementation Details and Design Notes


Interaction with Other Components


Mermaid Class Diagram

classDiagram
    class MediaHelper {
        <<utility>>
        +buildDataSourceFactory(context: Context, bandwidthMeter: DefaultBandwidthMeter?): DataSource.Factory
        +buildHttpDataSourceFactory(context: Context, bandwidthMeter: DefaultBandwidthMeter): HttpDataSource.Factory
    }

Summary

`MediaHelper.java` serves as a centralized utility for building ExoPlayer-compatible data source factories with integrated bandwidth metering and standardized user agent strings. Its methods facilitate seamless media stream fetching over HTTP, enabling adaptive streaming and consistent network request behavior in the media playback pipeline. Although previously considered as a media model manager, its current role focuses solely on providing these foundational factories to support other media playback components within the system.


If you are extending or maintaining the media playback system, use `MediaHelper` to obtain properly configured data source factories rather than repeatedly instantiating them manually. Consider externalizing the user agent string as noted in the TODO comment for greater flexibility.