Skip to content

Commit 379e1ae

Browse files
committed
optimize retry
1 parent 0838af0 commit 379e1ae

File tree

7 files changed

+93
-62
lines changed

7 files changed

+93
-62
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 更新日誌
22

3+
### v1.3.8.14
4+
5+
* 優化重試
6+
37
### v1.3.8.13
48

59
* 支持切換軟解/硬解

app/src/main/java/com/lizongying/mytv0/MainViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ class MainViewModel : ViewModel() {
354354
logo ?: "",
355355
"",
356356
uris,
357+
0,
357358
mapOf(),
358359
group,
359360
SourceType.UNKNOWN,
@@ -377,6 +378,7 @@ class MainViewModel : ViewModel() {
377378
t0.logo,
378379
"",
379380
uris,
381+
0,
380382
mapOf(),
381383
t0.group,
382384
SourceType.UNKNOWN,
@@ -425,6 +427,7 @@ class MainViewModel : ViewModel() {
425427
"",
426428
"",
427429
uris,
430+
0,
428431
mapOf(),
429432
channelGroup,
430433
SourceType.UNKNOWN,

app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,29 @@ class PlayerFragment : Fragment() {
5454

5555
@OptIn(UnstableApi::class)
5656
fun updatePlayer() {
57+
if (context == null) {
58+
Log.e(TAG, "context == null")
59+
return
60+
}
61+
62+
val ctx = requireContext()
63+
5764
val playerView = binding.playerView
5865

59-
val renderersFactory = context?.let { DefaultRenderersFactory(it) }
66+
val renderersFactory = DefaultRenderersFactory(ctx)
6067
val playerMediaCodecSelector = PlayerMediaCodecSelector()
61-
renderersFactory?.setMediaCodecSelector(playerMediaCodecSelector)
62-
renderersFactory?.setExtensionRendererMode(
68+
renderersFactory.setMediaCodecSelector(playerMediaCodecSelector)
69+
renderersFactory.setExtensionRendererMode(
6370
if (SP.softDecode) DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER else DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF
6471
)
6572

6673
if (player != null) {
6774
player?.release()
6875
}
6976

70-
player = context?.let {
71-
ExoPlayer.Builder(it)
72-
.setRenderersFactory(renderersFactory!!)
73-
.build()
74-
}
77+
player = ExoPlayer.Builder(ctx)
78+
.setRenderersFactory(renderersFactory)
79+
.build()
7580
player?.repeatMode = REPEAT_MODE_ALL
7681
player?.playWhenReady = true
7782
player?.addListener(object : Player.Listener {
@@ -91,22 +96,24 @@ class PlayerFragment : Fragment() {
9196

9297
override fun onIsPlayingChanged(isPlaying: Boolean) {
9398
super.onIsPlayingChanged(isPlaying)
99+
100+
if (tvModel == null) {
101+
Log.e(TAG, "tvModel == null")
102+
return
103+
}
104+
105+
val tv = tvModel!!
106+
94107
if (isPlaying) {
95-
tvModel?.confirmSourceType()
96-
tvModel?.setErrInfo("")
97-
tvModel!!.retryTimes = 0
108+
tv.confirmSourceType()
109+
tv.confirmVideoIndex()
110+
tv.setErrInfo("")
111+
tv.retryTimes = 0
98112
} else {
99-
Log.i(TAG, "${tvModel?.tv?.title} 播放停止")
100-
// tvModel?.setErrInfo("播放停止")
113+
Log.i(TAG, "${tv.tv.title} 播放停止")
101114
}
102115
}
103116

104-
override fun onPlaybackStateChanged(playbackState: Int) {
105-
Log.d(TAG, "playbackState $playbackState")
106-
super.onPlaybackStateChanged(playbackState)
107-
}
108-
109-
110117
override fun onPositionDiscontinuity(
111118
oldPosition: Player.PositionInfo,
112119
newPosition: Player.PositionInfo,
@@ -120,34 +127,42 @@ class PlayerFragment : Fragment() {
120127

121128
override fun onPlayerError(error: PlaybackException) {
122129
super.onPlayerError(error)
123-
tvModel?.setErrInfo(R.string.play_error.getString())
124130

125-
if (tvModel!!.retryTimes < tvModel!!.retryMaxTimes) {
131+
if (tvModel == null) {
132+
Log.e(TAG, "tvModel == null")
133+
return
134+
}
135+
136+
val tv = tvModel!!
137+
138+
if (tv.retryTimes < tv.retryMaxTimes) {
126139
var last = true
127-
if (tvModel?.getSourceTypeDefault() == SourceType.UNKNOWN) {
128-
last = tvModel!!.nextSourceType()
140+
if (tv.getSourceTypeDefault() == SourceType.UNKNOWN) {
141+
last = tv.nextSourceType()
129142
}
130-
tvModel?.setReady()
143+
tv.setReady(true)
131144
if (last) {
132-
tvModel!!.retryTimes++
145+
tv.retryTimes++
133146
}
134147
Log.i(
135148
TAG,
136-
"retry ${tvModel!!.videoIndex.value} ${tvModel!!.getSourceTypeCurrent()} ${tvModel!!.retryTimes}/${tvModel!!.retryMaxTimes}"
149+
"retry ${tv.videoIndex.value} ${tv.getSourceTypeCurrent()} ${tv.retryTimes}/${tv.retryMaxTimes}"
137150
)
138151
} else {
139-
if (!tvModel!!.isLastVideo()) {
140-
tvModel!!.nextVideo()
141-
tvModel?.setReady()
142-
tvModel!!.retryTimes = 0
152+
if (!tv.isLastVideo()) {
153+
tv.nextVideo()
154+
tv.setReady(true)
155+
tv.retryTimes = 0
156+
} else {
157+
tv.setErrInfo(R.string.play_error.getString())
143158
}
144159
}
145160
}
146161
})
147162

148163
playerView.player = player
149-
if (tvModel != null) {
150-
play(tvModel!!)
164+
tvModel?.let {
165+
play(it)
151166
}
152167
}
153168

app/src/main/java/com/lizongying/mytv0/data/TV.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ data class TV(
1010
var logo: String = "",
1111
var image: String? = null,
1212
var uris: List<String>,
13+
var videoIndex: Int = 0,
1314
var headers: Map<String, String>? = null,
1415
var group: String = "",
1516
var sourceType: SourceType = SourceType.UNKNOWN,

app/src/main/java/com/lizongying/mytv0/models/TVModel.kt

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class TVModel(var tv: TV) : ViewModel() {
4343

4444
var listIndex = 0
4545

46-
private var sourceTypeList: MutableList<SourceType> =
47-
mutableListOf(
46+
private var sourceTypeList: List<SourceType> =
47+
listOf(
4848
SourceType.UNKNOWN,
4949
)
5050
private var sourceTypeIndex = 0
@@ -69,17 +69,18 @@ class TVModel(var tv: TV) : ViewModel() {
6969
val program: LiveData<MutableList<Program>>
7070
get() = _program
7171

72-
fun getVideoUrl(): String? {
73-
if (_videoIndex.value == null || tv.uris.isEmpty()) {
74-
return null
75-
}
72+
private val _videoIndex = MutableLiveData<Int>()
73+
val videoIndex: LiveData<Int>
74+
get() = _videoIndex
75+
val videoIndexValue: Int
76+
get() = _videoIndex.value ?: 0
7677

77-
if (videoIndex.value!! >= tv.uris.size) {
78+
fun getVideoUrl(): String? {
79+
if (videoIndexValue >= tv.uris.size) {
7880
return null
7981
}
8082

81-
val index = min(max(_videoIndex.value!!, 0), tv.uris.size - 1)
82-
return tv.uris[index]
83+
return tv.uris[videoIndexValue]
8384
}
8485

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

97-
fun setReady() {
98+
fun setReady(retry: Boolean = false) {
99+
if (!retry) {
100+
setErrInfo("")
101+
retryTimes = 0
102+
103+
_videoIndex.value = max(0, min(tv.uris.size - 1, tv.videoIndex))
104+
sourceTypeIndex =
105+
max(0, min(sourceTypeList.size - 1, sourceTypeList.indexOf(tv.sourceType)))
106+
}
98107
_ready.value = true
99108
}
100109

101-
private val _videoIndex = MutableLiveData<Int>()
102-
val videoIndex: LiveData<Int>
103-
get() = _videoIndex
104-
105110
private var userAgent = ""
106111

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

134139
_httpDataSource = defaultHttpDataSource
135140

136-
if (path.lowercase().endsWith(".m3u8")) {
137-
sourceTypeList[0] = SourceType.HLS
141+
sourceTypeList = if (path.lowercase().endsWith(".m3u8")) {
142+
listOf(SourceType.HLS)
138143
} else if (path.lowercase().endsWith(".mpd")) {
139-
sourceTypeList[0] = SourceType.DASH
144+
listOf(SourceType.DASH)
140145
} else if (scheme.lowercase() == "rtsp") {
141-
sourceTypeList[0] = SourceType.RTSP
146+
listOf(SourceType.RTSP)
142147
} else if (scheme.lowercase() == "rtmp") {
143-
sourceTypeList[0] = SourceType.RTMP
148+
listOf(SourceType.RTMP)
144149
} else if (scheme.lowercase() == "rtp") {
145-
sourceTypeList[0] = SourceType.RTP
150+
listOf(SourceType.RTP)
146151
} else {
147-
sourceTypeList[0] = SourceType.HLS
148-
sourceTypeList.add(SourceType.PROGRESSIVE)
152+
listOf(SourceType.HLS, SourceType.PROGRESSIVE)
149153
}
150154

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

160164
fun getSourceTypeCurrent(): SourceType {
161-
sourceTypeIndex = min(max(0, sourceTypeIndex), sourceTypeList.size - 1)
165+
sourceTypeIndex = max(0, min(sourceTypeList.size - 1, sourceTypeIndex))
162166
return sourceTypeList[sourceTypeIndex]
163167
}
164168

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

180+
fun confirmVideoIndex() {
181+
tv.videoIndex = videoIndexValue
182+
}
183+
176184
@OptIn(UnstableApi::class)
177185
fun getMediaSource(): MediaSource? {
178186
if (sourceTypeList.isEmpty()) {
@@ -214,27 +222,28 @@ class TVModel(var tv: TV) : ViewModel() {
214222
}
215223

216224
fun isLastVideo(): Boolean {
217-
return _videoIndex.value!! == tv.uris.size - 1
225+
return videoIndexValue == tv.uris.size - 1
218226
}
219227

220228
fun nextVideo(): Boolean {
221229
if (tv.uris.isEmpty()) {
222230
return false
223231
}
224232

225-
_videoIndex.value = (_videoIndex.value!! + 1) % tv.uris.size
226-
sourceTypeList = mutableListOf(
233+
_videoIndex.value = (videoIndexValue + 1) % tv.uris.size
234+
sourceTypeList = listOf(
227235
SourceType.UNKNOWN,
228236
)
229-
return _videoIndex.value!! == tv.uris.size - 1
237+
238+
return isLastVideo()
230239
}
231240

232241
fun update(t: TV) {
233242
tv = t
234243
}
235244

236245
init {
237-
_videoIndex.value = min(0, tv.uris.size - 1)
246+
_videoIndex.value = max(0, min(tv.uris.size - 1, tv.videoIndex))
238247
_like.value = SP.getLike(tv.id)
239248
_program.value = mutableListOf()
240249
}

app/src/main/res/raw/ipv6.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version_code": 16975885, "version_name": "v1.3.8.13"}
1+
{"version_code": 16975886, "version_name": "v1.3.8.14"}

0 commit comments

Comments
 (0)