Search code examples
kotlinsoundpool

Using SoundPool for games


class SoundPlayer(context: Context) {

// For sound FX
private val soundPool: SoundPool = SoundPool(10, // Here 
    AudioManager.STREAM_MUSIC,
    0)

companion object {
    var playerExplodeID = -1
    var invaderExplodeID = -1
    var shootID = -1
    var damageShelterID = -1
    var uhID = -1
    var ohID = -1
}

init {
    try {
        // Create objects of the 2 required classes
        val assetManager = context.assets
        var descriptor: AssetFileDescriptor


        // Load our fx in memory ready for use
        descriptor = assetManager.openFd("shoot.ogg")
        shootID = soundPool.load(descriptor, 0)

        descriptor = assetManager.openFd("invaderexplode.ogg")
        invaderExplodeID = soundPool.load(descriptor, 0)

        descriptor = assetManager.openFd("damageshelter.ogg")
        damageShelterID = soundPool.load(descriptor, 0)

        descriptor = assetManager.openFd("playerexplode.ogg")
        playerExplodeID = soundPool.load(descriptor, 0)

        descriptor = assetManager.openFd("damageshelter.ogg")
        damageShelterID = soundPool.load(descriptor, 0)

        descriptor = assetManager.openFd("uh.ogg")
        uhID = soundPool.load(descriptor, 0)

        descriptor = assetManager.openFd("oh.ogg")
        ohID = soundPool.load(descriptor, 0)


    } catch (e: IOException) {
        // Print an error message to the console
        Log.e("error", "failed to load sound files")
    }
}

fun playSound(id: Int){
    soundPool.play(id, 1f, 1f, 0, 0, 1f)
}
}

i have a problem with SoundPool cant use it is says constructor SoundPool is deprecated i'm kinda new so don't know how to fix this (watched many videos and searched everywhere but i cant fix it) so maybe someone can help me out tell me what to do


Solution

  • When something is deprecated, there always should be a hint what to use instead. So you need to use SoundPool.Builder to create new instance of an object. But there is one issue if you target API level that was release before SoundPool.Builder then you will get ClassNotFoundException. So general approach is to check the API level and do things in old way before API X(when new feature was introduced), and a new way after API X:

    @Suppress("DEPRECATION")
    fun buildSoundPool(maxStreams: Int):SoundPool =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val attrs = AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_GAME)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build()
            SoundPool.Builder()
                .setAudioAttributes(attrs)
                .setMaxStreams(maxStreams)
                .build()
        } else {
            SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0)
        }
    

    Then:

    private val soundPool: SoundPool = buildSoundPool(10)
    

    Also I do recommend to use my custom implementation of SoundPool because lots of platform dependent issues that was introduced in original SoundPool on different versions on Android.