Home > Enterprise >  Audio file still loops after setting setOnCompletionListener android studio kotlin
Audio file still loops after setting setOnCompletionListener android studio kotlin

Time:10-13

I have added the setOnCompletionListener into my code but still, the audio file kept on looping after it is finished (Audio File retrieved from firebase storage URL).....I am not too sure what the problem is as the code I added seems pretty fine...

Here is the code for the activity that allow users to play audio file

class DetailActivity : AppCompatActivity() {

private lateinit var imageViewClass: ImageView
private lateinit var textViewClassName: TextView
private lateinit var textViewClassDes: TextView
private lateinit var textViewClassContent: TextView
private var stop:Boolean = false
var mediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_detail)

    val btnPlay: Button = findViewById(R.id.btnPlay)
    val btnStop: Button = findViewById(R.id.btnStop)


    imageViewClass = findViewById(R.id.imageViewClassImage)
    textViewClassName = findViewById(R.id.textViewClassName)
    textViewClassDes = findViewById(R.id.textViewClassDescription)
    textViewClassContent = findViewById(R.id.textViewClassContent)


    val classImage = intent.getStringExtra("class_image")
    val className = intent.getStringExtra("class_name")
    val classDes = intent.getStringExtra("class_des")
    val classContent = intent.getStringExtra("class_content")
    val classAudio = intent.getStringExtra("class_audio")

    Glide.with(this)
        .load(classImage)
        .centerCrop()
        .into(imageViewClass)
    textViewClassName.text = className
    textViewClassDes.text = classDes
    textViewClassDes.text = classDes!!.replace("\\n", "\n")
    textViewClassContent.text = classContent
    textViewClassContent.text = classContent!!.replace("\\n", "\n")

    btnPlay.isEnabled = true
    btnStop.isEnabled = false

    btnPlay.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
            if (mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(this@DetailActivity, Uri.parse(classAudio))
                mediaPlayer!!.isLooping = true
                mediaPlayer!!.start()
                btnStop.isEnabled = true
                btnPlay.isEnabled = false
                Toast.makeText(applicationContext, "Audio Starts", Toast.LENGTH_SHORT).show()
            } else {
                mediaPlayer!!.start()
            }
        }
    })

    mediaPlayer?.setOnCompletionListener {
        btnPlay.isEnabled = true
        btnStop.isEnabled = false
        mediaPlayer!!.stop()
        mediaPlayer!!.reset()
        mediaPlayer!!.release()
        Toast.makeText(applicationContext, "end", Toast.LENGTH_SHORT).show()
    }

    btnStop.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
            if(mediaPlayer!!.isPlaying){
                stop = false
                mediaPlayer!!.stop()
                mediaPlayer!!.reset()
                mediaPlayer!!.release()
                mediaPlayer = null
                btnPlay.isEnabled = true
                btnStop.isEnabled = false
                Toast.makeText(applicationContext,"Audio Stops",Toast.LENGTH_SHORT).show()
            }
        }
    })
}

override fun onDestroy(){
    super.onDestroy()
    if(mediaPlayer != null){
        mediaPlayer!!.stop()
        mediaPlayer!!.reset()
        mediaPlayer!!.release()
        mediaPlayer = null
    }
}

}

CodePudding user response:

You don't create media player during fragment creation. So during call:

mediaPlayer?.setOnCompletionListener {
        btnPlay.isEnabled = true
        btnStop.isEnabled = false
        mediaPlayer!!.stop()
        mediaPlayer!!.reset()
        mediaPlayer!!.release()
        Toast.makeText(applicationContext, "end", Toast.LENGTH_SHORT).show()
    }

mediaPlayer is always null. Therefore the listener is never set.

Just move this code to the onClick method after mediaPlayer is being created

CodePudding user response:

I tried both ways:

        btnPlay.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
            mediaPlayer?.setOnCompletionListener {
                btnPlay.isEnabled = true
                btnStop.isEnabled = false
                mediaPlayer!!.stop()
                mediaPlayer!!.reset()
                mediaPlayer!!.release()
                Toast.makeText(applicationContext, "end", Toast.LENGTH_SHORT).show()
            }
            if (mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(this@DetailActivity, Uri.parse(classAudio))
                mediaPlayer!!.isLooping = true
                mediaPlayer!!.start()
                btnStop.isEnabled = true
                btnPlay.isEnabled = false
                Toast.makeText(applicationContext, "Audio Starts", Toast.LENGTH_SHORT).show()
            } else {
                mediaPlayer!!.start()
            }
        }
    })

and

btnPlay.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
            if (mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(this@DetailActivity, Uri.parse(classAudio))
                mediaPlayer?.setOnCompletionListener {
                    btnPlay.isEnabled = true
                    btnStop.isEnabled = false
                    mediaPlayer!!.stop()
                    mediaPlayer!!.reset()
                    mediaPlayer!!.release()
                    Toast.makeText(applicationContext, "end", Toast.LENGTH_SHORT).show()
                }
                mediaPlayer!!.isLooping = true
                mediaPlayer!!.start()
                btnStop.isEnabled = true
                btnPlay.isEnabled = false
                Toast.makeText(applicationContext, "Audio Starts", Toast.LENGTH_SHORT).show()
            } else {
                mediaPlayer!!.start()
            }
        }
    })

but still doesn't work..

  • Related