Merge pull request #923 from Bnyro/master

better live stream handling
This commit is contained in:
Bnyro 2022-07-30 13:54:53 +02:00 committed by GitHub
commit cdcf307498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 13 deletions

View File

@ -1,3 +1,5 @@
import java.time.Instant
plugins {
id 'com.android.application'
id 'kotlin-android'
@ -25,9 +27,8 @@ android {
// use the date as version for debug builds
if (variant.name == 'debug') {
variant.outputs.each { output ->
def date = getDate()
output.versionCodeOverride = date
output.versionNameOverride = date
output.versionCodeOverride = getUnixTime()
output.versionNameOverride = getUnixTime()
}
}
}
@ -104,8 +105,6 @@ dependencies {
implementation libs.coil
}
static def getDate() {
def date = new Date()
def formattedDate = date.format('yyyyMMddHH')
return Integer.parseInt(formattedDate)
static def getUnixTime() {
return Instant.now().getEpochSecond()
}

View File

@ -19,6 +19,7 @@ import android.os.PowerManager
import android.support.v4.media.session.MediaSessionCompat
import android.text.Html
import android.text.TextUtils
import android.text.format.DateUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
@ -118,6 +119,7 @@ class PlayerFragment : Fragment() {
private var playlistId: String? = null
private var channelId: String? = null
private var isSubscribed: Boolean = false
private var isLive = false
/**
* for the transition
@ -522,7 +524,6 @@ class PlayerFragment : Fragment() {
val playbackSpeedValues =
context?.resources?.getStringArray(R.array.playbackSpeedValues)!!
exoPlayer.setPlaybackSpeed(playbackSpeed.toFloat())
Log.e(TAG, playbackSpeed)
val speedIndex = playbackSpeedValues.indexOf(playbackSpeed)
playerBinding.speedText.text = playbackSpeeds[speedIndex]
@ -769,6 +770,12 @@ class PlayerFragment : Fragment() {
// save related streams for autoplay
relatedStreams = response.relatedStreams
// duration that's not greater than 0 indicates that the video is live
if (!(response.duration!! > 0)) {
isLive = true
handleLiveVideo()
}
runOnUiThread {
// set media sources for the player
setResolutionAndSubtitles(response)
@ -793,6 +800,34 @@ class PlayerFragment : Fragment() {
run()
}
private fun handleLiveVideo() {
playerBinding.exoTime.visibility = View.GONE
playerBinding.liveLL.visibility = View.VISIBLE
refreshLiveStatus()
}
private fun refreshLiveStatus() {
// switch back to normal speed when on the end of live stream
Log.e(exoPlayer.duration.toString(), exoPlayer.currentPosition.toString())
if (isLive && (exoPlayer.duration - exoPlayer.currentPosition < 10000)) {
exoPlayer.setPlaybackSpeed(1F)
playerBinding.speedText.text = "1x"
playerBinding.liveSeparator.visibility = View.GONE
playerBinding.liveDiff.text = ""
} else if (isLive) {
Log.e(TAG, "changing the time")
// live stream but not watching at the end/live position
playerBinding.liveSeparator.visibility = View.VISIBLE
val diffText = DateUtils.formatElapsedTime(
(exoPlayer.duration - exoPlayer.currentPosition) / 1000
)
playerBinding.liveDiff.text = "-$diffText"
}
// call it again
Handler(Looper.getMainLooper())
.postDelayed(this@PlayerFragment::refreshLiveStatus, 100)
}
private fun seekToWatchPosition() {
// seek to saved watch position if available
val watchPositions = PreferenceHelper.getWatchPositions()
@ -946,7 +981,8 @@ class PlayerFragment : Fragment() {
binding.apply {
playerViewsInfo.text =
context?.getString(R.string.views, response.views.formatShort()) +
"" + response.uploadDate
if (!isLive) "" + response.uploadDate else ""
textLike.text = response.likes.formatShort()
textDislike.text = response.dislikes.formatShort()
ConnectionHelper.loadImage(response.uploaderAvatar, binding.playerChannelImage)

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
<merge xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<View
@ -175,13 +176,42 @@
<TextView
android:id="@id/exo_position"
style="@style/ExoStyledControls.TimeText.Position" />
style="@style/TimeString" />
<TextView style="@style/ExoStyledControls.TimeText.Separator" />
<TextView
android:text=" • "
style="@style/TimeString"
tools:ignore="HardcodedText" />
<TextView
android:id="@id/exo_duration"
style="@style/ExoStyledControls.TimeText.Duration" />
style="@style/TimeString" />
</LinearLayout>
<LinearLayout
android:id="@+id/liveLL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="5dp"
android:visibility="gone">
<TextView
android:id="@+id/liveDiff"
style="@style/TimeString" />
<TextView
android:id="@+id/liveSeparator"
style="@style/TimeString"
android:text=" • "
android:visibility="gone"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/liveIndicator"
android:text="@string/live"
style="@style/TimeString" />
</LinearLayout>

View File

@ -135,5 +135,16 @@
</style>
<style name="TimeString">
<item name="android:gravity">center</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="paddingStart">4dp</item>
<item name="paddingEnd">4dp</item>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@android:color/white</item>
</style>
</resources>