What most people know about ExoPlayer is that ExoPlayer is a highly efficient player. Currently, among all players, ExoPlayer is also unique. ExoPlayer is very popular on Github and is used by many people. You can see that the features and performance of ExoPlayer are pretty good.

If we use an open source library, it is good that we can use it. If we can use it in the first step, we can also deeply analyze its principles and methods, digest the open source things, expand the original functions on the original basis, and fix the problems of the original project. Ultimately, you can optimize open source projects based on open source projects. Get the functionality that fits our business scenario.

1. The analysis of the DataSource

DataSource = ‘DataSource’; DataSource = ‘DataSource’; DataSource = ‘DataSource’; You type in a URL, what schema the URL is, and then use the subdatasource of that schema to request it. In DefaultDataSource. In Java:

public long open(DataSpec dataSpec) throws IOException { Assertions.checkState(dataSource == null); // Choose the correct source for the scheme. String scheme = dataSpec.uri.getScheme(); if (Util.isLocalFileUri(dataSpec.uri)) { String uriPath = dataSpec.uri.getPath(); if (uriPath ! = null && uriPath.startsWith("/android_asset/")) { dataSource = getAssetDataSource(); } else { dataSource = getFileDataSource(); } } else if (SCHEME_ASSET.equals(scheme)) { dataSource = getAssetDataSource(); } else if (SCHEME_CONTENT.equals(scheme)) { dataSource = getContentDataSource(); } else if (SCHEME_RTMP.equals(scheme)) { dataSource = getRtmpDataSource(); } else if (SCHEME_UDP.equals(scheme)) { dataSource = getUdpDataSource(); } else if (DataSchemeDataSource.SCHEME_DATA.equals(scheme)) { dataSource = getDataSchemeDataSource(); } else if (SCHEME_RAW.equals(scheme)) { dataSource = getRawResourceDataSource(); } else { dataSource = baseDataSource; } // Open the source and return. return dataSource.open(dataSpec); }Copy the code

The DataSource interface uses the following methods:

/**
 * A component from which streams of data can be read.
 */
public interface DataSource {

  void addTransferListener(TransferListener transferListener);

  long open(DataSpec dataSpec) throws IOException;

  int read(byte[] buffer, int offset, int readLength) throws IOException;

  @Nullable Uri getUri();

  default Map<String, List<String>> getResponseHeaders() {
    return Collections.emptyMap();
  }
  void close() throws IOException;
}
Copy the code
  • AddTransferListener: Adds a listener for data transfer
  • Open: a place to initiate a request, either a network request or a local file request
  • Read: Read stream data
  • GetUri: Gets the uri of the request
  • GetResponseHeaders: Obtains the response header of the request
  • Close: closes the stream

These operations are required, but you can add additional interfaces to suit your business needs.

Look primarily at the incoming listener interface in TransferListener

public interface TransferListener {

  void onTransferInitializing(DataSource source, DataSpec dataSpec, boolean isNetwork);

  void onTransferStart(DataSource source, DataSpec dataSpec, boolean isNetwork);

  void onBytesTransferred(
      DataSource source, DataSpec dataSpec, boolean isNetwork, int bytesTransferred);

  void onTransferEnd(DataSource source, DataSpec dataSpec, boolean isNetwork);
}
Copy the code

OnBytesTransferred (DataSource source, DataSpec DataSpec, Boolean isNetwork, The int bytesTransferred () callback tells the developer how much stream data has been loaded for flow control. ExoPlayer is really nice about that.

The DataSource class relationships are as follows:

2. Okhttp replaces the originating network request

Native core network request processing in DefaultHttpDataSource. In Java, okhttp in controlling link multiplexing, multithreading and the custom to do better, we also want to access in ExoPlayer okhttp library. How to use it?

  • As above, need to introduce two classes, OkHttpDataSourceFactory. Java and OkHttpDataSource. Java
  • At the time of buildDataSource replace with OkHttpDataSourceFactory DefaultHtttpDataSourceFactory is ok.

For details, see github.com/JeffMony/Pl…