Skip to content

Commit

Permalink
optimize retry
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Jan 2, 2025
1 parent 0838af0 commit 379e1ae
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 62 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 更新日誌

### v1.3.8.14

* 優化重試

### v1.3.8.13

* 支持切換軟解/硬解
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ class MainViewModel : ViewModel() {
logo ?: "",
"",
uris,
0,
mapOf(),
group,
SourceType.UNKNOWN,
Expand All @@ -377,6 +378,7 @@ class MainViewModel : ViewModel() {
t0.logo,
"",
uris,
0,
mapOf(),
t0.group,
SourceType.UNKNOWN,
Expand Down Expand Up @@ -425,6 +427,7 @@ class MainViewModel : ViewModel() {
"",
"",
uris,
0,
mapOf(),
channelGroup,
SourceType.UNKNOWN,
Expand Down
79 changes: 47 additions & 32 deletions app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,29 @@ class PlayerFragment : Fragment() {

@OptIn(UnstableApi::class)
fun updatePlayer() {
if (context == null) {
Log.e(TAG, "context == null")
return
}

val ctx = requireContext()

val playerView = binding.playerView

val renderersFactory = context?.let { DefaultRenderersFactory(it) }
val renderersFactory = DefaultRenderersFactory(ctx)
val playerMediaCodecSelector = PlayerMediaCodecSelector()
renderersFactory?.setMediaCodecSelector(playerMediaCodecSelector)
renderersFactory?.setExtensionRendererMode(
renderersFactory.setMediaCodecSelector(playerMediaCodecSelector)
renderersFactory.setExtensionRendererMode(
if (SP.softDecode) DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER else DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF
)

if (player != null) {
player?.release()
}

player = context?.let {
ExoPlayer.Builder(it)
.setRenderersFactory(renderersFactory!!)
.build()
}
player = ExoPlayer.Builder(ctx)
.setRenderersFactory(renderersFactory)
.build()
player?.repeatMode = REPEAT_MODE_ALL
player?.playWhenReady = true
player?.addListener(object : Player.Listener {
Expand All @@ -91,22 +96,24 @@ class PlayerFragment : Fragment() {

override fun onIsPlayingChanged(isPlaying: Boolean) {
super.onIsPlayingChanged(isPlaying)

if (tvModel == null) {
Log.e(TAG, "tvModel == null")
return
}

val tv = tvModel!!

if (isPlaying) {
tvModel?.confirmSourceType()
tvModel?.setErrInfo("")
tvModel!!.retryTimes = 0
tv.confirmSourceType()
tv.confirmVideoIndex()
tv.setErrInfo("")
tv.retryTimes = 0
} else {
Log.i(TAG, "${tvModel?.tv?.title} 播放停止")
// tvModel?.setErrInfo("播放停止")
Log.i(TAG, "${tv.tv.title} 播放停止")
}
}

override fun onPlaybackStateChanged(playbackState: Int) {
Log.d(TAG, "playbackState $playbackState")
super.onPlaybackStateChanged(playbackState)
}


override fun onPositionDiscontinuity(
oldPosition: Player.PositionInfo,
newPosition: Player.PositionInfo,
Expand All @@ -120,34 +127,42 @@ class PlayerFragment : Fragment() {

override fun onPlayerError(error: PlaybackException) {
super.onPlayerError(error)
tvModel?.setErrInfo(R.string.play_error.getString())

if (tvModel!!.retryTimes < tvModel!!.retryMaxTimes) {
if (tvModel == null) {
Log.e(TAG, "tvModel == null")
return
}

val tv = tvModel!!

if (tv.retryTimes < tv.retryMaxTimes) {
var last = true
if (tvModel?.getSourceTypeDefault() == SourceType.UNKNOWN) {
last = tvModel!!.nextSourceType()
if (tv.getSourceTypeDefault() == SourceType.UNKNOWN) {
last = tv.nextSourceType()
}
tvModel?.setReady()
tv.setReady(true)
if (last) {
tvModel!!.retryTimes++
tv.retryTimes++
}
Log.i(
TAG,
"retry ${tvModel!!.videoIndex.value} ${tvModel!!.getSourceTypeCurrent()} ${tvModel!!.retryTimes}/${tvModel!!.retryMaxTimes}"
"retry ${tv.videoIndex.value} ${tv.getSourceTypeCurrent()} ${tv.retryTimes}/${tv.retryMaxTimes}"
)
} else {
if (!tvModel!!.isLastVideo()) {
tvModel!!.nextVideo()
tvModel?.setReady()
tvModel!!.retryTimes = 0
if (!tv.isLastVideo()) {
tv.nextVideo()
tv.setReady(true)
tv.retryTimes = 0
} else {
tv.setErrInfo(R.string.play_error.getString())
}
}
}
})

playerView.player = player
if (tvModel != null) {
play(tvModel!!)
tvModel?.let {
play(it)
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/lizongying/mytv0/data/TV.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ data class TV(
var logo: String = "",
var image: String? = null,
var uris: List<String>,
var videoIndex: Int = 0,
var headers: Map<String, String>? = null,
var group: String = "",
var sourceType: SourceType = SourceType.UNKNOWN,
Expand Down
65 changes: 37 additions & 28 deletions app/src/main/java/com/lizongying/mytv0/models/TVModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class TVModel(var tv: TV) : ViewModel() {

var listIndex = 0

private var sourceTypeList: MutableList<SourceType> =
mutableListOf(
private var sourceTypeList: List<SourceType> =
listOf(
SourceType.UNKNOWN,
)
private var sourceTypeIndex = 0
Expand All @@ -69,17 +69,18 @@ class TVModel(var tv: TV) : ViewModel() {
val program: LiveData<MutableList<Program>>
get() = _program

fun getVideoUrl(): String? {
if (_videoIndex.value == null || tv.uris.isEmpty()) {
return null
}
private val _videoIndex = MutableLiveData<Int>()
val videoIndex: LiveData<Int>
get() = _videoIndex
val videoIndexValue: Int
get() = _videoIndex.value ?: 0

if (videoIndex.value!! >= tv.uris.size) {
fun getVideoUrl(): String? {
if (videoIndexValue >= tv.uris.size) {
return null
}

val index = min(max(_videoIndex.value!!, 0), tv.uris.size - 1)
return tv.uris[index]
return tv.uris[videoIndexValue]
}

private val _like = MutableLiveData<Boolean>()
Expand All @@ -94,14 +95,18 @@ class TVModel(var tv: TV) : ViewModel() {
val ready: LiveData<Boolean>
get() = _ready

fun setReady() {
fun setReady(retry: Boolean = false) {
if (!retry) {
setErrInfo("")
retryTimes = 0

_videoIndex.value = max(0, min(tv.uris.size - 1, tv.videoIndex))
sourceTypeIndex =
max(0, min(sourceTypeList.size - 1, sourceTypeList.indexOf(tv.sourceType)))
}
_ready.value = true
}

private val _videoIndex = MutableLiveData<Int>()
val videoIndex: LiveData<Int>
get() = _videoIndex

private var userAgent = ""

private var _httpDataSource: DataSource.Factory? = null
Expand Down Expand Up @@ -133,19 +138,18 @@ class TVModel(var tv: TV) : ViewModel() {

_httpDataSource = defaultHttpDataSource

if (path.lowercase().endsWith(".m3u8")) {
sourceTypeList[0] = SourceType.HLS
sourceTypeList = if (path.lowercase().endsWith(".m3u8")) {
listOf(SourceType.HLS)
} else if (path.lowercase().endsWith(".mpd")) {
sourceTypeList[0] = SourceType.DASH
listOf(SourceType.DASH)
} else if (scheme.lowercase() == "rtsp") {
sourceTypeList[0] = SourceType.RTSP
listOf(SourceType.RTSP)
} else if (scheme.lowercase() == "rtmp") {
sourceTypeList[0] = SourceType.RTMP
listOf(SourceType.RTMP)
} else if (scheme.lowercase() == "rtp") {
sourceTypeList[0] = SourceType.RTP
listOf(SourceType.RTP)
} else {
sourceTypeList[0] = SourceType.HLS
sourceTypeList.add(SourceType.PROGRESSIVE)
listOf(SourceType.HLS, SourceType.PROGRESSIVE)
}

MediaItem.fromUri(it)
Expand All @@ -158,7 +162,7 @@ class TVModel(var tv: TV) : ViewModel() {
}

fun getSourceTypeCurrent(): SourceType {
sourceTypeIndex = min(max(0, sourceTypeIndex), sourceTypeList.size - 1)
sourceTypeIndex = max(0, min(sourceTypeList.size - 1, sourceTypeIndex))
return sourceTypeList[sourceTypeIndex]
}

Expand All @@ -173,6 +177,10 @@ class TVModel(var tv: TV) : ViewModel() {
tv.sourceType = getSourceTypeCurrent()
}

fun confirmVideoIndex() {
tv.videoIndex = videoIndexValue
}

@OptIn(UnstableApi::class)
fun getMediaSource(): MediaSource? {
if (sourceTypeList.isEmpty()) {
Expand Down Expand Up @@ -214,27 +222,28 @@ class TVModel(var tv: TV) : ViewModel() {
}

fun isLastVideo(): Boolean {
return _videoIndex.value!! == tv.uris.size - 1
return videoIndexValue == tv.uris.size - 1
}

fun nextVideo(): Boolean {
if (tv.uris.isEmpty()) {
return false
}

_videoIndex.value = (_videoIndex.value!! + 1) % tv.uris.size
sourceTypeList = mutableListOf(
_videoIndex.value = (videoIndexValue + 1) % tv.uris.size
sourceTypeList = listOf(
SourceType.UNKNOWN,
)
return _videoIndex.value!! == tv.uris.size - 1

return isLastVideo()
}

fun update(t: TV) {
tv = t
}

init {
_videoIndex.value = min(0, tv.uris.size - 1)
_videoIndex.value = max(0, min(tv.uris.size - 1, tv.videoIndex))
_like.value = SP.getLike(tv.id)
_program.value = mutableListOf()
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/raw/ipv6.txt

This file was deleted.

2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version_code": 16975885, "version_name": "v1.3.8.13"}
{"version_code": 16975886, "version_name": "v1.3.8.14"}

0 comments on commit 379e1ae

Please sign in to comment.