
This change: 1. Updates `DataSourceContractTest` to allow multiple "not found" resources, and to include additional info (e.g. headers) on them. 2. Updates the contract test to assert that `DataSource.getUri()` returns the expected (non-null) value for "not found" resources between the failed `open()` call and a subsequent `close()` call. The `DataSource` is 'open' at this point (since it needs to be 'closed' later), so `getUri()` must return non-null. * This change also fixes some implementations to comply with this contract. It also renames some imprecisely named `opened` booleans that **don't** track whether the `DataSource` is open or not. 3. Updates the contract test assertions to enforce that `DataSource.getResponseHeaders()` returns any headers associated with the 'not found' resource. 4. Configures `HttpDataSourceTestEnv` to provide both 404 and "server not found" resources, with the former having expected headers associated with it. PiperOrigin-RevId: 689316121 (cherry picked from commit 4a406be1bf8c787c4882815560a644a37792a2fb)
OkHttp DataSource module
This module provides an HttpDataSource implementation that uses Square's OkHttp.
OkHttp is a modern network stack that's widely used by many popular Android applications. It supports the HTTP and HTTP/2 protocols.
License note
Please note that whilst the code in this repository is licensed under Apache 2.0, using this extension requires depending on OkHttp, which is licensed separately.
Getting the module
The easiest way to get the module is to add it as a gradle dependency:
implementation 'androidx.media3:media3-datasource-okhttp:1.X.X'
where 1.X.X
is the version, which must match the version of the other media
modules being used.
Alternatively, you can clone this GitHub project and depend on the module locally. Instructions for doing this can be found in the top level README.
Using the module
Media components request data through DataSource
instances. These instances
are obtained from instances of DataSource.Factory
, which are instantiated and
injected from application code.
If your application only needs to play http(s) content, using the OkHttp
extension is as simple as updating any DataSource.Factory
instantiations in
your application code to use OkHttpDataSource.Factory
. If your application
also needs to play non-http(s) content such as local files, use:
new DefaultDataSourceFactory(
...
/* baseDataSourceFactory= */ new OkHttpDataSource.Factory(...));