Skip to content

Instantly share code, notes, and snippets.

@GOVINDDIXIT
Last active May 18, 2023 04:57
Show Gist options
  • Save GOVINDDIXIT/cd65cef11f9bff9e4a298e7f96c1cdd3 to your computer and use it in GitHub Desktop.
Save GOVINDDIXIT/cd65cef11f9bff9e4a298e7f96c1cdd3 to your computer and use it in GitHub Desktop.
Async implementation of CredSoundPool
internal class AsyncCredSoundPool(
context: Context,
audioAttributes: AudioAttributes?,
maxStreams: Int
) : SyncCredSoundPool(context, audioAttributes, maxStreams) {
@PublishedApi
internal val bgThread = Handler(
HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND)
.apply { start() }
.looper
)
/**
* similar to SyncCredSoundPool's play method (see {[SyncCredSoundPool.play]}
* only difference is, it will run on background thread
*/
override fun play(
soundId: Int,
leftVolume: Float,
rightVolume: Float,
priority: Int,
loop: Int,
rate: Float,
streamIdCallback: (Int) -> Unit
) {
bgThread.post {
super.play(
soundId = soundId,
leftVolume = leftVolume,
rightVolume = rightVolume,
priority = priority,
loop = loop,
rate = rate,
streamIdCallback = streamIdCallback
)
}
}
/**
* similar to SyncCredSoundPool's stop method (see {[SyncCredSoundPool.stop]}
* only difference is, it will run on background thread
*/
override fun stop(streamId: Int) {
bgThread.post {
super.stop(streamId = streamId)
}
}
/**
* similar to SyncCredSoundPool's release method (see {[SyncCredSoundPool.release]}
* only difference is, it will run on background thread
*/
override fun release() {
bgThread.post {
super.release()
bgThread.looper.quitSafely()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment