Stay organized with collections
Save and categorize content based on your preferences.
AsyncPlayer
open class AsyncPlayer
Plays a series of audio URIs, but does all the hard work on another thread so that any slowness with preparing or loading doesn't block the calling thread.
Summary
Public constructors |
Construct an AsyncPlayer object.
|
Public methods |
open Unit |
Start playing the sound.
|
open Unit |
Start playing the sound.
|
open Unit |
Stop a previously played sound.
|
Public constructors
AsyncPlayer
AsyncPlayer(tag: String!)
Construct an AsyncPlayer object.
Parameters |
tag |
String!: a string to use for debugging |
Public methods
play
open fun play(
context: Context,
uri: Uri,
looping: Boolean,
attributes: AudioAttributes
): Unit
Start playing the sound. It will actually start playing at some point in the future. There are no guarantees about latency here. Calling this before another audio file is done playing will stop that one and start the new one.
Exceptions |
java.lang.IllegalArgumentException |
|
stop
open fun stop(): Unit
Stop a previously played sound. It can't be played again or unpaused at this point. Calling this multiple times has no ill effects.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# AsyncPlayer\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nAsyncPlayer\n===========\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/media/AsyncPlayer \"View this page in Java\") \n\n```\nopen class AsyncPlayer\n```\n\n|---|--------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.media.AsyncPlayer](#) |\n\nPlays a series of audio URIs, but does all the hard work on another thread so that any slowness with preparing or loading doesn't block the calling thread.\n\nSummary\n-------\n\n| Public constructors ||\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|\n| [AsyncPlayer](#AsyncPlayer(kotlin.String))`(`tag:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!`)` Construct an AsyncPlayer object. |\n\n| Public methods ||\n|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [play](#play(android.content.Context,%20android.net.Uri,%20kotlin.Boolean,%20android.media.AudioAttributes))`(`context:` `[Context](../content/Context.html#)`, `uri:` `[Uri](../net/Uri.html#)`, `looping:` `[Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`, `attributes:` `[AudioAttributes](/reference/kotlin/android/media/AudioAttributes)`)` Start playing the sound. |\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [play](#play(android.content.Context,%20android.net.Uri,%20kotlin.Boolean,%20kotlin.Int))`(`context:` `[Context](../content/Context.html#)!`, `uri:` `[Uri](../net/Uri.html#)!`, `looping:` `[Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`, `stream:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Start playing the sound. |\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [stop](#stop())`()` Stop a previously played sound. |\n\nPublic constructors\n-------------------\n\n### AsyncPlayer\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nAsyncPlayer(tag: String!)\n```\n\nConstruct an AsyncPlayer object.\n\n| Parameters ||\n|-------|------------------------------------------------------------------------------------------------------------------|\n| `tag` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!: a string to use for debugging |\n\nPublic methods\n--------------\n\n### play\n\nAdded in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun play(\n context: Context, \n uri: Uri, \n looping: Boolean, \n attributes: AudioAttributes\n): Unit\n```\n\nStart playing the sound. It will actually start playing at some point in the future. There are no guarantees about latency here. Calling this before another audio file is done playing will stop that one and start the new one.\n\n| Parameters ||\n|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `context` | [Context](../content/Context.html#): the non-null application's context. |\n| `uri` | [Uri](../net/Uri.html#): the non-null URI to play. (see [MediaPlayer.setDataSource(Context, Uri)](/reference/kotlin/android/media/MediaPlayer#setDataSource(android.content.Context,%20android.net.Uri))) |\n| `looping` | [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html): whether the audio should loop forever. (see [MediaPlayer.setLooping(boolean)](/reference/kotlin/android/media/MediaPlayer#setLooping(kotlin.Boolean))) |\n| `attributes` | [AudioAttributes](/reference/kotlin/android/media/AudioAttributes): the non-null [AudioAttributes](/reference/kotlin/android/media/AudioAttributes) to use. (see [MediaPlayer.setAudioAttributes(AudioAttributes)](/reference/kotlin/android/media/MediaPlayer#setAudioAttributes(android.media.AudioAttributes))) |\n\n| Exceptions ||\n|--------------------------------------|---|\n| `java.lang.IllegalArgumentException` | |\n\n### play\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \nDeprecated in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun play(\n context: Context!, \n uri: Uri!, \n looping: Boolean, \n stream: Int\n): Unit\n```\n\n**Deprecated:** *use [play(android.content.Context,android.net.Uri,boolean,android.media.AudioAttributes)](#play(android.content.Context,%20android.net.Uri,%20kotlin.Boolean,%20android.media.AudioAttributes)) instead*\n\nStart playing the sound. It will actually start playing at some point in the future. There are no guarantees about latency here. Calling this before another audio file is done playing will stop that one and start the new one.\n\n| Parameters ||\n|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `context` | [Context](../content/Context.html#)!: Your application's context. |\n| `uri` | [Uri](../net/Uri.html#)!: The URI to play. (see [MediaPlayer.setDataSource(Context, Uri)](/reference/kotlin/android/media/MediaPlayer#setDataSource(android.content.Context,%20android.net.Uri))) |\n| `looping` | [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html): Whether the audio should loop forever. (see [MediaPlayer.setLooping(boolean)](/reference/kotlin/android/media/MediaPlayer#setLooping(kotlin.Boolean))) |\n| `stream` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): the AudioStream to use. (see [MediaPlayer.setAudioStreamType(int)](/reference/kotlin/android/media/MediaPlayer#setAudioStreamType(kotlin.Int))) |\n\n### stop\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun stop(): Unit\n```\n\nStop a previously played sound. It can't be played again or unpaused at this point. Calling this multiple times has no ill effects."]]