I have a SoundPool that I want to stay loaded in different fragments. So I load it with an intent for a service in my starting activities onCreate, and then try to play it with other intents in onClickListeners (and other methods) in the other fragments. Even though it prints the lines to confirm the sounds being loaded, afterwards it does not play them, saying "sample X not READY". What am I doing wrong?
Here´s the Main Activities onCreate:
override fun onCreate(savedInstanceState: Bundle?) {
var pIntent = Intent(this, SoulSoundService::class.java)
pIntent.putExtra("Load", "Ein")
startService(pIntent)
super.onCreate(savedInstanceState)
setContentView(com.example.soulfetchnew.R.layout.activity_fullscreen)
mVisible = false
}
and the onclicklistener (in a fragment)
override fun onResume() {
super.onResume()
majpentlevelbutton.setOnClickListener {playPing()
it.findNavController().navigate(R.id.action_levelSelectFragment_to_levelFragment)
}
}
fun playPing(){
val pIntent = Intent(activity, SoulSoundService::class.java)
pIntent.putExtra("name", R.raw.dbping)
activity?.startService(pIntent)
}
And the service:
open class SoulSoundService : Service() {
private lateinit var mSoundPool: SoundPool
private val myBinder = MyLocalBinder()
override fun onCreate() {
val mAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_GAME)
.build()
mSoundPool = SoundPool.Builder()
.setMaxStreams(14)
.setAudioAttributes(mAttributes)
.build()
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
if (intent.hasExtra("Load")) {
loadPings()
}
if (intent.hasExtra("name")) {
soundID = intent.getIntExtra("Name", soundID)
playPing(soundID)
}
if (intent.hasExtra("Stop")) {
stopSelf()
}
return super.onStartCommand(intent, flags, startId)
}
fun playPing(soundID: Int){
mSoundPool.play(soundID, 1f, 1f, 1, 1, 1f)
}
fun loadPings() {
val abping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.abping, 1)
val aping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.aping, 1)
val bbping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.bbping, 1)
val bping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.bping, 1)
val cping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.cping, 1)
val dbping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.dbping, 1)
val dping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.dping, 1)
val ebping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.ebping, 1)
val eping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.eping, 1)
val fping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.fping, 1)
val gbping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.gbping, 1)
val gping = mSoundPool.load(this, com.example.soulfetchnew.R.raw.gping, 1)
mSoundPool.setOnLoadCompleteListener(object: SoundPool.OnLoadCompleteListener {
override fun onLoadComplete(mSoundPool: SoundPool, sampleId:Int,
status:Int) {
println("SOUNDS ARE LOADED")
}
})
}
Here´s the log of MainActivity:
2019-02-06 14:12:14.225 17449-17449/com.example.soulfetchnew I/Choreographer: Skipped 57 frames! The application may be doing too much work on its main thread.
2019-02-06 14:12:14.253 17449-17467/com.example.soulfetchnew D/EGL_emulation: eglMakeCurrent: 0xb3485120: ver 3 0 (tinfo 0xb3483780)
2019-02-06 14:12:15.537 17449-17493/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:16.434 17449-17496/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:17.113 17449-17499/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:17.270 17449-17449/com.example.soulfetchnew I/Choreographer: Skipped 183 frames! The application may be doing too much work on its main thread.
2019-02-06 14:12:17.524 17449-17502/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:17.573 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:17.573 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:17.705 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:18.175 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:18.322 17449-17505/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:18.508 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:19.230 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:19.242 17449-17508/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:19.403 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:19.412 17449-17511/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:19.583 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:19.591 17449-17514/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:19.732 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:19.739 17449-17517/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:19.879 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:19.886 17449-17520/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:20.034 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
2019-02-06 14:12:20.044 17449-17523/com.example.soulfetchnew I/OMXClient: MuxOMX ctor
2019-02-06 14:12:20.203 17449-17449/com.example.soulfetchnew I/System.out: SOUNDS ARE LOADED
And in the new fragment:
2019-02-06 14:12:23.141 17449-17454/com.example.soulfetchnew I/art: Do partial code cache collection, code=20KB, data=29KB
2019-02-06 14:12:23.141 17449-17454/com.example.soulfetchnew I/art: After code cache collection, code=20KB, data=29KB
2019-02-06 14:12:23.141 17449-17454/com.example.soulfetchnew I/art: Increasing code cache capacity to 128KB
2019-02-06 14:12:26.787 17449-17454/com.example.soulfetchnew I/art: Do partial code cache collection, code=61KB, data=60KB
2019-02-06 14:12:26.855 17449-17454/com.example.soulfetchnew I/art: After code cache collection, code=60KB, data=60KB
2019-02-06 14:12:26.855 17449-17454/com.example.soulfetchnew I/art: Increasing code cache capacity to 256KB
2019-02-06 14:12:26.917 17449-17449/com.example.soulfetchnew W/SoundPool: sample 2131623947 not READY
You have to use the value returned by load()
to play the sound. That is, abping, aping
, etc. You are trying to play them based on the resource ID, which is incorrect. The resource ID is only used for loading the sound (as you have done).