@@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.fillMaxSize
99import androidx.compose.foundation.layout.size
1010import androidx.compose.foundation.pager.PagerState
1111import androidx.compose.runtime.Composable
12+ import androidx.compose.runtime.DisposableEffect
1213import androidx.compose.runtime.LaunchedEffect
1314import androidx.compose.runtime.derivedStateOf
1415import androidx.compose.runtime.getValue
@@ -65,44 +66,45 @@ fun MediaVideo(
6566 val scope = rememberCoroutineScope()
6667 val viewerAlpha = remember { Animatable (0F ) }
6768 val context = LocalContext .current
69+ val appContext = remember(context) { context.applicationContext }
6870
69- // 容器大小
71+ // Container size
7072 var bSize by remember { mutableStateOf(IntSize (0 , 0 )) }
71- // 容器比例
73+ // Container aspect ratio
7274 val bRatio by remember { derivedStateOf { bSize.width.toFloat() / bSize.height.toFloat() } }
73- // 视频原始大小
75+ // Video intrinsic size
7476 var vSize by remember { mutableStateOf(IntSize (0 , 0 )) }
75- // 视频原始比例
77+ // Video intrinsic aspect ratio
7678 val vRatio by remember { derivedStateOf { if (vSize.height == 0 ) 1f else vSize.width.toFloat() / vSize.height.toFloat() } }
77- // 是否宽度与容器大小一致
79+ // Whether width matches the container width
7880 var widthFixed by remember { mutableStateOf(false ) }
79- // 长宽是否均超出容器长宽
81+ // Whether both width and height exceed container bounds
8082 val superSize by remember {
8183 derivedStateOf {
8284 vSize.height > bSize.height && vSize.width > bSize.width
8385 }
8486 }
85- // 显示大小
87+ // Display size
8688 val uSize by remember {
8789 derivedStateOf {
8890 if (vSize == IntSize .Zero || bSize == IntSize .Zero ) {
8991 bSize
9092 } else if (vRatio > bRatio) {
91- // 宽度一致
93+ // Match container width
9294 val uW = bSize.width
9395 val uH = uW / vRatio
9496 widthFixed = true
9597 IntSize (uW, uH.toInt())
9698 } else {
97- // 高度一致
99+ // Match container height
98100 val uH = bSize.height
99101 val uW = uH * vRatio
100102 widthFixed = false
101103 IntSize (uW.toInt(), uH)
102104 }
103105 }
104106 }
105- // 视频显示的真实大小
107+ // Actual rendered video size
106108 val rSize by remember {
107109 derivedStateOf {
108110 IntSize (
@@ -135,19 +137,19 @@ fun MediaVideo(
135137 }
136138 }
137139
138- // 视频是否加载成功
140+ // Whether the video is successfully specified/loaded
139141 var videoSpecified by remember { mutableStateOf(false ) }
140142
141- // 初始化视频尺寸
143+ // Initialize video dimensions
142144 LaunchedEffect (model.path) {
143145 if (model.intrinsicSize == IntSize .Zero ) {
144- // 尝试从不同的数据源获取视频尺寸
146+ // Try to obtain video dimensions from various sources
145147 when (val data = model.data) {
146148 is com.ismartcoding.plain.data.DVideo -> {
147149 model.initAsync(data)
148150 }
149151 else -> {
150- // 如果没有data,直接使用VideoHelper获取尺寸
152+ // If there is no data, use VideoHelper to get the size directly
151153 val size = com.ismartcoding.plain.helpers.VideoHelper .getIntrinsicSize(model.path)
152154 if (size != IntSize .Zero ) {
153155 model.intrinsicSize = size
@@ -198,7 +200,7 @@ fun MediaVideo(
198200 }
199201 videoState.initData(player)
200202 mediaSession?.release()
201- mediaSession = MediaSession .Builder (context , ForwardingPlayer (player))
203+ mediaSession = MediaSession .Builder (appContext , ForwardingPlayer (player))
202204 .setId(" VideoPlayerMediaSession_${UUID .randomUUID().toString().lowercase().split(" -" ).first()} " )
203205 .build()
204206 val exoPlayerMediaItems = listOf (
@@ -220,6 +222,13 @@ fun MediaVideo(
220222 player.play()
221223 }
222224
225+ DisposableEffect (Unit ) {
226+ onDispose {
227+ mediaSession?.release()
228+ mediaSession = null
229+ }
230+ }
231+
223232 Box (
224233 modifier = modifier
225234 .fillMaxSize()
0 commit comments