8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

在选项卡之间切换或多次点击自定义控制器时,ExoPlayer(Media3)视频会变得混乱或混乱

Alexi Maschas 2月前

25 0

我正在使用 Jetpack Compose 构建一个采用模块化和 Clean 架构的 Android 应用。有时使用 ExoPlayer 播放视频。我的屏幕顶部有 ExoPlayer,播放器下方有...

我正在使用 Jetpack Compose 构建采用模块化和 Clean 架构的 Android 应用。有时使用 ExoPlayer 播放视频。

我的屏幕顶部有 ExoPlayer,播放器下方有一个带有选项卡的简单 UI。当我在选项卡之间切换时,视频会变得混乱。


@Composable
internal fun LessonDetailsScreenRoute(
) {
    var selectedVideoUrl by remember { mutableStateOf<String?>(dummyUrl) }
    var selectedTab by remember {
        mutableStateOf(LessonDetailsScreenTabs.CONTENT)
    }

    Column (modifier = Modifier.fillMaxSize()){
        // Moving the player outside the recomposable part
        PlayerContainer(selectedVideoUrl)
        LessonDetailsScreen(
            selectedTab = selectedTab,
            onTabSelected = { newTab -> selectedTab = newTab },
        )
    }
}

@Composable
internal fun LessonDetailsScreen(
    selectedTab: LessonDetailsScreenTabs,
    onTabSelected: (LessonDetailsScreenTabs) -> Unit,
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(MaterialTheme.colorScheme.background)
    ) {


        // Lesson details AppBar
        LessonAppBar(chapterName = chapterName, lessonName = lessonName, onBackPressed = onBackPressed)

        // Tabs
        UtkorshoTabs(
            titles = lessonDetailsTabsTitle,
            tabIdentifier = LessonDetailsScreenTabs.entries,
            tabSelected = selectedTab,
            onTabSelected = onTabSelected
        )

        // Handle the selected tab
        when (selectedTab) {
            LessonDetailsScreenTabs.CONTENT -> {
                Text(text = "Content Tab")
            }
            LessonDetailsScreenTabs.QNA -> {
                Box(modifier = Modifier.size(50.dp)) // Another simple UI element
            }
        }
    }
}

@OptIn(UnstableApi::class)
@Composable
fun PlayerContainer(videoUrl: String?) {
    // Isolate the player from the recompositions of other UI elements
   // RecorderPlayerScreen(videoUrl = videoUrl)

    val context = LocalContext.current

    // Keep the player instance stable across recompositions
    val playerInstance = remember {
        ExoPlayer.Builder(context).build().apply {
            playWhenReady = true
        }
    }

    // Only prepare the player when the URL changes
    LaunchedEffect(videoUrl) {
        Log.d("PROBLEM_DIAGNOSIS", "Preparing player with URL: $videoUrl")
        videoUrl?.let {
            val mediaSource = createDataSource(it)
            playerInstance.setMediaSource(mediaSource)
            playerInstance.prepare()
        }
    }

    // Only dispose of the player when the entire screen is disposed
    DisposableEffect(Unit) {
        onDispose {
            Log.d("PROBLEM_DIAGNOSIS", "Releasing player as screen is disposed")
            playerInstance.release()
        }
    }

    // Render the Player View
    AndroidView(
        factory = { ctx ->
            Log.d("PROBLEM_DIAGNOSIS", "PlayerView created")
            PlayerView(ctx).apply {
                player = playerInstance
            }
        },
        update = { view ->
            Log.d("PROBLEM_DIAGNOSIS", "PlayerView updated")
            view.player = playerInstance
        },
        modifier = Modifier
            .fillMaxWidth()
            .height(300.dp)
    )
}
 when (selectedTab) {
            LessonDetailsScreenTabs.CONTENT -> {
               Box(modifier = Modifier.size(50.dp))
            }
            LessonDetailsScreenTabs.QNA -> {
                Box(modifier = Modifier.size(50.dp)) // Another simple UI element
            }
        }

如果我不保留内容和 Qna 中的任何东西,它不会给播放器带来问题。如果我只保留 Box 或 Spacer,它不会给播放器带来任何问题。但如果我使用简单的文本视图,它就会开始产生上述问题。

帖子版权声明 1、本帖标题:在选项卡之间切换或多次点击自定义控制器时,ExoPlayer(Media3)视频会变得混乱或混乱
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Alexi Maschas在本站《kotlin》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: