Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

android - Kotlin:从另一个类调用函数(Kotlin: Calling a function from another class)

I'm trying to create a button that plays sound using SoundPool.

(我正在尝试创建一个使用SoundPool播放声音的按钮。)

The sound is created in another class and i'm trying to access it from my MainActivity as follow:

(声音是在另一个类中创建的,我正尝试通过MainActivity访问它,如下所示:)

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 .

(我在MainActivity中的val sound1 = SoundEngine().load(this, R.raw.ahem_x, 1)出现错误,弹出错误Unresolved reference load 。)

I googled about this and was overwhelmed by the information and got further confused.

(我在Google上搜索了一下,但对这些信息不知所措,进一步感到困惑。)

Please point me in the right direction.

(请指出正确的方向。)

Thank in advance.

(预先感谢。)

  ask by Joe Shamuraq translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...