0

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.

6
  • can you add the error logs as well as the soundEngine class code full Commented Dec 1, 2019 at 8:43
  • Umm, maybe I misunderstand what you're doing, but I think your method in SoundEngine is called someCode and you're trying to call it as load? Commented Dec 1, 2019 at 8:43
  • @zamb13, yes... is it possible? Commented Dec 1, 2019 at 8:45
  • in that case, it should give him compile-time error, not runtime I guess. @zsmb13 Commented Dec 1, 2019 at 8:45
  • "Unresolved reference" sounds like a compile time error in the IDE. @JoeShamuraq: if you want to use your method with the name load, you have to use that name at its declaration as well, instead of someCode. Commented Dec 1, 2019 at 8:48

1 Answer 1

2

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) }

    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.