Skip to content

Commit

Permalink
Update App Modification
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahidzbi4213 committed Aug 3, 2024
1 parent 547e226 commit e77a87f
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 62 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion app/src/main/java/com/gulehri/samplereelsapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class MainActivity : ComponentActivity() {
.padding(innerPadding),
videoList = listOf(
"https://vue-3-tiktok.vercel.app/video1.mp4",
"https://vue-3-tiktok.vercel.app/video2.mp4"
"https://vue-3-tiktok.vercel.app/video2.mp4",
"https://i.imgur.com/rzhgpNQ.mp4",
"https://www.instagram.com/reel/C82D-F4vNNv/?igsh=aGwzb3pteG45OGl2",
),
indexOfVideo = 0,

Expand Down
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.3"
activityCompose = "1.9.0"
composeBom = "2024.06.00"
exoplayer = "1.3.1"
exoplayer = "1.4.0"



Expand All @@ -34,9 +34,11 @@ androidx-material3 = { group = "androidx.compose.material3", name = "material3"
media-exoplayer = { group = "androidx.media3", name = "media3-exoplayer", version.ref = "exoplayer" }
media-ui = { group = "androidx.media3", name = "media3-ui", version.ref = "exoplayer" }
media-common = { group = "androidx.media3", name = "media3-common", version.ref = "exoplayer" }
media-hls = { module = "androidx.media3:media3-exoplayer-hls", version.ref = "exoplayer" }


[bundles]
media3Exoplayer = ["media-exoplayer", "media-ui", "media-common"]
media3Exoplayer = ["media-exoplayer", "media-ui", "media-hls", "media-common"]

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.shahid.iqbal.reelsplayer.actions

import androidx.compose.runtime.Stable
import com.shahid.iqbal.reelsplayer.actions.PlayerResizeMode.FILL
import com.shahid.iqbal.reelsplayer.actions.PlayerResizeMode.FIT
import com.shahid.iqbal.reelsplayer.actions.PlayerResizeMode.FIXED_HEIGHT
Expand All @@ -21,7 +20,6 @@ import com.shahid.iqbal.reelsplayer.actions.PlayerResizeMode.ZOOM
* @property FIXED_HEIGHT Maintains a fixed height for the video, adjusting the width to maintain aspect ratio.
*/

@Stable
enum class PlayerResizeMode {
FIT, FILL, ZOOM, FIXED_WIDTH, FIXED_HEIGHT
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.shahid.iqbal.reelsplayer.actions

import androidx.compose.runtime.Stable
import com.shahid.iqbal.reelsplayer.actions.RepeatMode.ALL
import com.shahid.iqbal.reelsplayer.actions.RepeatMode.CURRENT

Expand All @@ -11,7 +10,6 @@ import com.shahid.iqbal.reelsplayer.actions.RepeatMode.CURRENT
* @property ALL Plays all videos in the list sequentially and repeats from the beginning when the end is reached.
*/

@Stable
enum class RepeatMode {
CURRENT,
ALL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.shahid.iqbal.reelsplayer.actions

import androidx.compose.runtime.Stable
import com.shahid.iqbal.reelsplayer.actions.ThumbnailDisplayMode.FILL
import com.shahid.iqbal.reelsplayer.actions.ThumbnailDisplayMode.FIT
import com.shahid.iqbal.reelsplayer.actions.ThumbnailDisplayMode.OFF
Expand All @@ -12,8 +11,6 @@ import com.shahid.iqbal.reelsplayer.actions.ThumbnailDisplayMode.OFF
* @property FIT Thumbnails are scaled to fit within the display area, maintaining aspect ratio.
* @property FILL Thumbnails are scaled to fill the display area, potentially cropping the image.
*/

@Stable
enum class ThumbnailDisplayMode {
OFF, FIT, FILL
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.shahid.iqbal.reelsplayer.actions

import androidx.compose.runtime.Stable
import com.shahid.iqbal.reelsplayer.actions.VideoScalingMode.DEFAULT
import com.shahid.iqbal.reelsplayer.actions.VideoScalingMode.FIT
import com.shahid.iqbal.reelsplayer.actions.VideoScalingMode.FIT_WITH_CROPPING
Expand All @@ -14,7 +13,6 @@ import com.shahid.iqbal.reelsplayer.actions.VideoScalingMode.FIT_WITH_CROPPING
* @property FIT_WITH_CROPPING Scales the video to fill the player's bounds, which may involve cropping parts of the video.
*/

@Stable
enum class VideoScalingMode {
DEFAULT, FIT, FIT_WITH_CROPPING
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.shahid.iqbal.reelsplayer.components

import androidx.annotation.OptIn
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
Expand All @@ -14,13 +11,10 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.LocalLifecycleOwner
Expand All @@ -29,12 +23,11 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import androidx.media3.exoplayer.hls.HlsMediaSource
import com.shahid.iqbal.reelsplayer.configs.ReelsConfig
import com.shahid.iqbal.reelsplayer.configs.ReelsConfigUtils
import com.shahid.iqbal.reelsplayer.configs.ReelsConfigUtils.hideControllersViews
import com.shahid.iqbal.reelsplayer.configs.ReelsConfigUtils.setPlayerAttributes

/*
* Created by Shahid Iqbal on 7/20/2024.
Expand Down Expand Up @@ -62,10 +55,11 @@ fun ReelsPlayer(
contentPadding: PaddingValues = PaddingValues(0.dp),
) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current

val playerViewModel: PlayerViewModel = viewModel()
val playerUiState by playerViewModel.playerUiState.collectAsStateWithLifecycle()

val lifecycleOwner by rememberUpdatedState(newValue = LocalLifecycleOwner.current)
val listOfVideos = remember { videoList }
val index by remember { mutableIntStateOf(indexOfVideo) }
val pageState = rememberPagerState(initialPage = index) { listOfVideos.size }
Expand All @@ -74,6 +68,7 @@ fun ReelsPlayer(
ExoPlayer.Builder(context).build().apply {
videoScalingMode = ReelsConfigUtils.getVideoScalingMode(reelConfig.videoScalingMode)
repeatMode = ReelsConfigUtils.getVideoRepeatMode(reelConfig.repeatMode)
setHandleAudioBecomingNoisy(true)
addListener(object : Player.Listener {
override fun onPlaybackStateChanged(playbackState: Int) {
when (playbackState) {
Expand Down Expand Up @@ -116,7 +111,12 @@ fun ReelsPlayer(

LaunchedEffect(pageState.currentPage) {
with(exoPlayer) {
setMediaItem(MediaItem.fromUri(videoList[pageState.currentPage]))
setMediaSource(
HlsMediaSource.Factory(DefaultHttpDataSource.Factory())
.createMediaSource(
MediaItem.Builder().setUri(videoList[pageState.currentPage]).build()
)
)
prepare()
}
}
Expand All @@ -141,36 +141,16 @@ fun ReelsPlayer(
modifier = modifier,
beyondBoundsPageCount = 0,
pageSpacing = pageSpacing,

contentPadding = contentPadding,
key = { videoList[it] }) { page ->

Box(modifier = Modifier.fillMaxSize()) {

if (page == pageState.currentPage) {
AndroidView({ ctx ->
PlayerView(ctx).apply {
player = exoPlayer
setPlayerAttributes(reelConfig)
hideControllersViews()
}
}, modifier = Modifier.fillMaxSize(),
update = {
exoPlayer.playWhenReady = true
}, onRelease = {
it.player = null
})

if (playerUiState.isLoading) {
reelConfig.playerLoader?.invoke() ?: DefaultVideoLoader(
modifier = Modifier
.align(Alignment.Center)
.size(30.dp)
)
}
}
}

PageContent(
exoPlayer = exoPlayer,
page = page,
pagerState = pageState,
reelConfig = reelConfig,
isPlayerLoading = playerUiState.isLoading
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import com.shahid.iqbal.reelsplayer.actions.VideoScalingMode
@Stable
@Keep
data class ReelsConfig(
var playerResizeMode: PlayerResizeMode = PlayerResizeMode.FILL,
var videoScalingMode: VideoScalingMode = VideoScalingMode.FIT_WITH_CROPPING,
var repeatMode: RepeatMode = RepeatMode.CURRENT,
var thumbnailDisplayMode: ThumbnailDisplayMode = ThumbnailDisplayMode.FILL,
val playerResizeMode: PlayerResizeMode = PlayerResizeMode.FILL,
val videoScalingMode: VideoScalingMode = VideoScalingMode.FIT_WITH_CROPPING,
val repeatMode: RepeatMode = RepeatMode.CURRENT,
val thumbnailDisplayMode: ThumbnailDisplayMode = ThumbnailDisplayMode.FILL,
val showControlsMenu: Boolean = false,
var playerSize: Size = Size(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT),
var reelDetail: (@Composable (Int) -> Unit)? = null,
var playerLoader: (@Composable () -> Unit)? = null,
val playerSize: Size = Size(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT),
val reelDetail: (@Composable (Int) -> Unit)? = null,
val playerLoader: (@Composable () -> Unit)? = null,
)

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shahid.iqbal.reelsplayer.configs;
package com.shahid.iqbal.reelsplayer.configs

import android.view.ViewGroup
import androidx.annotation.OptIn
Expand All @@ -16,8 +16,6 @@ import com.shahid.iqbal.reelsplayer.actions.VideoScalingMode
* Created by Shahid Iqbal on 7/20/2024.
*/



@OptIn(UnstableApi::class)
object ReelsConfigUtils {

Expand Down Expand Up @@ -71,5 +69,4 @@ object ReelsConfigUtils {
artworkDisplayMode = getThumbnailDisplayMode(reelConfig.thumbnailDisplayMode)
}


}

0 comments on commit e77a87f

Please sign in to comment.