Hytod

ExoPlayer ViewModel

Jul 16th, 2025 (edited)
117
0
2 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.63 KB | None | 0 0
  1. this is the code for my ViewModel, i'm using Koin Di:
  2.  
  3. class VideoPlayerVM(
  4.    private val savedStateHandle: SavedStateHandle,
  5.    val player:Player,
  6.    private val mediaScanner: MediaScanner,
  7.    private val exoPlayerListener: ExoPlayerListener
  8. ): ViewModel() {
  9.  
  10.    private val videoUrisList = savedStateHandle.getStateFlow(Constants.VIDEO_URIS_SAVED_KEY,
  11.        emptyList<Uri>()
  12.    )
  13.  
  14.    private val videoIndex = savedStateHandle.getStateFlow(Constants.VIDEO_INDEX_SAVED_KEY,0)
  15.  
  16.    private val _videoPlayerState = MutableStateFlow(VideoPlayerState())
  17.    val videoPlayerState = _videoPlayerState.asStateFlow()
  18.  
  19.  
  20.  
  21.    init {
  22.        player.prepare()
  23.        collectIsPlaying()
  24.        collectIsLoading()
  25.        collectDuration()
  26.        collectCurrentPosition()
  27.        collectBufferedPosition()
  28.    }
  29.  
  30.    fun onAction(action: VideoPlayerAction){
  31.        when(action){
  32.            is VideoPlayerAction.GetVideosUri -> {
  33.                player.clearMediaItems()
  34.                val mediaUris = mediaScanner.getVideosInFolder(action.folder).map {
  35.                    it.videoUri
  36.                }
  37.  
  38.                savedStateHandle[Constants.VIDEO_URIS_SAVED_KEY] = videoUrisList.value + mediaUris
  39.                savedStateHandle[Constants.VIDEO_INDEX_SAVED_KEY] = action.videoIndex
  40.  
  41.                mediaUris.forEach {
  42.                    player.addMediaItem(MediaItem.fromUri(it!!))
  43.                }
  44.  
  45.                Log.d(Constants.PLAYER_RESULTS_LOG, "Get Videos Uri Is Called and the Video Number is: ${videoIndex.value} ")
  46.  
  47.                setStateMediaItemsList(mediaUris)
  48.                val currentVideo = _videoPlayerState.value.videoPlayerMediaItemList[videoIndex.value].mediaItem
  49.                player.setMediaItem(currentVideo)
  50.                player.playWhenReady = true
  51.  
  52.            }
  53.            is VideoPlayerAction.OnFastForward -> {
  54.                Log.d(Constants.PLAYER_RESULTS_LOG, "onFastForward is Called ")
  55.                player.seekTo(player.currentPosition + 10000)
  56.            }
  57.            is VideoPlayerAction.OnRewind -> {
  58.                Log.d(Constants.PLAYER_RESULTS_LOG, " Rewind is Called ")
  59.                player.seekTo(player.currentPosition - 10000)
  60.            }
  61.            is VideoPlayerAction.OnSeek -> {
  62.                player.seekTo(action.position)
  63.            }
  64.        }
  65.    }
  66.  
  67.    private fun setStateMediaItemsList(mediaUris: List<Uri?>) {
  68.        val videoItemList: MutableList<VideoPlayerMediaItem> = mutableListOf()
  69.        mediaUris.forEach { uri ->
  70.            if (uri != null){
  71.                val videoItem = VideoPlayerMediaItem(
  72.                    name = "No Name",
  73.                    contentUri = uri,
  74.                    mediaItem = MediaItem.fromUri(uri)
  75.                )
  76.                videoItemList.add(videoItem)
  77.            }
  78.        }
  79.        _videoPlayerState.update {
  80.            _videoPlayerState.value.copy(videoPlayerMediaItemList = videoItemList)
  81.        }
  82.    }
  83.  
  84.  
  85.    override fun onCleared() {
  86.        exoPlayerListener.release()
  87.        super.onCleared()
  88.        player.release()
  89.    }
  90.  
  91.    private fun collectIsPlaying(){
  92.        viewModelScope.launch {
  93.            exoPlayerListener.isPlaying.collect{ isPlaying ->
  94.                _videoPlayerState.update {
  95.                    _videoPlayerState.value.copy(isPlaying = isPlaying)
  96.                }
  97.            }
  98.        }
  99.    }
  100.    private fun collectIsLoading(){
  101.        viewModelScope.launch {
  102.            exoPlayerListener.isLoading.collect{ isLoading ->
  103.                _videoPlayerState.update {
  104.                    _videoPlayerState.value.copy(isLoading = isLoading)
  105.                }
  106.            }
  107.        }
  108.    }
  109.    private fun collectCurrentPosition(){
  110.        viewModelScope.launch {
  111.            exoPlayerListener.currentPosition.collect{ currentPosition ->
  112.                _videoPlayerState.update {
  113.                    _videoPlayerState.value.copy(currentPosition = currentPosition)
  114.                }
  115.            }
  116.        }
  117.    }
  118.    private fun collectBufferedPosition(){
  119.        viewModelScope.launch {
  120.            exoPlayerListener.bufferedPosition.collect{ bufferedPosition ->
  121.                _videoPlayerState.update {
  122.                    _videoPlayerState.value.copy(bufferedPosition = bufferedPosition)
  123.                }
  124.            }
  125.        }
  126.    }
  127.  
  128.    private fun collectDuration(){
  129.        viewModelScope.launch {
  130.            exoPlayerListener.duration.collect{ duration ->
  131.                _videoPlayerState.update {
  132.                    _videoPlayerState.value.copy(duration = duration)
  133.                }
  134.            }
  135.        }
  136.    }
  137.  
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment