Last active
February 3, 2019 21:19
-
-
Save Bradleycorn/d658ba979bece7e5c2834d24ad7bd23b to your computer and use it in GitHub Desktop.
MotionLayout - MainActivity
This file contains hidden or 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
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
title = getString(R.string.app_name) | |
setupVideo() | |
} | |
override fun onPause() { | |
super.onPause() | |
if (currentPlaybackState == MainActivityViewModel.VideoPlaybackState.PLAYING) { | |
video_player.pause() | |
} | |
} | |
override fun onStart() { | |
super.onStart() | |
if (currentPlaybackState == MainActivityViewModel.VideoPlaybackState.PLAYING) { | |
video_player.ensureSurface() | |
video_player.showControls() | |
attachVideoListeners() | |
} | |
} | |
private fun setupVideo() { | |
// Set the video URL | |
video_player.videoUrl = viewModel.videoUrl | |
viewModel.playbackState.observe(this, Observer { playbackState -> | |
if (playbackState != currentPlaybackState) { | |
invalidateOptionsMenu() // update the play/stop icon in the options menu. | |
currentPlaybackState = playbackState | |
when (currentPlaybackState) { | |
MainActivityViewModel.VideoPlaybackState.PLAYING -> { | |
playVideo() | |
} | |
else -> { | |
stopVideo() | |
} | |
} | |
} | |
updateVideoLayout() | |
}) | |
viewModel.layoutState.observe(this, Observer { layoutState -> | |
if (layoutState != currentLayoutState) { | |
currentLayoutState = layoutState | |
updateVideoLayout() | |
} | |
}) | |
} | |
private fun updateVideoLayout() { | |
val isFullscreen = (currentLayoutState == MainActivityViewModel.VideoLayoutState.FULLSCREEN) | |
if (isFullscreen && resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE) { | |
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE | |
} else if (!isFullscreen && resources.configuration.orientation != Configuration.ORIENTATION_PORTRAIT) { | |
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT | |
} | |
video_player.setFullScreenMode(isFullscreen) | |
video_player.setPreserveAspectRatio(isFullscreen) | |
val videoState = Pair(currentPlaybackState, currentLayoutState) | |
val newState = when (videoState) { | |
Pair(MainActivityViewModel.VideoPlaybackState.PLAYING, MainActivityViewModel.VideoLayoutState.FULLSCREEN) -> R.id.video_state_fullscreen | |
Pair(MainActivityViewModel.VideoPlaybackState.PLAYING, MainActivityViewModel.VideoLayoutState.PIP) -> R.id.video_state_pip_playing | |
Pair(MainActivityViewModel.VideoPlaybackState.STOPPED, MainActivityViewModel.VideoLayoutState.PIP) -> R.id.video_state_pip_stopped | |
Pair(MainActivityViewModel.VideoPlaybackState.PLAYING, MainActivityViewModel.VideoLayoutState.EMBEDDED) -> R.id.video_state_embedded_playing | |
else -> R.id.video_state_embedded_stopped | |
} | |
if (activity_container.currentState != newState) { | |
activity_container.transitionToState(newState) | |
} | |
} | |
private fun playVideo() { | |
video_player.play() | |
attachVideoListeners() | |
} | |
private fun stopVideo() { | |
if (video_player.isPlaying) { | |
video_player.stop() | |
video_player.setVideoListener(null) | |
} | |
} | |
private fun attachVideoListeners() { | |
video_player.setVideoListener(videoPlayerCallbacks) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment