Skip to content

Commit

Permalink
VideoPlayer 영상 준비 에러에 따른 Message 표시
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Aug 23, 2023
1 parent 67f42c2 commit f0dbfbb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.jik.lib.videoplayer

enum class VideoPlayerState {
sealed interface VideoPlayerState {

INITIAL,
LOADING,
GET_ERROR,
NO_VIDEO,
CAN_PLAY,
object Initial : VideoPlayerState
object Loading : VideoPlayerState
class GetError(val errorMessage: String) : VideoPlayerState
object NoVideo : VideoPlayerState
object CanPlay : VideoPlayerState
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jik.lib.videoplayer.component.error
package com.jik.lib.videoplayer.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
Expand All @@ -15,9 +15,10 @@ import androidx.compose.ui.graphics.Color
import com.jik.lib.videoplayer.component.VideoPlayerIcons.Refresh

@Composable
fun NetworkError(
fun ErrorScreen(
modifier: Modifier = Modifier,
onRefreshClick: () -> Unit
errorMessage: String,
onRefreshClick: (() -> Unit)? = null,
) {
Column(
modifier = modifier
Expand All @@ -27,20 +28,22 @@ fun NetworkError(
horizontalAlignment = Alignment.CenterHorizontally,
) {
Text(
text = "Please check network connection.",
text = errorMessage,
style = MaterialTheme.typography.bodySmall,
color = Color.White,
)
IconButton(
onClick = {
onRefreshClick()
},
) {
Icon(
imageVector = Refresh,
contentDescription = "Refresh",
tint = Color.White,
)
if (onRefreshClick != null) {
IconButton(
onClick = {
onRefreshClick()
},
) {
Icon(
imageVector = Refresh,
contentDescription = "Refresh",
tint = Color.White,
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import androidx.media3.exoplayer.ExoPlayer
import com.jik.lib.videoplayer.VideoPlayerState
import com.jik.lib.videoplayer.VideoPlayerUtil
import com.jik.lib.videoplayer.VideoPlayerUtil.toStreamUrlOfYouTube
import com.jik.lib.videoplayer.component.error.NetworkError
import com.jik.lib.videoplayer.component.error.NoVideoFound
import com.jik.lib.videoplayer.component.thumnail.ThumbnailLoadingWheel
import com.jik.lib.videoplayer.component.thumnail.ThumbnailPlayIcon
import kotlinx.coroutines.launch
Expand All @@ -28,14 +26,14 @@ fun VideoPlayer(
) {
val context = LocalContext.current

var videoPlayerState by remember { mutableStateOf(VideoPlayerState.INITIAL) }
var videoPlayerState: VideoPlayerState by remember { mutableStateOf(VideoPlayerState.Initial) }
val coroutineScope = rememberCoroutineScope()
var player: ExoPlayer? by remember { mutableStateOf(null) }
val renderListener = VideoPlayerUtil.renderListener { player!!.play() }

fun initializePlayer() {
if (videoUrl == null) {
videoPlayerState = VideoPlayerState.NO_VIDEO
videoPlayerState = VideoPlayerState.NoVideo
return
}
coroutineScope.launch {
Expand All @@ -46,7 +44,7 @@ fun VideoPlayer(
prepare()
}
} catch (e: Exception) {
videoPlayerState = VideoPlayerState.GET_ERROR
videoPlayerState = VideoPlayerState.GetError(e.message ?: "Unknown Error")
}
}
}
Expand Down Expand Up @@ -86,32 +84,33 @@ fun VideoPlayer(
contentAlignment = Alignment.Center
) {
when (videoPlayerState) {
VideoPlayerState.INITIAL -> {
is VideoPlayerState.Initial -> {
Thumbnail()
ThumbnailPlayIcon {
videoPlayerState = VideoPlayerState.LOADING
videoPlayerState = VideoPlayerState.Loading
}
}
VideoPlayerState.LOADING -> {
is VideoPlayerState.Loading -> {
Thumbnail()
ThumbnailLoadingWheel()
if (player != null) {
videoPlayerState = VideoPlayerState.CAN_PLAY
videoPlayerState = VideoPlayerState.CanPlay
}
}
VideoPlayerState.CAN_PLAY -> {
is VideoPlayerState.CanPlay -> {
VideoPlayerScreen(player = player ?: return)
}
VideoPlayerState.GET_ERROR -> {
NetworkError(
is VideoPlayerState.GetError -> {
ErrorScreen(
errorMessage = (videoPlayerState as VideoPlayerState.GetError).errorMessage,
onRefreshClick = {
initializePlayer()
videoPlayerState = VideoPlayerState.LOADING
videoPlayerState = VideoPlayerState.Loading
}
)
}
VideoPlayerState.NO_VIDEO -> {
NoVideoFound()
is VideoPlayerState.NoVideo -> {
ErrorScreen(errorMessage = "No Video Found")
}
}
}
Expand Down

0 comments on commit f0dbfbb

Please sign in to comment.