使用 mediaPlayer.release() 时 mediaPlayer 崩溃

mediaPlayer crash when using mediaPlayer.release()

我们有带有儿童歌曲列表的 RecyclerView,当您单击一首歌曲时,它会将您带到另一个 activity mediaPlayer。问题是当我播放一首歌曲然后使用后退按钮停止播放歌曲时没关系,应用程序停止了,这就是问题所在,但是当我评论 //mediaPlayer.release() 时,应用程序运行良好,它没有崩溃,歌曲只在我返回时停止,但问题是当我从 RecyclerView 中选择另一首歌曲并单击 PlayBotton 时,歌曲从头开始播放,但 SeekBar 一直寻找到结尾并且没有移动。有没有人知道如何解决这个问题?

我试过了,但是 mediaPlayer.release() 在 Try 和 Catch 之间,但问题还是一样。

class ChildrenSongsPreview : AppCompatActivity() {

private var handler = Handler()
var mediaPlayer: MediaPlayer? = null
private var startTime = 0.0
private var finalTime = 0.0

private var updateSongTime = object : Runnable {
    override fun run() {
        startTime = mediaPlayer?.currentPosition!!.toDouble()
        txt_playing_duration.text = String.format(
            "%d:%d", TimeUnit.MILLISECONDS.toMinutes(startTime.toLong()),
            TimeUnit.MILLISECONDS.toSeconds(startTime.toLong()) -
                    TimeUnit.MINUTES.toSeconds(
                        TimeUnit.MILLISECONDS.toMinutes(startTime.toLong())
                    )
        )

        songs_seekbar.progress = startTime.toInt()
        handler.postDelayed(this, 100)
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_children_songs_preview)

    var pos = intent.getIntExtra("KEY_SONG", 0)
    var song_ID: Int = R.raw.zahab_elel

    when (pos) {
        0 -> {
            song_ID = R.raw.mama_zmanha_gaya
            txt_song_name_preview.text = "ماما زمانها جاية"
        }
        1 -> {
            song_ID = R.raw.zahab_elel
            txt_song_name_preview.text = "ذهب الليل"
        }
        2 -> {
            song_ID = R.raw.gdo_ali
            txt_song_name_preview.text = "جدو علي"
        }
        3 -> {
            song_ID = R.raw.ebre2_shay
            txt_song_name_preview.text = "إبريق الشاي"
        }
    }

    var position = 0
    mediaPlayer = MediaPlayer.create(this, song_ID)

    //set song duration
    finalTime = mediaPlayer?.duration!!.toDouble()
    txt_song_duration.text = String.format(
        "%d:%d",
        TimeUnit.MILLISECONDS.toMinutes(finalTime.toLong()),
        TimeUnit.MILLISECONDS.toSeconds(finalTime.toLong()) -
                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(finalTime.toLong()))
    )

    btn_play.setOnClickListener {
        mediaPlayer?.seekTo(position)
        mediaPlayer?.start()
        btn_play.visibility = View.GONE
        btn_pause.visibility = View.VISIBLE

        if (oneTimeOnly == 0) {
            songs_seekbar!!.max = finalTime.toInt()
            oneTimeOnly = 1
        }

        songs_seekbar!!.progress = startTime.toInt()
        handler.postDelayed(updateSongTime, 100)
    }

    btn_pause.setOnClickListener {
        position = mediaPlayer?.currentPosition!!
        mediaPlayer?.pause()
        btn_pause.visibility = View.GONE
        btn_play.visibility = View.VISIBLE
    }

    btn_stop.setOnClickListener {
        mediaPlayer?.stop()
        position = 0
        btn_pause.visibility = View.GONE
        btn_play.visibility = View.VISIBLE
        mediaPlayer = MediaPlayer.create(this, song_ID)
    }

    mediaPlayer?.setOnCompletionListener {
        position = 0
        btn_pause.visibility = View.GONE
        btn_play.visibility = View.VISIBLE
        mediaPlayer = MediaPlayer.create(this, song_ID)
    }

    songs_seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
        override fun onProgressChanged(seekBar: SeekBar?, p: Int, fromUser: Boolean) {
            if (fromUser) {
                position = p
            }
        }

        override fun onStartTrackingTouch(seekBar: SeekBar?) {
        }

        override fun onStopTrackingTouch(seekBar: SeekBar?) {
            mediaPlayer?.seekTo(position)
        }
    })
}

override fun onDestroy() {
    super.onDestroy()
    mediaPlayer?.stop()
    mediaPlayer?.release()
}

companion object {
    var oneTimeOnly = 0
  }
}

媒体播放器实例也应在 onDestroy()

中发布
override fun onDestroy() {
        mediaPlayer?.release()
        mediaPlayer = null
        super.onDestroy()
    }