如何在 android studio 中保存 seekbar 值?

How to save seekbar value in android studio?

我有一个媒体播放器,底部有一个搜索栏 sheet,每当我在 URL 上播放任何声音时,搜索栏就可以正常播放,但是当我关闭底部 sheet 并再次打开它,搜索栏达到其值的 100%,声音仍在开始。

如何在后台播放声音时让搜索栏工作?

请帮助我,在此先感谢。

我的片段

    private var mediaPlayer:MediaPlayer? = null
 private var oTime = 0
private var sTime: Int = 0
private var eTime: Int = 0

  fun quranMp3(){

 quranPlay?.setOnClickListener {
        // الفاتحة

        if (quranPageNum?.equals(0)!!){

            if (mediaPlayer == null){
                mediaPlayer = MediaPlayer.create(context, Uri.parse("https://server7.mp3quran.net/basit/Almusshaf-Al-Mojawwad/001.mp3"))
                Log.d("quranMp3", "ID: ${mediaPlayer!!.audioSessionId}")

                val position = mediaPlayer!!.currentPosition/100
                mp3SeekBar.progress = position
                //totalTime = mediaPlayer!!.duration

                eTime = mediaPlayer!!.duration.toLong().toInt()
                sTime = mediaPlayer!!.currentPosition.toLong().toInt()

                if (oTime == 0) {
                    quranSeekBar!!.setMax(eTime)
                    oTime = 1
                }

                playerDuration?.text = String.format(
                    "%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(eTime.toLong()),
                    TimeUnit.MILLISECONDS.toSeconds(eTime.toLong()) - TimeUnit.MINUTES.toSeconds(
                        TimeUnit.MILLISECONDS.toMinutes(eTime.toLong())))

                playerPosition?.text = String.format(
                    "%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(sTime.toLong()),
                    TimeUnit.MILLISECONDS.toSeconds(sTime.toLong()) - TimeUnit.MINUTES.toSeconds(
                        TimeUnit.MILLISECONDS.toMinutes(sTime.toLong())))





                initializeSeekBar()



            }

            mediaPlayer?.start()
            Log.d("QuranMp3", "Duration: ${mediaPlayer!!.duration/1000} seconds")

        }

   quranPause?.setOnClickListener {

        if (mediaPlayer !== null){
            mediaPlayer?.pause()
            Log.d("QuranMp3", "Paused at: ${mediaPlayer!!.duration/1000} seconds")

        }



 mp3SeekBar.setOnSeekBarChangeListener( object : SeekBar.OnSeekBarChangeListener{

            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {


                if (fromUser){
                    mediaPlayer?.seekTo(progress)

                }
            }
            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                val Toast = Toast.makeText(context,"تتبع..", Toast.LENGTH_LONG)
                Toast.setGravity(Gravity.CENTER_VERTICAL, 50, 50)
                Toast.show()
            }
            override fun onStopTrackingTouch(seekBar: SeekBar) {
                val Toast = Toast.makeText(context,"يرجى الانتظار للحصول على البيانات..", Toast.LENGTH_LONG)
                Toast.setGravity(Gravity.CENTER_VERTICAL, 50 ,50)
                Toast.show()


            }

        })

 private fun initializeSeekBar() {

    val mp3SeekBar = view?.findViewById<SeekBar>(R.id.mp3SeekBar)
    mp3SeekBar?.max = mediaPlayer!!.duration
    val playerPosition = view?.findViewById<TextView>(R.id.player_position)
    val playerDuration =  view?.findViewById<TextView>(R.id.player_duraation)

    val seekbar = this.activity?.getSharedPreferences("QuranPdf", Context.MODE_PRIVATE)


    val handler = Handler()
    handler.postDelayed(object : Runnable {

        override fun run() {


            try {

                sTime = mediaPlayer!!.currentPosition

                playerDuration!!.text = String.format(
                    "%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(eTime.toLong()),
                    TimeUnit.MILLISECONDS.toSeconds(eTime.toLong()) - TimeUnit.MINUTES.toSeconds(
                        TimeUnit.MILLISECONDS.toMinutes(eTime.toLong())))

                playerPosition?.text = String.format(
                    "%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(sTime.toLong()),
                    TimeUnit.MILLISECONDS.toSeconds(sTime.toLong()) - TimeUnit.MINUTES.toSeconds(
                        TimeUnit.MILLISECONDS.toMinutes(sTime.toLong())))


                mp3SeekBar?.progress = sTime //mediaPlayer!!.currentPosition
                handler.postDelayed(this, 1000)

            } catch (e: Exception) {
                mp3SeekBar?.progress = 0

            }
        }

    }, 0)
}

我的应用照片

当我打开底部 sheet 并按下播放时,一切正常。

第一个

!

但是当我关闭底部 sheet 并再次打开它时,声音正常,但搜索栏和文本不起作用。

第二个

Java 和 Kotlin 是可以接受的

您将 seekbar 更新逻辑包装在 mediaplayer == null 条件中,一旦 MediaPlayer 初始化,该条件将是 false

quranPlay?.setOnClickListener {
        // الفاتحة
      if (quranPageNum?.equals(0)!!){
            if (mediaPlayer == null){
                mediaPlayer = MediaPlayer.create(context, Uri.parse("https://server7.mp3quran.net/basit/Almusshaf-Al-Mojawwad/001.mp3"))
             }//end it here, rest the code should be outside if condition
       //seek bar logic and rest of the code
       }
  }             

我找到了这个问题的答案。显然,我必须在

中初始化搜索栏

onResume 方法。

这就是我的问题的答案。我只需要添加这段代码

 override fun onResume() {


    try {

        if (mediaPlayer !=null && mediaPlayer!!.isPlaying){
            initializeSeekBar()
        }

    }catch (e : Exception){
        e.printStackTrace()
    }

    super.onResume()
}