Compare commits
10 Commits
947ffc4599
...
756c2ea9f8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
756c2ea9f8 | ||
|
|
c9c3306766 | ||
|
|
49751c55d9 | ||
|
|
d918508c16 | ||
|
|
d858cd18bd | ||
|
|
82f537038c | ||
|
|
a298cb75e2 | ||
|
|
92f0e8543e | ||
|
|
f78bcc83c9 | ||
|
|
55ea2e1ae3 |
@@ -51,6 +51,7 @@ dependencies {
|
||||
|
||||
implementation(libs.hilt.android)
|
||||
implementation(libs.hilt.navigation.compose)
|
||||
implementation(libs.androidx.compose.material.core)
|
||||
ksp(libs.hilt.android.compiler)
|
||||
|
||||
implementation(libs.androidx.room.runtime)
|
||||
@@ -61,6 +62,7 @@ dependencies {
|
||||
implementation(libs.bcprov.jdk15on)
|
||||
implementation(libs.converter.gson)
|
||||
implementation(libs.gson)
|
||||
|
||||
implementation(libs.androidx.media3.exoplayer)
|
||||
implementation(libs.androidx.media3.ui)
|
||||
implementation(libs.androidx.media3.common)
|
||||
|
||||
@@ -28,6 +28,13 @@ class Video(
|
||||
"${ApiClient.getBase()}api/video/$klass/$id/av?token=$token"
|
||||
}
|
||||
|
||||
fun getSubtitle(): String {
|
||||
return if (isLocal)
|
||||
"$localBase/videos/$klass/$id/subtitle.vtt"
|
||||
else
|
||||
"${ApiClient.getBase()}api/video/$klass/$id/subtitle?token=$token"
|
||||
}
|
||||
|
||||
fun getGallery(): List<KeyImage> {
|
||||
return if (isLocal)
|
||||
video.gallery.map {
|
||||
|
||||
@@ -22,6 +22,6 @@ interface VideoRecordDao {
|
||||
@Delete
|
||||
suspend fun delete(rec: VideoRecord)
|
||||
|
||||
@Query("SELECT * FROM videorecord WHERE id = :id")
|
||||
suspend fun getById(id: String): VideoRecord?
|
||||
@Query("SELECT * FROM videorecord WHERE id = :id and name = :klass")
|
||||
suspend fun get(id: String, klass: String): VideoRecord?
|
||||
}
|
||||
@@ -157,6 +157,11 @@ class FetchManager @Inject constructor(
|
||||
video.getCover(),
|
||||
File(context.getExternalFilesDir(null), "videos/${video.klass}/${video.id}/cover.jpg"))
|
||||
|
||||
downloadFile(
|
||||
client!!,
|
||||
video.getSubtitle(),
|
||||
File(context.getExternalFilesDir(null), "videos/${video.klass}/${video.id}/subtitle.vtt"))
|
||||
|
||||
enqueue(request)
|
||||
File(context.getExternalFilesDir(null), "videos/${video.klass}/${video.id}/summary.json").writeText(Json.encodeToString(video))
|
||||
|
||||
|
||||
@@ -130,8 +130,8 @@ fun generateColorScheme(primaryColor: Color, isDarkMode: Boolean): ColorScheme {
|
||||
}
|
||||
}
|
||||
|
||||
private val DarkColorScheme = generateColorScheme(Color(0xFF4A6F9F), isDarkMode = true)
|
||||
private val LightColorScheme = generateColorScheme(Color(0xFF4A6F9F), isDarkMode = false)
|
||||
private val DarkColorScheme = generateColorScheme(Color(0xFF36A0FF), isDarkMode = true)
|
||||
private val LightColorScheme = generateColorScheme(Color(0xFF36A0FF), isDarkMode = false)
|
||||
|
||||
@Composable
|
||||
fun AetherTheme(
|
||||
|
||||
@@ -187,7 +187,7 @@ fun ComicScreen(
|
||||
HorizontalDivider(thickness = 1.5.dp)
|
||||
|
||||
LazyVerticalStaggeredGrid(
|
||||
columns = StaggeredGridCells.Adaptive(128.dp),
|
||||
columns = StaggeredGridCells.Adaptive(136.dp),
|
||||
contentPadding = PaddingValues(8.dp),
|
||||
verticalItemSpacing = 8.dp,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
@@ -277,9 +277,18 @@ fun ComicCard(
|
||||
fontWeight = FontWeight.Bold,
|
||||
maxLines = 2,
|
||||
modifier = Modifier
|
||||
.padding(8.dp)
|
||||
.padding(4.dp)
|
||||
.background(Color.Transparent)
|
||||
.heightIn(max = 48.dp)
|
||||
)
|
||||
Spacer(Modifier.height(4.dp))
|
||||
Text(
|
||||
text = "Id: ${comic.id}",
|
||||
fontSize = 12.sp,
|
||||
maxLines = 2,
|
||||
modifier = Modifier
|
||||
.padding(bottom = 4.dp).padding(horizontal = 4.dp)
|
||||
.background(Color.Transparent)
|
||||
.heightIn(48.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -118,156 +118,168 @@ fun VideoScreen(
|
||||
val tabIndex by videoScreenViewModel.tabIndex
|
||||
var menuVisibility by videoScreenViewModel.menuVisibility
|
||||
var searchFilter by videoScreenViewModel.searchFilter
|
||||
var doneInit by videoScreenViewModel.doneInit
|
||||
|
||||
CardPage(title = "Videos") {
|
||||
Box(Modifier.fillMaxSize())
|
||||
{
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
// TopRow(videoScreenViewModel);
|
||||
Row(Modifier.padding(bottom = 4.dp))
|
||||
{
|
||||
Card(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = colorScheme.primary),
|
||||
modifier = Modifier
|
||||
.align(Alignment.CenterVertically)
|
||||
.padding(horizontal = 2.dp)
|
||||
.size(36.dp),
|
||||
onClick = {
|
||||
menuVisibility = !menuVisibility
|
||||
})
|
||||
if (doneInit)
|
||||
CardPage(title = "Videos") {
|
||||
Box(Modifier.fillMaxSize())
|
||||
{
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
// TopRow(videoScreenViewModel);
|
||||
Row(Modifier.padding(bottom = 4.dp))
|
||||
{
|
||||
Box(Modifier.fillMaxSize())
|
||||
Card(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = colorScheme.primary),
|
||||
modifier = Modifier
|
||||
.align(Alignment.CenterVertically)
|
||||
.padding(horizontal = 2.dp)
|
||||
.size(36.dp),
|
||||
onClick = {
|
||||
menuVisibility = !menuVisibility
|
||||
})
|
||||
{
|
||||
Box(Modifier.fillMaxSize())
|
||||
{
|
||||
Icon(
|
||||
modifier = Modifier
|
||||
.size(30.dp)
|
||||
.align(Alignment.Center),
|
||||
imageVector = Icons.Default.Menu,
|
||||
contentDescription = "Catalogue"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Card(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = colorScheme.primary),
|
||||
modifier = Modifier
|
||||
.align(Alignment.CenterVertically)
|
||||
.padding(horizontal = 2.dp)
|
||||
.height(36.dp),
|
||||
onClick = {
|
||||
menuVisibility = !menuVisibility
|
||||
})
|
||||
{
|
||||
Box(Modifier.fillMaxHeight())
|
||||
{
|
||||
Text(
|
||||
text = videoScreenViewModel.videoLibrary.classes.getOrNull(
|
||||
tabIndex
|
||||
)
|
||||
?: "",
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier
|
||||
.align(Alignment.CenterStart)
|
||||
.padding(horizontal = 8.dp),
|
||||
maxLines = 1
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.height(36.dp)
|
||||
.widthIn(max = 240.dp)
|
||||
.background(colorScheme.primary, RoundedCornerShape(8.dp))
|
||||
.padding(horizontal = 6.dp)
|
||||
) {
|
||||
Icon(
|
||||
modifier = Modifier
|
||||
.size(30.dp)
|
||||
.align(Alignment.Center),
|
||||
imageVector = Icons.Default.Menu,
|
||||
.align(Alignment.CenterVertically),
|
||||
imageVector = Icons.Default.Search,
|
||||
contentDescription = "Catalogue"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Card(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = colorScheme.primary),
|
||||
modifier = Modifier
|
||||
.align(Alignment.CenterVertically)
|
||||
.padding(horizontal = 2.dp)
|
||||
.height(36.dp),
|
||||
onClick = {
|
||||
menuVisibility = !menuVisibility
|
||||
})
|
||||
{
|
||||
Box(Modifier.fillMaxHeight())
|
||||
{
|
||||
Text(
|
||||
text = videoScreenViewModel.videoLibrary.classes.getOrNull(tabIndex)
|
||||
?: "",
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier
|
||||
.align(Alignment.CenterStart)
|
||||
.padding(horizontal = 8.dp),
|
||||
maxLines = 1
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.height(36.dp).widthIn(max = 240.dp)
|
||||
.background(colorScheme.primary, RoundedCornerShape(8.dp))
|
||||
.padding(horizontal = 6.dp)
|
||||
) {
|
||||
Icon(
|
||||
modifier = Modifier
|
||||
.size(30.dp).align(Alignment.CenterVertically),
|
||||
imageVector = Icons.Default.Search,
|
||||
contentDescription = "Catalogue"
|
||||
)
|
||||
Spacer(Modifier.width(4.dp))
|
||||
BasicTextField(
|
||||
value = searchFilter,
|
||||
onValueChange = { searchFilter = it },
|
||||
textStyle = LocalTextStyle.current.copy(
|
||||
fontSize = 18.sp,
|
||||
color = Color.White,
|
||||
textAlign = TextAlign.Start
|
||||
),
|
||||
singleLine = true,
|
||||
modifier = Modifier.align(Alignment.CenterVertically)
|
||||
)
|
||||
}
|
||||
}
|
||||
HorizontalDivider(Modifier.padding(bottom = 8.dp), 1.5.dp, DividerDefaults.color)
|
||||
LazyVerticalStaggeredGrid(
|
||||
columns = StaggeredGridCells.Adaptive(160.dp),
|
||||
contentPadding = PaddingValues(8.dp),
|
||||
verticalItemSpacing = 8.dp,
|
||||
horizontalArrangement = androidx.compose.foundation.layout.Arrangement.spacedBy(8.dp),
|
||||
state = state,
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
items(
|
||||
items = videoScreenViewModel.videoLibrary.classesMap.getOrDefault(
|
||||
videoScreenViewModel.videoLibrary.classes.getOrNull(
|
||||
tabIndex
|
||||
), listOf()
|
||||
).filter { it.video.name.contains(searchFilter) },
|
||||
key = { "${it.klass}/${it.id}" }
|
||||
) { video ->
|
||||
androidx.compose.foundation.layout.Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.wrapContentHeight()
|
||||
) {
|
||||
VideoCard(video, navController, videoScreenViewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AnimatedVisibility(
|
||||
visible = menuVisibility,
|
||||
enter = slideInHorizontally(initialOffsetX = { full -> full }),
|
||||
exit = slideOutHorizontally(targetOffsetX = { full -> full }),
|
||||
modifier = Modifier.align(Alignment.CenterEnd)
|
||||
) {
|
||||
Card(
|
||||
Modifier
|
||||
.fillMaxHeight()
|
||||
.width(200.dp)
|
||||
.align(Alignment.CenterEnd),
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = colorScheme.surface)
|
||||
)
|
||||
{
|
||||
LazyColumn {
|
||||
items(videoScreenViewModel.videoLibrary.classes) { item ->
|
||||
CatalogueItemRow(
|
||||
item = Pair(
|
||||
videoScreenViewModel.videoLibrary.classes.indexOf(item),
|
||||
item
|
||||
Spacer(Modifier.width(4.dp))
|
||||
BasicTextField(
|
||||
value = searchFilter,
|
||||
onValueChange = { searchFilter = it },
|
||||
textStyle = LocalTextStyle.current.copy(
|
||||
fontSize = 18.sp,
|
||||
color = Color.White,
|
||||
textAlign = TextAlign.Start
|
||||
),
|
||||
onItemClick = {
|
||||
menuVisibility = false
|
||||
videoScreenViewModel.setTabIndex(
|
||||
videoScreenViewModel.videoLibrary.classes.indexOf(
|
||||
item
|
||||
)
|
||||
)
|
||||
}
|
||||
singleLine = true,
|
||||
modifier = Modifier.align(Alignment.CenterVertically)
|
||||
)
|
||||
}
|
||||
}
|
||||
HorizontalDivider(
|
||||
Modifier.padding(bottom = 8.dp),
|
||||
1.5.dp,
|
||||
DividerDefaults.color
|
||||
)
|
||||
LazyVerticalStaggeredGrid(
|
||||
columns = StaggeredGridCells.Adaptive(160.dp),
|
||||
contentPadding = PaddingValues(8.dp),
|
||||
verticalItemSpacing = 8.dp,
|
||||
horizontalArrangement = androidx.compose.foundation.layout.Arrangement.spacedBy(
|
||||
8.dp
|
||||
),
|
||||
state = state,
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
items(
|
||||
items = videoScreenViewModel.videoLibrary.classesMap.getOrDefault(
|
||||
videoScreenViewModel.videoLibrary.classes.getOrNull(
|
||||
tabIndex
|
||||
), listOf()
|
||||
).filter { it.video.name.contains(searchFilter) },
|
||||
key = { "${it.klass}/${it.id}" }
|
||||
) { video ->
|
||||
androidx.compose.foundation.layout.Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.wrapContentHeight()
|
||||
) {
|
||||
VideoCard(video, navController, videoScreenViewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AnimatedVisibility(
|
||||
visible = menuVisibility,
|
||||
enter = slideInHorizontally(initialOffsetX = { full -> full }),
|
||||
exit = slideOutHorizontally(targetOffsetX = { full -> full }),
|
||||
modifier = Modifier.align(Alignment.CenterEnd)
|
||||
) {
|
||||
Card(
|
||||
Modifier
|
||||
.fillMaxHeight()
|
||||
.width(250.dp)
|
||||
.align(Alignment.CenterEnd),
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = colorScheme.surface)
|
||||
)
|
||||
{
|
||||
LazyColumn {
|
||||
items(videoScreenViewModel.videoLibrary.classes) { item ->
|
||||
CatalogueItemRow(
|
||||
item = Pair(
|
||||
videoScreenViewModel.videoLibrary.classes.indexOf(item),
|
||||
item
|
||||
),
|
||||
onItemClick = {
|
||||
menuVisibility = false
|
||||
videoScreenViewModel.setTabIndex(
|
||||
videoScreenViewModel.videoLibrary.classes.indexOf(
|
||||
item
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@@ -282,13 +294,13 @@ fun CatalogueItemRow(
|
||||
.padding(4.dp)
|
||||
.padding(horizontal = 4.dp)
|
||||
.heightIn(min = 28.dp)
|
||||
.width(200.dp),
|
||||
.width(250.dp),
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = colorScheme.primary)
|
||||
) {
|
||||
Text(
|
||||
text = item.second,
|
||||
fontSize = 18.sp,
|
||||
fontSize = 14.sp,
|
||||
maxLines = 1,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier
|
||||
@@ -298,27 +310,6 @@ fun CatalogueItemRow(
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun TopRow(videoScreenViewModel: VideoScreenViewModel) {
|
||||
val tabIndex by videoScreenViewModel.tabIndex;
|
||||
if (videoScreenViewModel.videoLibrary.classes.isEmpty()) return
|
||||
val colorScheme = MaterialTheme.colorScheme
|
||||
|
||||
ScrollableTabRow(
|
||||
selectedTabIndex = tabIndex,
|
||||
modifier = Modifier.background(colorScheme.surface)
|
||||
) {
|
||||
videoScreenViewModel.videoLibrary.classes.forEachIndexed { index, title ->
|
||||
Tab(
|
||||
selected = tabIndex == index,
|
||||
onClick = { videoScreenViewModel.setTabIndex(index) },
|
||||
text = { Text(text = title, maxLines = 1) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun VideoCard(
|
||||
video: Video,
|
||||
@@ -354,7 +345,7 @@ fun VideoCard(
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.fillMaxWidth(),
|
||||
) {
|
||||
Box(modifier = Modifier.fillMaxSize()) {
|
||||
|
||||
@@ -415,21 +406,30 @@ fun VideoCard(
|
||||
}
|
||||
Text(
|
||||
text = video.video.name,
|
||||
fontSize = 14.sp,
|
||||
fontSize = 12.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
maxLines = 2,
|
||||
maxLines = 4,
|
||||
modifier = Modifier
|
||||
.padding(8.dp)
|
||||
.background(Color.Transparent)
|
||||
.heightIn(24.dp)
|
||||
.heightIn(min = 24.dp),
|
||||
lineHeight = 14.sp
|
||||
)
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
Row(
|
||||
modifier = Modifier.padding(horizontal = 8.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text("Class: ", fontSize = 12.sp, maxLines = 1)
|
||||
Text(video.klass, fontSize = 12.sp, maxLines = 1)
|
||||
Text("Class: ", fontSize = 10.sp, maxLines = 1)
|
||||
Text(video.klass, fontSize = 10.sp, maxLines = 1)
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier.padding(horizontal = 8.dp).padding(bottom = 6.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text("Id: ", fontSize = 10.sp, maxLines = 1, lineHeight = 10.sp)
|
||||
Text(video.id, fontSize = 10.sp, maxLines = 1, lineHeight = 10.sp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.PlaybackException
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.common.Player.STATE_READY
|
||||
import androidx.media3.common.text.Cue
|
||||
import androidx.media3.common.util.Log
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.datasource.okhttp.OkHttpDataSource
|
||||
@@ -40,8 +41,14 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.Request
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
import androidx.core.net.toUri
|
||||
import androidx.media3.common.C
|
||||
import androidx.media3.common.Tracks
|
||||
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
|
||||
|
||||
@HiltViewModel
|
||||
class VideoPlayerViewModel @Inject constructor(
|
||||
@@ -64,7 +71,7 @@ class VideoPlayerViewModel @Inject constructor(
|
||||
var thumbUp by mutableIntStateOf(0)
|
||||
var thumbDown by mutableIntStateOf(0)
|
||||
var star by mutableStateOf(false)
|
||||
|
||||
var locked by mutableStateOf(false)
|
||||
private var _init: Boolean = false;
|
||||
var startPlaying by mutableStateOf(false)
|
||||
var renderedFirst = false
|
||||
@@ -74,6 +81,7 @@ class VideoPlayerViewModel @Inject constructor(
|
||||
var imageLoader: ImageLoader? = null;
|
||||
var brit by mutableFloatStateOf(0.5f)
|
||||
val database: VideoRecordDatabase = VideoRecordDatabase.getDatabase(context)
|
||||
var cues by mutableStateOf(listOf<Cue>())
|
||||
|
||||
@OptIn(UnstableApi::class)
|
||||
fun init(videoId: String) {
|
||||
@@ -88,53 +96,154 @@ class VideoPlayerViewModel @Inject constructor(
|
||||
viewModelScope.launch {
|
||||
video = mediaManager.queryVideo(v.split("/")[0], v.split("/")[1])!!
|
||||
recentManager.pushVideo(context, VideoQueryIndex(v.split("/")[0], v.split("/")[1]))
|
||||
_player =
|
||||
(if (video!!.isLocal) ExoPlayer.Builder(context) else ExoPlayer.Builder(context)
|
||||
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory)))
|
||||
.build().apply {
|
||||
val url = video?.getVideo() ?: ""
|
||||
val mediaItem = if (video!!.isLocal)
|
||||
MediaItem.fromUri(Uri.fromFile(File(url)))
|
||||
else
|
||||
MediaItem.fromUri(url)
|
||||
|
||||
setMediaItem(mediaItem)
|
||||
prepare()
|
||||
playWhenReady = true
|
||||
val subtitleCandidate = video?.getSubtitle()?.trim()
|
||||
val subtitleUri = tryResolveSubtitleUri(subtitleCandidate)
|
||||
|
||||
addListener(object : Player.Listener {
|
||||
override fun onPlaybackStateChanged(playbackState: Int) {
|
||||
if (playbackState == STATE_READY) {
|
||||
startPlaying = true
|
||||
}
|
||||
}
|
||||
// decide whether we need network-capable media source factory:
|
||||
val subtitleIsRemote = subtitleUri?.scheme?.startsWith("http", ignoreCase = true) == true
|
||||
val videoIsRemote = !video!!.isLocal
|
||||
val needNetworkFactory = videoIsRemote || subtitleIsRemote
|
||||
val trackSelector = DefaultTrackSelector(context)
|
||||
|
||||
override fun onRenderedFirstFrame() {
|
||||
super.onRenderedFirstFrame()
|
||||
if(!renderedFirst)
|
||||
{
|
||||
viewModelScope.launch {
|
||||
val ii = database.userDao().getById(video!!.id)
|
||||
if(ii != null)
|
||||
{
|
||||
_player!!.seekTo(ii.position)
|
||||
Toast.makeText(context, "Recover from ${formatTime(ii.position)} ", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
renderedFirst = true
|
||||
}
|
||||
// build ExoPlayer with or without custom DefaultMediaSourceFactory
|
||||
val builder = if (needNetworkFactory)
|
||||
ExoPlayer.Builder(context).setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory))
|
||||
else
|
||||
ExoPlayer.Builder(context)
|
||||
|
||||
override fun onPlayerError(error: PlaybackException) {
|
||||
_player = builder.setTrackSelector(trackSelector).build().apply {
|
||||
val url = video?.getVideo() ?: ""
|
||||
val videoUri = if (video!!.isLocal) Uri.fromFile(File(url)) else url.toUri()
|
||||
|
||||
}
|
||||
})
|
||||
val mediaItem: MediaItem = if (subtitleUri != null) {
|
||||
// prepare subtitle configuration with guessed mime type
|
||||
val subConfig = MediaItem.SubtitleConfiguration.Builder(subtitleUri)
|
||||
.setMimeType("text/vtt")
|
||||
.build()
|
||||
|
||||
MediaItem.Builder()
|
||||
.setUri(videoUri)
|
||||
.setSubtitleConfigurations(listOf(subConfig))
|
||||
.build()
|
||||
} else {
|
||||
MediaItem.fromUri(videoUri)
|
||||
}
|
||||
|
||||
setMediaItem(mediaItem)
|
||||
prepare()
|
||||
playWhenReady = true
|
||||
|
||||
addListener(object : Player.Listener {
|
||||
override fun onTracksChanged(tracks: Tracks) {
|
||||
super.onTracksChanged(tracks)
|
||||
|
||||
val trackSelector = _player?.trackSelector
|
||||
if (trackSelector is DefaultTrackSelector) {
|
||||
val parameters = trackSelector.buildUponParameters()
|
||||
.setSelectUndeterminedTextLanguage(true)
|
||||
.build()
|
||||
trackSelector.parameters = parameters
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPlaybackStateChanged(playbackState: Int) {
|
||||
if (playbackState == STATE_READY) {
|
||||
startPlaying = true
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRenderedFirstFrame() {
|
||||
super.onRenderedFirstFrame()
|
||||
if(!renderedFirst)
|
||||
{
|
||||
viewModelScope.launch {
|
||||
val ii = database.userDao().get(video!!.id, video!!.klass)
|
||||
if(ii != null)
|
||||
{
|
||||
_player!!.seekTo(ii.position)
|
||||
Toast.makeText(context, "Recover from ${formatTime(ii.position)} ", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
renderedFirst = true
|
||||
}
|
||||
|
||||
override fun onPlayerError(error: PlaybackException)
|
||||
{
|
||||
print(error.message)
|
||||
}
|
||||
|
||||
override fun onCues(lcues: MutableList<Cue>) {
|
||||
cues = lcues
|
||||
}
|
||||
})
|
||||
}
|
||||
startListen()
|
||||
}
|
||||
_init = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to resolve the given subtitle pathOrUrl to a Uri.
|
||||
* - If it's a local path and file exists -> Uri.fromFile
|
||||
* - If it's a http(s) URL -> try HEAD; if HEAD unsupported, try GET with Range: bytes=0-1
|
||||
* - Return null when unreachable / 404 / not exist
|
||||
*/
|
||||
private suspend fun tryResolveSubtitleUri(pathOrUrl: String?): Uri? = withContext(Dispatchers.IO) {
|
||||
if (pathOrUrl.isNullOrBlank()) return@withContext null
|
||||
val trimmed = pathOrUrl.trim()
|
||||
|
||||
// Remote URL case (http/https)
|
||||
if (trimmed.startsWith("http://", ignoreCase = true) || trimmed.startsWith("https://", ignoreCase = true)) {
|
||||
try {
|
||||
val client = createOkHttp()
|
||||
|
||||
var headReq = Request.Builder().url(trimmed).head().build()
|
||||
var headResp = try { client.newCall(headReq).execute() } catch (e: Exception) { null }
|
||||
|
||||
headResp?.use { resp ->
|
||||
val code = resp.code
|
||||
if (code == 200 || code == 206) {
|
||||
return@withContext trimmed.toUri()
|
||||
}
|
||||
if (code == 404) {
|
||||
return@withContext null
|
||||
}
|
||||
}
|
||||
val rangeReq = Request.Builder()
|
||||
.url(trimmed)
|
||||
.addHeader("Range", "bytes=0-1")
|
||||
.get()
|
||||
.build()
|
||||
|
||||
var rangeResp = try { client.newCall(rangeReq).execute() } catch (e: Exception) { null }
|
||||
|
||||
rangeResp?.use { resp ->
|
||||
val code = resp.code
|
||||
if (code == 206) {
|
||||
return@withContext trimmed.toUri()
|
||||
}
|
||||
|
||||
if (code == 200) {
|
||||
return@withContext trimmed.toUri()
|
||||
}
|
||||
|
||||
if (code == 404) {
|
||||
return@withContext null
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
return@withContext null
|
||||
}
|
||||
return@withContext null
|
||||
} else {
|
||||
// Local path
|
||||
val f = File(trimmed)
|
||||
return@withContext if (f.exists() && f.isFile) Uri.fromFile(f) else null
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(UnstableApi::class)
|
||||
fun startListen() {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
@@ -156,4 +265,4 @@ class VideoPlayerViewModel @Inject constructor(
|
||||
database.userDao().insert(VideoRecord(video!!.id, video!!.klass, p))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,12 +47,16 @@ class VideoScreenViewModel @Inject constructor(
|
||||
var imageLoader: ImageLoader? = null;
|
||||
var menuVisibility = mutableStateOf(false)
|
||||
var searchFilter = mutableStateOf("")
|
||||
var doneInit = mutableStateOf(false)
|
||||
|
||||
suspend fun init() {
|
||||
fetchManager.configured.filter { it }.first()
|
||||
|
||||
if (Global.loggedIn) {
|
||||
videoLibrary.classes.addAll(mediaManager.listVideoKlasses())
|
||||
if(videoLibrary.classes.isEmpty())
|
||||
return
|
||||
|
||||
var i = 0
|
||||
for (it in videoLibrary.classes) {
|
||||
videoLibrary.updatingMap[i++] = false
|
||||
@@ -66,7 +70,8 @@ class VideoScreenViewModel @Inject constructor(
|
||||
val r = vl.sortedWith(compareBy(naturalOrder()) { it.video.name })
|
||||
videoLibrary.classesMap[videoLibrary.classes[0]]?.addAll(r)
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
videoLibrary.classes.add("Offline")
|
||||
videoLibrary.updatingMap[0] = true
|
||||
videoLibrary.classesMap["Offline"] = mutableStateListOf<Video>()
|
||||
@@ -82,6 +87,8 @@ class VideoScreenViewModel @Inject constructor(
|
||||
|
||||
videoLibrary.classesMap[videoLibrary.classes[0]]?.addAll(jsonQuery)
|
||||
}
|
||||
|
||||
doneInit.value = true
|
||||
}
|
||||
|
||||
fun setTabIndex(index: Int) {
|
||||
|
||||
@@ -22,6 +22,7 @@ activityCompose = "1.11.0"
|
||||
composeBom = "2025.09.00"
|
||||
media3Common = "1.8.0"
|
||||
media3Exoplayer = "1.8.0"
|
||||
media3ExoplayerFfmpeg = "1.8.0"
|
||||
media3Ui = "1.8.0"
|
||||
navigationCompose = "2.9.4"
|
||||
okhttp = "5.1.0"
|
||||
@@ -35,6 +36,7 @@ roomRuntime = "2.8.0"
|
||||
ksp = "2.1.21-2.0.2"
|
||||
hilt = "2.57.1"
|
||||
hilt-navigation-compose = "1.3.0"
|
||||
composeMaterialCore = "1.5.1"
|
||||
|
||||
[libraries]
|
||||
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
||||
@@ -77,6 +79,7 @@ androidx-media3-datasource-okhttp = { group = "androidx.media3", name = "media3-
|
||||
hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
|
||||
hilt-android-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hilt" }
|
||||
hilt-navigation-compose = { group = "androidx.hilt", name = "hilt-navigation-compose", version.ref = "hilt-navigation-compose" }
|
||||
androidx-compose-material-core = { group = "androidx.wear.compose", name = "compose-material-core", version.ref = "composeMaterialCore" }
|
||||
|
||||
[plugins]
|
||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||
|
||||
Reference in New Issue
Block a user