Home > front end >  Audio keeps playing when exoplayer releases
Audio keeps playing when exoplayer releases

Time:03-24

I am using Exoplayer in my dialog. I want the video to play automatically when dialog opens. When simpleExoPlayer.prepare() snippet is active I am able to do autoplay but when I close the dialog audio keeps playing. Before activating simpleExoPlayer.prepare() audio stops when I dismiss dialog. Is there another method to autoplay exoplayer or stop the audio when dialog dismiss?

class VideoViewDialog (context: Context) : BaseDialog<LayoutDialogVideoViewBinding>(context) {


private var videoUrl : String = ""
private lateinit var simpleExoPlayer: ExoPlayer

override fun populateUi() {
    setCanceledOnTouchOutside(true)
    mBinding?.apply {

        initializePlayer()
    }
}


private fun initializePlayer() {
    val mediaDataSourceFactory: DataSource.Factory = DefaultDataSource.Factory(context)

    val mediaSource = ProgressiveMediaSource.Factory(mediaDataSourceFactory).createMediaSource(
        MediaItem.fromUri(videoUrl))

    val mediaSourceFactory: MediaSource.Factory = DefaultMediaSourceFactory(mediaDataSourceFactory)

    simpleExoPlayer = ExoPlayer.Builder(context)
        .setMediaSourceFactory(mediaSourceFactory)
        .build()

    simpleExoPlayer.addMediaSource(mediaSource)

    simpleExoPlayer.playWhenReady = true
    simpleExoPlayer.prepare()

    mBinding?.apply {

        playerView.player = simpleExoPlayer
        playerView.requestFocus()


    }
    simpleExoPlayer.play()

}

private fun releasePlayer() {
    simpleExoPlayer.release()
}

public override fun onStart() {
    super.onStart()

    if (Util.SDK_INT > 23) initializePlayer()
}

public override fun onStop() {
    super.onStop()

    if (Util.SDK_INT > 23) releasePlayer()
}

override fun getLayoutRes(): Int {
    return R.layout.layout_dialog_video_view
}


companion object{
    fun newInstance(
        context: Context,
        videoUrl : String,
    ) : VideoViewDialog{
        val dialog = VideoViewDialog(context)
        dialog.also {
            it.videoUrl = videoUrl
        }
        return dialog
    }
}
}

I tried .stop, clearVideoSurface(), playerView.player = null before .release(). Didn't work

CodePudding user response:

Seems like you called initializePlayer() twice resulting in two Exoplayer Instance playing which you're only able to release the one the simpleExoPlayer variable hold reference to

  • Related