Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this is the code for my ViewModel, i'm using Koin Di:
- class VideoPlayerVM(
- private val savedStateHandle: SavedStateHandle,
- val player:Player,
- private val mediaScanner: MediaScanner,
- private val exoPlayerListener: ExoPlayerListener
- ): ViewModel() {
- private val videoUrisList = savedStateHandle.getStateFlow(Constants.VIDEO_URIS_SAVED_KEY,
- emptyList<Uri>()
- )
- private val videoIndex = savedStateHandle.getStateFlow(Constants.VIDEO_INDEX_SAVED_KEY,0)
- private val _videoPlayerState = MutableStateFlow(VideoPlayerState())
- val videoPlayerState = _videoPlayerState.asStateFlow()
- init {
- player.prepare()
- collectIsPlaying()
- collectIsLoading()
- collectDuration()
- collectCurrentPosition()
- collectBufferedPosition()
- }
- fun onAction(action: VideoPlayerAction){
- when(action){
- is VideoPlayerAction.GetVideosUri -> {
- player.clearMediaItems()
- val mediaUris = mediaScanner.getVideosInFolder(action.folder).map {
- it.videoUri
- }
- savedStateHandle[Constants.VIDEO_URIS_SAVED_KEY] = videoUrisList.value + mediaUris
- savedStateHandle[Constants.VIDEO_INDEX_SAVED_KEY] = action.videoIndex
- mediaUris.forEach {
- player.addMediaItem(MediaItem.fromUri(it!!))
- }
- Log.d(Constants.PLAYER_RESULTS_LOG, "Get Videos Uri Is Called and the Video Number is: ${videoIndex.value} ")
- setStateMediaItemsList(mediaUris)
- val currentVideo = _videoPlayerState.value.videoPlayerMediaItemList[videoIndex.value].mediaItem
- player.setMediaItem(currentVideo)
- player.playWhenReady = true
- }
- is VideoPlayerAction.OnFastForward -> {
- Log.d(Constants.PLAYER_RESULTS_LOG, "onFastForward is Called ")
- player.seekTo(player.currentPosition + 10000)
- }
- is VideoPlayerAction.OnRewind -> {
- Log.d(Constants.PLAYER_RESULTS_LOG, " Rewind is Called ")
- player.seekTo(player.currentPosition - 10000)
- }
- is VideoPlayerAction.OnSeek -> {
- player.seekTo(action.position)
- }
- }
- }
- private fun setStateMediaItemsList(mediaUris: List<Uri?>) {
- val videoItemList: MutableList<VideoPlayerMediaItem> = mutableListOf()
- mediaUris.forEach { uri ->
- if (uri != null){
- val videoItem = VideoPlayerMediaItem(
- name = "No Name",
- contentUri = uri,
- mediaItem = MediaItem.fromUri(uri)
- )
- videoItemList.add(videoItem)
- }
- }
- _videoPlayerState.update {
- _videoPlayerState.value.copy(videoPlayerMediaItemList = videoItemList)
- }
- }
- override fun onCleared() {
- exoPlayerListener.release()
- super.onCleared()
- player.release()
- }
- private fun collectIsPlaying(){
- viewModelScope.launch {
- exoPlayerListener.isPlaying.collect{ isPlaying ->
- _videoPlayerState.update {
- _videoPlayerState.value.copy(isPlaying = isPlaying)
- }
- }
- }
- }
- private fun collectIsLoading(){
- viewModelScope.launch {
- exoPlayerListener.isLoading.collect{ isLoading ->
- _videoPlayerState.update {
- _videoPlayerState.value.copy(isLoading = isLoading)
- }
- }
- }
- }
- private fun collectCurrentPosition(){
- viewModelScope.launch {
- exoPlayerListener.currentPosition.collect{ currentPosition ->
- _videoPlayerState.update {
- _videoPlayerState.value.copy(currentPosition = currentPosition)
- }
- }
- }
- }
- private fun collectBufferedPosition(){
- viewModelScope.launch {
- exoPlayerListener.bufferedPosition.collect{ bufferedPosition ->
- _videoPlayerState.update {
- _videoPlayerState.value.copy(bufferedPosition = bufferedPosition)
- }
- }
- }
- }
- private fun collectDuration(){
- viewModelScope.launch {
- exoPlayerListener.duration.collect{ duration ->
- _videoPlayerState.update {
- _videoPlayerState.value.copy(duration = duration)
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment