Skip to content

Instantly share code, notes, and snippets.

@GOVINDDIXIT
Last active May 18, 2023 04:04
Show Gist options
  • Save GOVINDDIXIT/f55b48e57ad878a2b1f5d45d28c716d2 to your computer and use it in GitHub Desktop.
Save GOVINDDIXIT/f55b48e57ad878a2b1f5d45d28c716d2 to your computer and use it in GitHub Desktop.
Sync implementation of CredSoundPool
open class SyncCredSoundPool constructor(
protected val context: Context,
audioAttributes: AudioAttributes?,
maxStreams: Int
) : CredSoundPool {
/**
* Soundpool instance created using SoundPool Builder
* In case the passed audioAttributes is null we will create default AudioAttributes
* with usage as AudioAttributes.USAGE_GAME and contentType as AudioAttributes.CONTENT_TYPE_SONIFICATION
*/
val soundPool: SoundPool = SoundPool.Builder()
.setAudioAttributes(
audioAttributes
?: AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
)
.setMaxStreams(maxStreams)
.build()
override val instance: SoundPool
get() = soundPool
/**
* Load the sound from the specified APK resource.
*
* Note that the extension is dropped. For example, if you want to load
* a sound from the raw resource file "chime.mp3", you would specify
* "R.raw.chime" as the resource ID.
*
* @param resId the resource ID
* @param priority the priority of the sound. Currently has no effect. Use
* a value of 1 for future compatibility.
* @param logError the error you want to log in case there is an IOException
* @return a sound ID. This value can be used to play or unload the sound.
*/
override fun load(resId: Int, priority: Int, logError: String): Int? {
return try {
soundPool.load(context, resId, priority)
} catch (e: IOException) {
logError(TAG, logError, e)
e.debugStackTrace()
null
}
}
/**
* Play a sound from a given sound ID.
*
* @param soundID a soundID returned by the load() function
* @return non-zero streamID if successful, zero if failed
*/
override fun play(soundId: Int, volume: Float) {
play(
soundId = soundId,
leftVolume = volume,
rightVolume = volume,
priority = 0,
loop = 0,
rate = 1f
)
}
/**
* Play a sound from a given sound ID.
*
* @param soundID a soundID returned by the load() function
* @param leftVolume left volume value (range = 0.0 to 1.0)
* @param rightVolume right volume value (range = 0.0 to 1.0)
* @param priority stream priority (0 = lowest priority)
* @param loop loop mode (0 = no loop, -1 = loop forever)
* @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
* @param streamIdCallback return non-zero streamID if successful, zero if failed
*/
override fun play(
soundId: Int,
leftVolume: Float,
rightVolume: Float,
priority: Int,
loop: Int,
rate: Float,
streamIdCallback: (Int) -> Unit
) {
val streamId = soundPool.play(
soundId, leftVolume, rightVolume, priority, loop, rate
)
streamIdCallback(streamId)
}
/**
* Stop a playback stream.
*
* Stop the stream specified by the streamID. This
* is the value returned by the play() function. If the stream
* is playing, it will be stopped. It also releases any native
* resources associated with this stream. If the stream is not
* playing, it will have no effect.
*
* @param streamID a streamID returned by the play() function
*/
override fun stop(streamId: Int) {
soundPool.stop(streamId)
}
/**
* Release the SoundPool resources.
*
* Release all memory and native resources used by the SoundPool
* object. The SoundPool can no longer be used and the reference
* should be set to null.
*/
override fun release() {
soundPool.release()
}
/**
* Play a sound from a given sound ID.
*
* @param streamID a streamID returned by the play() function
* @param leftVolume left volume value (range = 0.0 to 1.0)
* @param rightVolume right volume value (range = 0.0 to 1.0)
*/
override fun setVolume(streamId: Int, leftVolume: Float, rightVolume: Float) {
soundPool.setVolume(streamId, leftVolume, rightVolume)
}
companion object {
private const val TAG = "SoundPoolWrapper"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment