Joe Shamuraq
Joe Shamuraq

Reputation: 1385

Kotlin: Calling a function from another class

I'm trying to create a button that plays sound using SoundPool. The sound is created in another class and i'm trying to access it from my MainActivity as follow:

MainActivity:

class MainActivity : AppCompatActivity() {
    private var soundPool: SoundPool? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val sound1 = SoundEngine().load(this, R.raw.ahem_x, 1)//<-error pop up at load

        button1.setOnClickListener { soundPool!!.play(sound1, 1f, 1f, 1, 0, 1f) }

    }

}

Sound class:

class SoundEngine() {

    private var soundPool: SoundPool? = null

    fun someCode():Int{
        soundPool = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val audioAttributes = AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .setUsage(AudioAttributes.USAGE_GAME)
                .build()
            SoundPool.Builder()
                .setMaxStreams(1)
                .setAudioAttributes(audioAttributes)
                .build()
        } else {
            SoundPool(1, AudioManager.STREAM_MUSIC, 0)
        }
        val sound1 = soundPool!!.load(MainActivity(), R.raw.ahem_x, 1)
        return sound1

    }
}

I'm getting an error at val sound1 = SoundEngine().load(this, R.raw.ahem_x, 1) in MainActivity with pop up error Unresolved reference load. I googled about this and was overwhelmed by the information and got further confused. Please point me in the right direction. Thank in advance.

Upvotes: 0

Views: 6632

Answers (1)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15433

Try to change your implementation like below:

Sound class:

class SoundEngine {

    private var soundPool: SoundPool

    init {
        soundPool = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val audioAttributes = AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .setUsage(AudioAttributes.USAGE_GAME)
                .build()
            SoundPool.Builder()
                .setMaxStreams(1)
                .setAudioAttributes(audioAttributes)
                .build()
        } else {
            SoundPool(1, AudioManager.STREAM_MUSIC, 0)
        }
    }


    fun load(context: Context, rawId: Int, priority: Int):Int {
        return soundPool.load(context, rawId, priority)
    }

    fun play(soundID: Int, leftVolume: Float, rightVolume: Float, priority: Int, loop: Int, rate: Float) {
        soundPool.play(soundID, leftVolume, rightVolume, priority, loop, rate)
    }
}

MainActivity:

class MainActivity : AppCompatActivity() {
    private var soundPool: SoundPool? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val soundEngine = SoundEngine()
        val sound1 = soundEngine.load(this, R.raw.ahem_x, 1)

        button1.setOnClickListener { soundEngine.play(sound1, 1f, 1f, 1, 0, 1f) }

    }

}

Upvotes: 2

Related Questions