Last active
July 31, 2021 08:05
-
-
Save mattiaferigutti/4c9a1667f07b872935f714ae82730ce4 to your computer and use it in GitHub Desktop.
Get a file from assets ExoPlayer 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.studio.mattiaferigutti.exoplayertest | |
import android.annotation.SuppressLint | |
import android.net.Uri | |
import android.os.Bundle | |
import android.util.Log | |
import android.view.View | |
import androidx.appcompat.app.AppCompatActivity | |
import com.google.android.exoplayer2.* | |
import com.google.android.exoplayer2.source.ProgressiveMediaSource | |
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector | |
import com.google.android.exoplayer2.upstream.* | |
import com.google.android.exoplayer2.upstream.DataSource.Factory | |
import com.google.android.exoplayer2.util.Util | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity(), Player.EventListener { | |
private var player: SimpleExoPlayer? = null | |
private var playWhenReady = true | |
private var currentWindow = 0 | |
private var playbackPosition: Long = 0 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
} | |
public override fun onStart() { | |
super.onStart() | |
if (Util.SDK_INT > 23) { | |
initializePlayer("assets:///your_file.your_extension") //"assets:///pillwheel_pills.mp4" | |
} | |
} | |
public override fun onResume() { | |
super.onResume() | |
hideSystemUi() | |
if (Util.SDK_INT <= 23 || player == null) { | |
initializePlayer("assets:///your_file.your_extension") //"assets:///pillwheel_pills.mp4" | |
} | |
} | |
public override fun onPause() { | |
super.onPause() | |
if (Util.SDK_INT <= 23) { | |
releasePlayer() | |
} | |
} | |
public override fun onStop() { | |
super.onStop() | |
if (Util.SDK_INT > 23) { | |
releasePlayer() | |
} | |
} | |
private fun initializePlayer(path: String) { | |
if (player == null) { | |
val trackSelector = DefaultTrackSelector(this) | |
trackSelector.setParameters( | |
trackSelector.buildUponParameters().setMaxVideoSizeSd()) | |
player = SimpleExoPlayer.Builder(this) | |
.setTrackSelector(trackSelector) | |
.build() | |
} | |
video_view?.player = player | |
video_view?.requestFocus() | |
val dataSourceFactory = Factory { AssetDataSource(this@MainActivity) } | |
val videoSource = ProgressiveMediaSource.Factory(dataSourceFactory) | |
.createMediaSource(Uri.parse(path)) | |
player?.playWhenReady = playWhenReady | |
player?.seekTo(currentWindow, playbackPosition) | |
player?.addListener(this) | |
player?.prepare(videoSource) | |
} | |
private fun releasePlayer() { | |
if (player != null) { | |
playbackPosition = player?.currentPosition!! | |
currentWindow = player?.currentWindowIndex!! | |
playWhenReady = player?.playWhenReady!! | |
player?.removeListener(this) | |
player?.release() | |
player = null | |
} | |
} | |
/** | |
* set fullscreen | |
*/ | |
@SuppressLint("InlinedApi") | |
private fun hideSystemUi() { | |
video_view?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE | |
or View.SYSTEM_UI_FLAG_FULLSCREEN | |
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE | |
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | |
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | |
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) | |
} | |
override fun onPlayerError(error: ExoPlaybackException) { | |
super.onPlayerError(error) | |
//handle the error | |
} | |
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) { | |
val stateString: String = when (playbackState) { | |
ExoPlayer.STATE_IDLE -> "ExoPlayer.STATE_IDLE -" | |
ExoPlayer.STATE_BUFFERING -> "ExoPlayer.STATE_BUFFERING -" | |
ExoPlayer.STATE_READY -> "ExoPlayer.STATE_READY -" | |
ExoPlayer.STATE_ENDED -> "ExoPlayer.STATE_ENDED -" | |
else -> "UNKNOWN_STATE -" | |
} | |
Log.d(TAG, "changed state to " + stateString | |
+ " playWhenReady: " + playWhenReady) | |
} | |
companion object { | |
private val TAG = MainActivity::class.java.name | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can find a better example here 🙃