# Loader

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

*** ** * ** ***


```
@UnstableApi
public final class Loader implements LoaderErrorThrower
```

<br />

*** ** * ** ***

Manages the background loading of `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable`s.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Callback<T extends https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable>` A callback to be notified of `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader` events. |
| `public final class https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` Action that can be taken in response to `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Callback#onLoadError(T,long,long,java.io.IOException,int)`. |
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable` An object that can be loaded using a `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader`. |
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.ReleaseCallback` A callback to be notified when a `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader` has finished being released. |
| `public final class https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.UnexpectedLoaderException extends https://developer.android.com/reference/java/io/IOException.html` Thrown when an unexpected exception or error is encountered during loading. |

| ### Constants |
|---|---|
| `static final https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#DONT_RETRY()` Discards the failed `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable` and ignores any errors that have occurred. |
| `static final https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#DONT_RETRY_FATAL()` Discards the failed `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable`. |
| `static final https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#RETRY()` Retries the load using the default delay. |
| `static final https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#RETRY_RESET_ERROR_COUNT()` Retries the load using the default delay and resets the error count. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#Loader(androidx.media3.exoplayer.util.ReleasableExecutor)(https://developer.android.com/reference/androidx/media3/exoplayer/util/ReleasableExecutor downloadExecutor)` Constructs an instance. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#Loader(java.lang.String)(https://developer.android.com/reference/java/lang/String.html threadNameSuffix)` Constructs an instance. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#cancelLoading()()` Cancels the current load. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#clearFatalError()()` Clears any stored fatal error. |
| `static https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#createRetryAction(boolean,long)(boolean resetErrorCount, long retryDelayMillis)` Creates a `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` for retrying with the given parameters. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#hasFatalError()()` Whether the last call to `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#startLoading(T,androidx.media3.exoplayer.upstream.Loader.Callback<T>,int)` resulted in a fatal error. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#isLoading()()` Returns whether the loader is currently loading. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#maybeThrowError()()` Throws a fatal error, or a non-fatal error if loading is currently backed off and the current `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable` has incurred a number of errors greater than the `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader`s default minimum number of retries. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#maybeThrowError(int)(int minRetryCount)` Throws a fatal error, or a non-fatal error if loading is currently backed off and the current `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable` has incurred a number of errors greater than the specified minimum number of retries. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#release()()` Releases the loader. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#release(androidx.media3.exoplayer.upstream.Loader.ReleaseCallback)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.ReleaseCallback callback)` Releases the loader. |
| `long` | `<T extends https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable> https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#startLoading(T,androidx.media3.exoplayer.upstream.Loader.Callback<T>,int)( T loadable, https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Callback<T> callback, int defaultMinRetryCount )` Starts loading a `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable`. |

## Constants

### DONT_RETRY

```
public static final Loader.LoadErrorAction DONT_RETRY
```

Discards the failed `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable` and ignores any errors that have occurred.

### DONT_RETRY_FATAL

```
public static final Loader.LoadErrorAction DONT_RETRY_FATAL
```

Discards the failed `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable`. The next call to `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#maybeThrowError()` will throw the last load error.

### RETRY

```
public static final Loader.LoadErrorAction RETRY
```

Retries the load using the default delay.

### RETRY_RESET_ERROR_COUNT

```
public static final Loader.LoadErrorAction RETRY_RESET_ERROR_COUNT
```

Retries the load using the default delay and resets the error count.

## Public constructors

### Loader

```
public Loader(ReleasableExecutor downloadExecutor)
```

Constructs an instance.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/util/ReleasableExecutor downloadExecutor` | A `https://developer.android.com/reference/androidx/media3/exoplayer/util/ReleasableExecutor` to run the load task. The `https://developer.android.com/reference/androidx/media3/exoplayer/util/ReleasableExecutor` will be `https://developer.android.com/reference/androidx/media3/exoplayer/util/ReleasableExecutor#release()` once the loader no longer requires it for new load tasks. |

### Loader

```
public Loader(String threadNameSuffix)
```

Constructs an instance.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html threadNameSuffix` | A name suffix for the loader's thread. This should be the name of the component using the loader. |

## Public methods

### cancelLoading

```
public void cancelLoading()
```

Cancels the current load.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If the loader is not currently loading. |

### clearFatalError

```
public void clearFatalError()
```

Clears any stored fatal error.

### createRetryAction

```
public static Loader.LoadErrorAction createRetryAction(boolean resetErrorCount, long retryDelayMillis)
```

Creates a `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` for retrying with the given parameters.

| Parameters |
|---|---|
| `boolean resetErrorCount` | Whether the previous error count should be set to zero. |
| `long retryDelayMillis` | The number of milliseconds to wait before retrying. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` | A `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction` for retrying with the given parameters. |

### hasFatalError

```
public boolean hasFatalError()
```

Whether the last call to `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#startLoading(T,androidx.media3.exoplayer.upstream.Loader.Callback<T>,int)` resulted in a fatal error. Calling `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#maybeThrowError()` will throw the fatal error.

### isLoading

```
public boolean isLoading()
```

Returns whether the loader is currently loading.

### maybeThrowError

```
public void maybeThrowError()
```

Throws a fatal error, or a non-fatal error if loading is currently backed off and the current `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable` has incurred a number of errors greater than the `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader`s default minimum number of retries. Else does nothing.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | The error. |

### maybeThrowError

```
public void maybeThrowError(int minRetryCount)
```

Throws a fatal error, or a non-fatal error if loading is currently backed off and the current `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable` has incurred a number of errors greater than the specified minimum number of retries. Else does nothing.

| Parameters |
|---|---|
| `int minRetryCount` | A minimum retry count that must be exceeded for a non-fatal error to be thrown. Should be non-negative. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | The error. |

### release

```
public void release()
```

Releases the loader. This method should be called when the loader is no longer required.

### release

```
public void release(@Nullable Loader.ReleaseCallback callback)
```

Releases the loader. This method should be called when the loader is no longer required.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.ReleaseCallback callback` | An optional callback to be called on the loading thread once the loader has been released. |

### startLoading

```
public long <T extends Loader.Loadable> startLoading(
    T loadable,
    Loader.Callback<T> callback,
    int defaultMinRetryCount
)
```

Starts loading a `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable`.

The calling thread must be a `https://developer.android.com/reference/android/os/Looper.html` thread, which is the thread on which the `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Callback` will be called.

| Parameters |
|---|---|
| `<T extends https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable>` | The type of the loadable. |
| `T loadable` | The `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Loadable` to load. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader.Callback<T> callback` | A callback to be called when the load ends. |
| `int defaultMinRetryCount` | The minimum number of times the load must be retried before `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader#maybeThrowError()` will propagate an error. |

| Returns |
|---|---|
| `long` | `https://developer.android.com/reference/android/os/SystemClock.html#elapsedRealtime()` when the load started. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If the calling thread does not have an associated `https://developer.android.com/reference/android/os/Looper.html`. |