在的Soundpool服务不加载

问题描述 投票:0回答:1

我有我想在不同的片段,以保持加载的Soundpool。所以,我在我的首发活动服务的onCreate意图加载它,然后尝试在其他片段onClickListeners(以及其他方法)与其它意图进行播放。尽管它打印线,以确认正在加载的声音,后来不打他们,说:“样品X没有准备好”。我究竟做错了什么?

Here's的主要活动的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
    }

和onclicklistener(在片段)

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

和服务:

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日志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

而在新的片段:

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
android android-intent service kotlin soundpool
1个回答
1
投票

你必须使用由load()返回播放声音的价值。也就是说,abping, aping,等你尝试基于资源ID,这是不正确的播放它们。资源ID仅用于加载声音(如您已完成)。

© www.soinside.com 2019 - 2024. All rights reserved.