we have RecyclerView with list of children songs, when you click on a song it takes you to another activity mediaPlayer.. The problem is that when I play a song and then use back bottom the song stop and that is ok, and application stop this is the problem, but when I comment //mediaPlayer.release() the application works well it didn't crash and the song only stop when I go back, but the problem is that when I choose another song from RecyclerView and click PlayBotton the song starts from the start but the SeekBar seeks to the end and don't move. does any body have idea how to solve this?
I tried to but mediaPlayer.release() between Try and Catch but the problem still the same.
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
}
}
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)