I'm new to Android Development here, and I'm having a hard time integrating and using ExoPlayer in my project. I found a lot of tutorials but as expected I am getting a lot of different approaches and most are outdated and hard to build. I found one approach that works, but the problem is, that it plays m3u8 files. I want to make Exoplayer play mp4 files (from a URL). Here is the code for the Exoplayer demo that works for me but only plays m3u8 files.
private lateinit var binding: ActivityMainBinding
private var exoPlayer: ExoPlayer? = null
private var playbackPosition = 0L
private var playWhenReady = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
preparePlayer()
}
private fun preparePlayer() {
exoPlayer = ExoPlayer.Builder(this).build()
exoPlayer?.playWhenReady = true
binding.playerView.player = exoPlayer
val defaultHttpDataSourceFactory = DefaultHttpDataSource.Factory()
val mediaItem =
MediaItem.fromUri(URL)
val mediaSource =
HlsMediaSource.Factory(defaultHttpDataSourceFactory).createMediaSource(mediaItem)
exoPlayer?.setMediaSource(mediaSource)
exoPlayer?.seekTo(playbackPosition)
exoPlayer?.playWhenReady = playWhenReady
exoPlayer?.prepare()
}
private fun releasePlayer() {
exoPlayer?.let { player ->
playbackPosition = player.currentPosition
playWhenReady = player.playWhenReady
player.release()
exoPlayer = null
}
}
override fun onStop() {
super.onStop()
releasePlayer()
}
override fun onPause() {
super.onPause()
releasePlayer()
}
override fun onDestroy() {
super.onDestroy()
releasePlayer()
}
companion object {
const val URL =
"sample m3u8 file URL here"
}
Please help me tweak or make the code play mp4 files. Please bare with me I'm totally new to Android development and ExoPlayer. I will greatly appreciate your help.