Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lsongdev committed Aug 22, 2024
1 parent 0886713 commit a7bd840
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 209 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

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

17 changes: 4 additions & 13 deletions app/src/main/java/me/lsong/mytv/ui/MainContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.lsong.mytv.ui
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -38,19 +39,7 @@ fun LeanbackMainContent(
groupList: TVGroupList = TVGroupList(),
settingsViewModel: MyTvSettingsViewModel = viewModel(),
) {
val configuration = LocalConfiguration.current
val videoPlayerState = rememberLeanbackVideoPlayerState(
defaultAspectRatioProvider = {
when (settingsViewModel.videoPlayerAspectRatio) {
Settings.VideoPlayerAspectRatio.ORIGINAL -> null
Settings.VideoPlayerAspectRatio.SIXTEEN_NINE -> 16f / 9f
Settings.VideoPlayerAspectRatio.FOUR_THREE -> 4f / 3f
Settings.VideoPlayerAspectRatio.AUTO -> {
configuration.screenHeightDp.toFloat() / configuration.screenWidthDp.toFloat()
}
}
}
)
val videoPlayerState = rememberLeanbackVideoPlayerState()
val mainContentState = rememberMainContentState(
videoPlayerState = videoPlayerState,
tvGroupList = groupList,
Expand Down Expand Up @@ -84,8 +73,10 @@ fun LeanbackMainContent(
) {
MyTvVideoScreen(
state = videoPlayerState,
aspectRatioProvider = { settingsViewModel.videoPlayerAspectRatio },
showMetadataProvider = { settingsViewModel.debugShowVideoPlayerMetadata },
modifier = Modifier
.fillMaxSize()
.focusRequester(focusRequester)
.focusable()
.handleLeanbackKeyEvents(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class LeanbackMedia3VideoPlayer(
})

val mediaItem = MediaItem.fromUri(uri)

val mediaSource = when (val type = contentType ?: Util.inferContentType(uri)) {
C.CONTENT_TYPE_HLS -> {
HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem)
Expand Down
29 changes: 19 additions & 10 deletions app/src/main/java/me/lsong/mytv/ui/player/VideoScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,50 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import me.lsong.mytv.rememberLeanbackChildPadding
import me.lsong.mytv.ui.components.LeanbackVideoPlayerMetadata
import me.lsong.mytv.utils.Settings

@Composable
fun MyTvVideoScreen(
modifier: Modifier = Modifier,
state: LeanbackVideoPlayerState = rememberLeanbackVideoPlayerState(),
aspectRatioProvider: () -> Settings.VideoPlayerAspectRatio,
showMetadataProvider: () -> Boolean = { false },
) {
val context = LocalContext.current
val childPadding = rememberLeanbackChildPadding()

Box(modifier = modifier.fillMaxSize()) {
AndroidView(
modifier = Modifier
.align(Alignment.Center)
.aspectRatio(state.aspectRatio),
factory = {
// PlayerView 切换视频时黑屏闪烁,使用 SurfaceView 代替
SurfaceView(context)
},
update = { surfaceView ->
state.setVideoSurfaceView(surfaceView)
},
modifier = when (aspectRatioProvider()) {
Settings.VideoPlayerAspectRatio.ORIGINAL -> Modifier
Settings.VideoPlayerAspectRatio.ASPECT_16_9 -> Modifier.aspectRatio(16f / 9f)
Settings.VideoPlayerAspectRatio.ASPECT_4_3 -> Modifier.aspectRatio( 4f / 3f)
Settings.VideoPlayerAspectRatio.FULL_SCREEN -> {
val configuration = LocalConfiguration.current
Modifier.aspectRatio(configuration.screenWidthDp.toFloat() / configuration.screenHeightDp.toFloat())
}

}.fillMaxSize().align(Alignment.Center),
factory = { SurfaceView(context) },
update = { surfaceView -> state.setVideoSurfaceView(surfaceView) },
)

LeanbackVideoPlayerErrorScreen(
errorProvider = { state.error },
)

// Text(text = "$aspectRatio")

if (showMetadataProvider()) {
LeanbackVideoPlayerMetadata(
modifier = Modifier.padding(start = childPadding.start, top = childPadding.top),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.content.Context
import android.content.pm.PackageInfo
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Switch
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
Expand All @@ -13,9 +15,11 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.tv.foundation.lazy.list.TvLazyColumn
import me.lsong.mytv.ui.components.LeanbackQrcode
import me.lsong.mytv.ui.settings.MyTvSettingsViewModel
import me.lsong.mytv.ui.theme.LeanbackTheme
import me.lsong.mytv.utils.Constants
import me.lsong.mytv.utils.HttpServer

@Composable
fun LeanbackSettingsCategoryAbout(
Expand All @@ -39,19 +43,6 @@ fun LeanbackSettingsCategoryAbout(
)
}

item {
LeanbackSettingsCategoryListItem(
headlineContent = "显示FPS",
supportingContent = "在屏幕左上角显示fps和柱状图",
trailingContent = {
Switch(checked = settingsViewModel.debugShowFps, onCheckedChange = null)
},
onSelected = {
settingsViewModel.debugShowFps = !settingsViewModel.debugShowFps
},
)
}

item {
LeanbackSettingsCategoryListItem(
headlineContent = "显示播放器信息",
Expand All @@ -68,6 +59,33 @@ fun LeanbackSettingsCategoryAbout(
},
)
}
item {
LeanbackSettingsCategoryListItem(
headlineContent = "显示FPS",
supportingContent = "在屏幕左上角显示fps和柱状图",
trailingContent = {
Switch(checked = settingsViewModel.debugShowFps, onCheckedChange = null)
},
onSelected = {
settingsViewModel.debugShowFps = !settingsViewModel.debugShowFps
},
)
}

item{
LeanbackSettingsCategoryListItem(
headlineContent = "扫码进入设置页面",
supportingContent = HttpServer.serverUrl,
trailingContent = { LeanbackQrcode(
text = HttpServer.serverUrl,
modifier = Modifier
.width(80.dp)
.height(80.dp),
)
}
)

}
}
}
}
Expand Down
Loading

0 comments on commit a7bd840

Please sign in to comment.