in Education by
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)

1 Answer

0 votes
by
Media player instance should also be released in onDestroy() override fun onDestroy() { mediaPlayer?.release() mediaPlayer = null super.onDestroy() }

Related questions

0 votes
    for all the examples on the internet i cant figure out when and how is kotlins let ran? if(phones ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    for all the examples on the internet i cant figure out when and how is kotlins let ran? if(phones ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I've had several occasions in my selenium tests where I decided to use Selenium's click_and_hold() (source ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    State true or false. When you have finished your work in the IMG or ABAP Workbench, or have reached a certain stage, you can release the request. A. True B. False...
asked Feb 20, 2023 in Technology by JackTerrance
0 votes
    I'm making a new Android project, with the standard 'app' module, as well as a library project ( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Deadlock is a situation when thread is waiting for other thread to release acquired object. (a) True (b) ... Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    I have a Selenium Python automated regression test script running on our 64bit Server on IE11, Windows ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I was reading a book on programming skills wherein the author asks the interviewee, "How do you crash a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I was reading a book on programming skills wherein the author asks the interviewee, "How do you crash a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I was reading a book on programming skills wherein the author asks the interviewee, "How do you crash a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I was reading a book on programming skills wherein the author asks the interviewee, "How do you crash a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I was reading a book on programming skills wherein the author asks the interviewee, "How do you crash a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I was reading a book on programming skills wherein the author asks the interviewee, "How do you crash a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Is there a way to view the register contents in each stack frame in a crash dump? The registers ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
...