ExpandedController中直播Current time显示异常,如何重载或隐藏该控件?
Got it, let's tackle this problem with the ExpandedController's incorrect Current Time display in your live streaming scenario. Here are two practical approaches—either hiding the control entirely or overriding it to fix the time display properly:
1. Hide the Current Time Control
If you don't need the Current Time display at all, you can easily hide it with these steps:
- First, locate the ID of the Current Time TextView in your
ExpandedControllerlayout. For common media player libraries like ExoPlayer, this is oftenR.id.exo_current_time. - In your activity or fragment where you initialize the player, grab a reference to this view and set its visibility to
GONE:
// Kotlin example val currentTimeView = playerView.findViewById<TextView>(R.id.exo_current_time) currentTimeView.visibility = View.GONE
// Java example TextView currentTimeView = playerView.findViewById(R.id.exo_current_time); currentTimeView.setVisibility(View.GONE);
- Alternatively, if you're using a custom
ExpandedControllerlayout file, you can directly setandroid:visibility="gone"on the Current Time TextView in XML to avoid runtime code changes.
2. Override the Current Time Control to Fix the Display
If you want to keep the Current Time but fix its incorrect value (common in live streams where default logic shows offset instead of wall-clock time), you can override the controller's update logic:
- Create a custom class that extends your base
ExpandedController(e.g.,PlayerControlViewfor ExoPlayer) and override the method responsible for updating progress/time:
class CustomLiveExpandedController(context: Context, attrs: AttributeSet?) : PlayerControlView(context, attrs) { override fun updateProgress() { super.updateProgress() player?.let { livePlayer -> // For live streams, calculate the actual wall-clock time if needed val currentLivePosition = livePlayer.currentPosition // Adjust with live offset if your stream uses it val correctedTime = currentLivePosition - (livePlayer.currentLiveOffset ?: 0) // Update the Current Time TextView with formatted corrected time findViewById<TextView>(R.id.exo_current_time)?.text = formatLiveTime(correctedTime) } } private fun formatLiveTime(timeMs: Long): String { // Implement your preferred time formatting logic here val totalSeconds = (timeMs / 1000).toInt() val hours = totalSeconds / 3600 val minutes = (totalSeconds % 3600) / 60 val seconds = totalSeconds % 60 return if (hours > 0) { String.format("%d:%02d:%02d", hours, minutes, seconds) } else { String.format("%02d:%02d", minutes, seconds) } } }
- Replace the default controller in your layout XML with this custom one:
<com.your.app.package.CustomLiveExpandedController android:id="@+id/live_player_controller" android:layout_width="match_parent" android:layout_height="wrap_content" app:playerView="@id/your_player_view"/>
- This gives you full control over how the Current Time is calculated and displayed, ensuring it's accurate for your live streaming use case.
内容的提问来源于stack exchange,提问作者Hrk
相关产品推荐
相关产品推荐

