您能帮助我理解为什么我无法在Application的onCreate中运行 SoundManager.initialize(this)行吗?
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
SoundManager.initialize(this)
}
}
来自
class SoundManager {
var soundPool: SoundPool
var c3 = 0
init {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
soundPool = SoundPool.Builder().setMaxStreams(13).build()
} else {
soundPool = SoundPool(13, AudioManager.STREAM_MUSIC, 5)
}
}
fun loadSound(context: Context) {
c3 = soundPool.load(context, R.raw.c3, 1)
}
fun playSound() {
soundPool.play(c3, 1.0f, 1.0f, 0, 0, 1.0f)
}
companion object {
private var singleton: SoundManager? = null
fun initialize(context: Context) {
val soundManager = instance
soundManager.loadSound(context)
}
val instance: SoundManager
@Synchronized get() {
if (singleton == null) {
singleton = SoundManager()
}
return singleton as SoundManager
}
}
}
我通过在我的第一个Activity的onCreate函数中调用SoundManager.initialize(this)找到了一种解决方法,但是我很好奇为什么我不能使其与Application的onCreate一起运行。
谢谢!
答案 0 :(得分:2)
<application
-------
android:name=".MyApplication"
------
>
将此添加到清单文件中。