# OkHttpDataSource.Factory

Artifact: [androidx.media3:media3-datasource-okhttp](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/datasource/okhttp/OkHttpDataSource.java+class:androidx.media3.datasource.okhttp.OkHttpDataSource.Factory)

*** ** * ** ***


```
public final class OkHttpDataSource.Factory implements HttpDataSource.Factory
```

<br />

*** ** * ** ***

`https://developer.android.com/reference/androidx/media3/datasource/DataSource.Factory` for `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource` instances.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory#Factory(Call.Factory)(Factory callFactory)` Creates an instance. |

| ### Public methods |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory#createDataSource()()` Creates a `https://developer.android.com/reference/androidx/media3/datasource/DataSource` instance. |
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html @https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory#setCacheControl(CacheControl)(@https://developer.android.com/reference/androidx/annotation/Nullable CacheControl cacheControl)` Sets the CacheControl that will be used. |
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html @https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory#setContentTypePredicate(com.google.common.base.Predicate<java.lang.String>)( @https://developer.android.com/reference/androidx/annotation/Nullable https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Predicate.html<https://developer.android.com/reference/java/lang/String.html> contentTypePredicate )` Sets a content type `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Predicate.html`. |
| `final https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html @https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory#setDefaultRequestProperties(java.util.Map<java.lang.String,java.lang.String>)( https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html> defaultRequestProperties )` Sets the default request headers for `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource` instances created by the factory. |
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html @https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory#setTransferListener(androidx.media3.datasource.TransferListener)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/datasource/TransferListener transferListener)` Sets the `https://developer.android.com/reference/androidx/media3/datasource/TransferListener` that will be used. |
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory#setUserAgent(java.lang.String)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html userAgent)` Sets the user agent that will be used. |

### Inherited methods

From [androidx.media3.datasource.DataSource.Factory](https://developer.android.com/reference/androidx/media3/datasource/DataSource.Factory) `abstract https://developer.android.com/reference/androidx/media3/datasource/DataSource` `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi
https://developer.android.com/reference/androidx/media3/datasource/DataSource.Factory#createDataSource()()`

Creates a `https://developer.android.com/reference/androidx/media3/datasource/DataSource` instance.

## Public constructors

### Factory

```
public Factory(Factory callFactory)
```

Creates an instance.

| Parameters |
|---|---|
| `Factory callFactory` | A Call.Factory (typically an OkHttpClient) for use by the sources created by the factory. |

## Public methods

### createDataSource

```
@UnstableApi
public OkHttpDataSource createDataSource()
```

Creates a `https://developer.android.com/reference/androidx/media3/datasource/DataSource` instance.

### setCacheControl

```
@CanIgnoreReturnValue
@UnstableApi
public OkHttpDataSource.Factory setCacheControl(@Nullable CacheControl cacheControl)
```

Sets the CacheControl that will be used.

The default is `null`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable CacheControl cacheControl` | The cache control that will be used. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | This factory. |

### setContentTypePredicate

```
@CanIgnoreReturnValue
@UnstableApi
public OkHttpDataSource.Factory setContentTypePredicate(
    @Nullable Predicate<String> contentTypePredicate
)
```

Sets a content type `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Predicate.html`. If a content type is rejected by the predicate then a `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource.InvalidContentTypeException` is thrown from `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource#open(androidx.media3.datasource.DataSpec)`.

The default is `null`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Predicate.html<https://developer.android.com/reference/java/lang/String.html> contentTypePredicate` | The content type `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Predicate.html`, or `null` to clear a predicate that was previously set. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | This factory. |

### setDefaultRequestProperties

```
@CanIgnoreReturnValue
@UnstableApi
public final OkHttpDataSource.Factory setDefaultRequestProperties(
    Map<String, String> defaultRequestProperties
)
```

Sets the default request headers for `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource` instances created by the factory.

The new request properties will be used for future requests made by `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource` created by the factory, including instances that have already been created. Modifying the `defaultRequestProperties` map after a call to this method will have no effect, and so it's necessary to call this method again each time the request properties need to be updated.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html> defaultRequestProperties` | The default request properties. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | This factory. |

### setTransferListener

```
@CanIgnoreReturnValue
@UnstableApi
public OkHttpDataSource.Factory setTransferListener(@Nullable TransferListener transferListener)
```

Sets the `https://developer.android.com/reference/androidx/media3/datasource/TransferListener` that will be used.

The default is `null`.

See `https://developer.android.com/reference/androidx/media3/datasource/DataSource#addTransferListener(androidx.media3.datasource.TransferListener)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/datasource/TransferListener transferListener` | The listener that will be used. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | This factory. |

### setUserAgent

```
@CanIgnoreReturnValue
public OkHttpDataSource.Factory setUserAgent(@Nullable String userAgent)
```

Sets the user agent that will be used.

The default is `null`, which causes the default user agent of the underlying OkHttpClient to be used.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html userAgent` | The user agent that will be used, or `null` to use the default user agent of the underlying OkHttpClient. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/okhttp/OkHttpDataSource.Factory` | This factory. |