MediaPlayer服务无法(重新)启动

时间:2019-06-02 07:27:45

标签: android android-fragments android-intent kotlin android-mediaplayer

我在服务中使用MediaPlayer,该服务仅在打开某些片段时才可以播放。按下主页按钮时,它应该停止/暂停播放。

在这些片段中,我有一个按钮,可以重新加载相同的片段,但可以激活/禁用不同的按钮。当按下该按钮时,媒体播放器将停止,并且在新创建片段时不会再次启动。我不明白为什么它不会再次开始。

按钮如下所示:

nextbutton.setOnClickListener {
    FullscreenActivity.level.levelSelection(FullscreenActivity.makeLevel.nextlevelsetup)
    NavHostFragment.findNavController(fragment5).navigate(R.id.action_global_learningmode)
        }

在片段的onStop()中,我称之为:

        var sIntent = Intent(mContext, BordunService::class.java)
        sIntent.putExtra("Pause", "Ein")
        activity?.startService(sIntent)

并在其onResume()中再次播放:

    var sIntent = Intent(mContext, BordunService::class.java)
    sIntent.putExtra("Play", "Ein")
    activity?.startService(sIntent)

服务类别是这样:

open class BordunService : Service() {

private lateinit var mPlayer: MediaPlayer
private var mLength = 0

override fun onBind(intent: Intent): IBinder? {
    return null
}

override fun onCreate() {
    mPlayer = MediaPlayer.create(this, R.raw.accordeon_tief)  
}

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {

if (intent.hasExtra("Play")) {
        if (mLength != 0) {
                mPlayer.seekTo(mLength)
                mPlayer.start()
          } else {
        playBordun()
    }
}

if (intent.hasExtra("Pause")) {
    mLength = mPlayer.currentPosition
    mPlayer.pause()
}

if (intent.hasExtra("Stop")) {
    mPlayer.stop()
    mPlayer.release()
    stopSelf()
}
    return START_NOT_STICKY
    }

override fun onDestroy() {
    stopSelf()
}

private fun playBordun() {
                        fun playMPlayer() {
mPlayer.setOnCompletionListener(object: MediaPlayer.OnCompletionListener  {
                override fun onCompletion(p0: MediaPlayer?) {
                    println("completem")
                    selectMBordun()
                }
            })
            mPlayer.setOnPreparedListener(object:MediaPlayer.OnPreparedListener {
                override fun onPrepared(p0: MediaPlayer?) {
                    println("preparedm")
                    mPlayer.start()
                }
            })
        } GlobalScope.launch {
            playMPlayer()
        }
    }

fun selectMBordun() {
    println("selectionstarted")
    mPlayer.release()
    val randomGenerator = Random()
    val bordunInt = randomGenerator.nextInt(3) + 1
    when (bordunInt) {
        1 -> {
            mPlayer = MediaPlayer.create(this, R.raw.accordeon_tief)
            playM()
        }
        2 -> {
            mPlayer = MediaPlayer.create(this, R.raw.cello)
            playM()
        }
        3 -> {
            mPlayer = MediaPlayer.create(this, R.raw.tuba)
            playM()
        }
    }
}
fun playM() {
    println("playstartedM")
    mPlayer.setOnPreparedListener(object:MediaPlayer.OnPreparedListener {
        override fun onPrepared(p0: MediaPlayer?) {
            println("preparedplayerM")
            mPlayer.start()
        }
    })


    mPlayer.setOnCompletionListener(object: MediaPlayer.OnCompletionListener  {
        override fun onCompletion(p0: MediaPlayer?) {
            println("completeplayerM")
            selectMBordun()
        }
    })
}
}

0 个答案:

没有答案