title: How to check internet connection before playing video?
body: <p>I have created the activity of <code>exoplayer</code>, when I click play button from <code>MainActivity</code> It should open my exoplayer activity file and play my hls source. Even though there is no mobile data or wifi connection, it is trying to open exoplayer and showing black with controls. How to fix this with some condition. My android project is in kotlin. I am using exoplayer version 2.6.0. </p>
  
  <pre class="lang-java prettyprint-override"><code>    package com.example.sample
  
      import android.net.Uri
      import android.os.Bundle
      import android.os.Handler
      import android.view.View
      import android.view.WindowManager
      import androidx.appcompat.app.AppCompatActivity
      import com.google.android.exoplayer2.DefaultLoadControl
      import com.google.android.exoplayer2.DefaultRenderersFactory
      import com.google.android.exoplayer2.ExoPlayerFactory
      import com.google.android.exoplayer2.SimpleExoPlayer
      import com.google.android.exoplayer2.source.hls.HlsMediaSource
      import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
      import com.google.android.exoplayer2.ui.SimpleExoPlayerView
      import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
  
      class playlive : AppCompatActivity() {
  
          private var player: SimpleExoPlayer? = null
          private val handler = Handler()
  
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_playlive)
  
              window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
              actionBar?.hide()
              window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
  
           play()
  
          }
  
          private fun play(){
          player = 
          ExoPlayerFactory.newSimpleInstance(DefaultRenderersFactory(this), 
          DefaultTrackSelector(), DefaultLoadControl())
          val playerView = findViewById&lt;SimpleExoPlayerView&gt;(R.id.player_view)
          val uri = Uri.parse("http://localhost/tv2hls/live.m3u8")
  
          playerView.player = player
  
          val dataSourceFactory = DefaultDataSourceFactory(this, "user-agent")
          val mediaSource = HlsMediaSource(uri, dataSourceFactory, handler, null)
  
          player?.prepare(mediaSource)
          player?.playWhenReady = true
      }
  
          override fun onStop() {
              super.onStop()
              releasePlayer()
              releaseInstance()
          }
  
          override fun onResume() {
              super.onResume()
              releasePlayer()
              resumePlayer()
          }
  
  
          override fun onDestroy() {
              super.onDestroy()
              releasePlayer()
              releaseInstance()
          }
  
          private fun releasePlayer(){
              player?.release()
          }
  
          private fun resumePlayer(){
              play()
          }
  
      }
  </code></pre>