# Loader

# 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)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader "View this page in Java")

<br />

```
@UnstableApi
class Loader : LoaderErrorThrower
```

<br />

*** ** * ** ***

Manages the background loading of[Loadable](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Loadable)s.

## Summary

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

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

|                                                                                                                                                       ### Public constructors                                                                                                                                                       |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Loader](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#Loader(androidx.media3.exoplayer.util.ReleasableExecutor))`(downloadExecutor: `[ReleasableExecutor](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/util/ReleasableExecutor)`!)` Constructs an instance. |
| [Loader](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#Loader(java.lang.String))`(threadNameSuffix: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!)` Constructs an instance.                                                                       |

|                                                                ### Public functions                                                                 |
|-----------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                       | [cancelLoading](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#cancelLoading())`()` Cancels the current load.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                       | [clearFatalError](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#clearFatalError())`()` Clears any stored fatal error.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `java-static `[Loader.LoadErrorAction](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction)`!` | [createRetryAction](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#createRetryAction(boolean,long))`(resetErrorCount: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`, retryDelayMillis: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Creates a[LoadErrorAction](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction)for retrying with the given parameters.                                                                                                                                                                                   |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                 | [hasFatalError](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#hasFatalError())`()` Whether the last call to[startLoading](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#startLoading(T,androidx.media3.exoplayer.upstream.Loader.Callback<T>,int))resulted in a fatal error.                                                                                                                                                                                                                                                                                                                                                       |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                 | [isLoading](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#isLoading())`()` Returns whether the loader is currently loading.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                       | [maybeThrowError](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#maybeThrowError())`()` Throws a fatal error, or a non-fatal error if loading is currently backed off and the current[Loadable](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Loadable)has incurred a number of errors greater than the[Loader](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader)s default minimum number of retries.                                                                                                                                                                                       |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                       | [maybeThrowError](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#maybeThrowError(int))`(minRetryCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Throws a fatal error, or a non-fatal error if loading is currently backed off and the current[Loadable](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Loadable)has incurred a number of errors greater than the specified minimum number of retries.                                                                                                                                                                                         |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                       | [release](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#release())`()` Releases the loader.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                       | [release](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#release(androidx.media3.exoplayer.upstream.Loader.ReleaseCallback))`(callback: `[Loader.ReleaseCallback](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.ReleaseCallback)`?)` Releases the loader.                                                                                                                                                                                                                                                                                                                                                                           |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                       | `<T : `[Loader.Loadable](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Loadable)`?> `[startLoading](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#startLoading(T,androidx.media3.exoplayer.upstream.Loader.Callback<T>,int))`(` ` loadable: T!,` ` callback: `[Loader.Callback](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Callback)`<T!>!,` ` defaultMinRetryCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) `)` Starts loading a[Loadable](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Loadable). |

## Constants

### DONT_RETRY

```
const val DONT_RETRY: Loader.LoadErrorAction!
```

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

### DONT_RETRY_FATAL

```
const val DONT_RETRY_FATAL: Loader.LoadErrorAction!
```

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

### RETRY

```
const val RETRY: Loader.LoadErrorAction!
```

Retries the load using the default delay.  

### RETRY_RESET_ERROR_COUNT

```
const val RETRY_RESET_ERROR_COUNT: Loader.LoadErrorAction!
```

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

## Public constructors

### Loader

```
Loader(downloadExecutor: ReleasableExecutor!)
```

Constructs an instance.  

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

### Loader

```
Loader(threadNameSuffix: String!)
```

Constructs an instance.  

|                                                Parameters                                                |
|----------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------|
| `threadNameSuffix: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | A name suffix for the loader's thread. This should be the name of the component using the loader. |

## Public functions

### cancelLoading

```
fun cancelLoading(): Unit
```

Cancels the current load.  

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

### clearFatalError

```
fun clearFatalError(): Unit
```

Clears any stored fatal error.  

### createRetryAction

```
java-static fun createRetryAction(resetErrorCount: Boolean, retryDelayMillis: Long): Loader.LoadErrorAction!
```

Creates a[LoadErrorAction](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.LoadErrorAction)for retrying with the given parameters.  

|                                               Parameters                                               |
|--------------------------------------------------------------------------------------------------------|---------------------------------------------------------|
| `resetErrorCount: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | Whether the previous error count should be set to zero. |
| `retryDelayMillis: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)      | The number of milliseconds to wait before retrying.     |

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

### hasFatalError

```
fun hasFatalError(): Boolean
```

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

### isLoading

```
fun isLoading(): Boolean
```

Returns whether the loader is currently loading.  

### maybeThrowError

```
fun maybeThrowError(): Unit
```

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

|                                         Throws                                          |
|-----------------------------------------------------------------------------------------|------------|
| [java.io.IOException](https://developer.android.com/reference/java/io/IOException.html) | The error. |

### maybeThrowError

```
fun maybeThrowError(minRetryCount: Int): Unit
```

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

|                                          Parameters                                          |
|----------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| `minRetryCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | A minimum retry count that must be exceeded for a non-fatal error to be thrown. Should be non-negative. |

|                                         Throws                                          |
|-----------------------------------------------------------------------------------------|------------|
| [java.io.IOException](https://developer.android.com/reference/java/io/IOException.html) | The error. |

### release

```
fun release(): Unit
```

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

### release

```
fun release(callback: Loader.ReleaseCallback?): Unit
```

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

|                                                                    Parameters                                                                     |
|---------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------|
| `callback: `[Loader.ReleaseCallback](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.ReleaseCallback)`?` | An optional callback to be called on the loading thread once the loader has been released. |

### startLoading

```
fun <T : Loader.Loadable?> startLoading(
    loadable: T!,
    callback: Loader.Callback<T!>!,
    defaultMinRetryCount: Int
): Long
```

Starts loading a[Loadable](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Loadable).

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

|                                                               Parameters                                                                |
|-----------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `<T : `[Loader.Loadable](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Loadable)`?>`         | The type of the loadable.                                                                                                                                                                                        |
| `loadable: T!`                                                                                                                          | The[Loadable](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Loadable)to load.                                                                                         |
| `callback: `[Loader.Callback](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader.Callback)`<T!>!` | A callback to be called when the load ends.                                                                                                                                                                      |
| `defaultMinRetryCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                     | The minimum number of times the load must be retried before[maybeThrowError](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/Loader#maybeThrowError())will propagate an error. |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | [elapsedRealtime](https://developer.android.com/reference/android/os/SystemClock.html#elapsedRealtime())when the load started. |

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