Skip to content

Commit cbf7c4c

Browse files
committed
remember play position for offline video
1 parent 5c6e437 commit cbf7c4c

File tree

5 files changed

+56
-33
lines changed

5 files changed

+56
-33
lines changed

README.md

-13
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,6 @@
4444
<a href="art/18.png"><img src="art/18.png" width="30%"/></a>
4545

4646
## 待做列表
47-
- [x] 修复:Android6.0以下机型更新失败的问题(V1.1.2)
48-
- [x] 修复:镇站之宝后20集没有声音的问题(V1.1.2)
49-
- [x] 修复:多任务下载卡顿的问题(V1.1.2)
50-
- [x] 修复:下载时可能出现多个任务同时下载的问题(V1.1.2)
51-
- [x] 修复:下载导致的内存泄露问题(V1.1.2)
52-
- [x] 优化:登陆用户名不再限制为邮箱(V1.1.2)
53-
- [x] 优化:按顺序下载(V1.1.2)
54-
- [x] 新增:修改个人信息(V1.1.2)
55-
- [x] 新增:修改密码(V1.1.2)
56-
- [x] 新增:注册(V1.1.2)
57-
- [x] 新增:忘记密码(V1.1.2)
58-
- [x] 新增:弹幕屏蔽功能(V1.1.2)
59-
- [x] 删除:变速播放(V1.1.2)
6047
- [ ] 修改头像(V1.1.3)
6148
- [ ] 同步收藏(接口不全)
6249
- [ ] 发私信(接口不全)

app/src/main/kotlin/me/sweetll/tucao/business/download/adapter/DownloadedVideoAdapter.kt

+9-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import me.sweetll.tucao.business.video.VideoActivity
1515
import me.sweetll.tucao.extension.formatWithUnit
1616
import me.sweetll.tucao.extension.load
1717

18-
class DownloadedVideoAdapter(val downloadActivity: DownloadActivity, data: MutableList<MultiItemEntity>?): BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {
18+
class DownloadedVideoAdapter(val downloadActivity: DownloadActivity, data: MutableList<MultiItemEntity>?) : BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {
1919
companion object {
2020
const val TYPE_VIDEO = 0
2121
const val TYPE_PART = 1
@@ -38,8 +38,7 @@ class DownloadedVideoAdapter(val downloadActivity: DownloadActivity, data: Mutab
3838
helper.setVisible(R.id.checkbox, video.checkable)
3939
val checkBox = helper.getView<CheckBox>(R.id.checkbox)
4040
checkBox.isChecked = video.checked
41-
checkBox.setOnCheckedChangeListener {
42-
_, checked ->
41+
checkBox.setOnCheckedChangeListener { _, checked ->
4342
video.checked = checked
4443
updateMenu()
4544
}
@@ -56,7 +55,7 @@ class DownloadedVideoAdapter(val downloadActivity: DownloadActivity, data: Mutab
5655
updateMenu()
5756
} else {
5857
if (video.singlePart) {
59-
CachedVideoActivity.intentTo(mContext, video.parts[0])
58+
CachedVideoActivity.intentTo(mContext, video)
6059
} else {
6160
if (video.isExpanded) {
6261
collapse(helper.adapterPosition)
@@ -78,19 +77,18 @@ class DownloadedVideoAdapter(val downloadActivity: DownloadActivity, data: Mutab
7877
helper.setVisible(R.id.checkbox, part.checkable)
7978
val checkBox = helper.getView<CheckBox>(R.id.checkbox)
8079
checkBox.isChecked = part.checked
81-
checkBox.setOnCheckedChangeListener {
82-
_, checked ->
80+
checkBox.setOnCheckedChangeListener { _, checked ->
8381
part.checked = checked
8482
updateMenu()
8583
}
8684

8785
helper.itemView.setOnClickListener {
86+
val parentVideo = data.find { video ->
87+
(video as Video).parts.any { it.vid == part.vid }
88+
} as Video
8889
if (part.checkable) {
8990
checkBox.isChecked = !checkBox.isChecked
90-
val parentVideo = data.find {
91-
video ->
92-
(video as Video).parts.any { it.vid == part.vid }
93-
} as Video
91+
9492
val currentPosition = parentVideo.parts.indexOf(part)
9593
val newParentChecked = parentVideo.parts.all(Part::checked)
9694
if (newParentChecked != parentVideo.checked) {
@@ -99,7 +97,7 @@ class DownloadedVideoAdapter(val downloadActivity: DownloadActivity, data: Mutab
9997
}
10098
updateMenu()
10199
} else {
102-
CachedVideoActivity.intentTo(mContext, part)
100+
CachedVideoActivity.intentTo(mContext, parentVideo.apply { parts = mutableListOf(parts.find { it.vid == part.vid }!! ) })
103101
}
104102
}
105103
}

app/src/main/kotlin/me/sweetll/tucao/business/video/CachedVideoActivity.kt

+45-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.content.Intent
55
import android.databinding.DataBindingUtil
66
import android.os.Bundle
7+
import android.text.format.DateFormat
78
import android.view.View
89
import android.view.WindowManager
910
import com.shuyu.gsyvideoplayer.GSYPreViewManager
@@ -19,11 +20,14 @@ import me.sweetll.tucao.base.BaseActivity
1920
import me.sweetll.tucao.model.json.Part
2021
import me.sweetll.tucao.business.video.adapter.StandardVideoAllCallBackAdapter
2122
import me.sweetll.tucao.databinding.ActivityCachedVideoBinding
23+
import me.sweetll.tucao.extension.HistoryHelpers
2224
import me.sweetll.tucao.extension.setUp
2325
import me.sweetll.tucao.extension.toast
26+
import me.sweetll.tucao.model.json.Video
2427
import me.sweetll.tucao.widget.DanmuVideoPlayer
2528
import tv.danmaku.ijk.media.player.IjkMediaPlayer
2629
import java.io.File
30+
import java.util.*
2731

2832
class CachedVideoActivity : BaseActivity(), DanmuVideoPlayer.DanmuPlayerHolder {
2933
lateinit var binding: ActivityCachedVideoBinding
@@ -33,12 +37,17 @@ class CachedVideoActivity : BaseActivity(), DanmuVideoPlayer.DanmuPlayerHolder {
3337
var isPlay = false
3438
var isPause = false
3539

40+
var firstPlay = true
41+
42+
lateinit var video: Video
43+
lateinit var selectedPart: Part
44+
3645
companion object {
37-
private val ARG_PART = "part"
46+
private val ARG_VIDEO = "video"
3847

39-
fun intentTo(context: Context, part: Part) {
48+
fun intentTo(context: Context, video: Video) {
4049
val intent = Intent(context, CachedVideoActivity::class.java)
41-
intent.putExtra(ARG_PART, part)
50+
intent.putExtra(ARG_VIDEO, video)
4251
context.startActivity(intent)
4352
}
4453
}
@@ -47,11 +56,22 @@ class CachedVideoActivity : BaseActivity(), DanmuVideoPlayer.DanmuPlayerHolder {
4756
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
4857

4958
binding = DataBindingUtil.setContentView(this, R.layout.activity_cached_video)
50-
val part: Part = intent.getParcelableExtra(ARG_PART)
59+
video = intent.getParcelableExtra(ARG_VIDEO)
60+
selectedPart = video.parts[0]
61+
62+
HistoryHelpers.loadPlayHistory()
63+
.flatMap { it.parts }
64+
.find { it.vid == selectedPart.vid }
65+
?.let {
66+
selectedPart.hadPlay = true
67+
selectedPart.lastPlayPosition = it.lastPlayPosition
68+
}
69+
70+
5171
setupPlayer()
52-
loadPart(part)
72+
loadPart(selectedPart)
5373

54-
val danmuFile = File(part.durls[0].cacheFolderPath, "danmu.xml")
74+
val danmuFile = File(selectedPart.durls[0].cacheFolderPath, "danmu.xml")
5575
if (danmuFile.exists()) {
5676
loadDanmuUri(danmuFile.absolutePath)
5777
} else {
@@ -88,7 +108,14 @@ class CachedVideoActivity : BaseActivity(), DanmuVideoPlayer.DanmuPlayerHolder {
88108
override fun onPrepared(url: String?) {
89109
super.onPrepared(url)
90110
isPlay = true
111+
if (firstPlay) {
112+
firstPlay = false
113+
if (selectedPart.lastPlayPosition != 0) {
114+
binding.player.showJump(selectedPart.lastPlayPosition)
115+
}
116+
}
91117
}
118+
92119
})
93120

94121
binding.player.fullscreenButton.visibility = View.GONE
@@ -119,7 +146,7 @@ class CachedVideoActivity : BaseActivity(), DanmuVideoPlayer.DanmuPlayerHolder {
119146

120147
override fun onPause() {
121148
super.onPause()
122-
binding.player.onVideoPause()
149+
binding.player.onVideoPause(isPlay)
123150
isPause = true
124151
}
125152

@@ -141,6 +168,16 @@ class CachedVideoActivity : BaseActivity(), DanmuVideoPlayer.DanmuPlayerHolder {
141168
}
142169

143170
override fun onSavePlayHistory(position: Int) {
144-
// DO nothing
171+
HistoryHelpers.savePlayHistory(
172+
video.copy(create = DateFormat.format("yyyy-MM-dd hh:mm:ss", Date()).toString())
173+
.also {
174+
it.parts = video.parts.filter {
175+
it.vid == selectedPart.vid
176+
}.map {
177+
it.lastPlayPosition = position
178+
it
179+
}.toMutableList()
180+
}
181+
)
145182
}
146183
}

app/src/main/kotlin/me/sweetll/tucao/widget/DanmuVideoPlayer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class DanmuVideoPlayer : PreviewGSYVideoPlayer {
144144

145145
fun hideSetting() {
146146
settingLayout.animate()
147-
.translationX((250f).dp2px())
147+
.translationX((280f).dp2px())
148148
.setDuration(200)
149149
.setListener(object : AnimatorListenerAdapter() {
150150
override fun onAnimationEnd(animation: Animator?) {

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- 修复:下载导致的内存泄露问题
99
- 优化:登陆用户名不再限制为邮箱
1010
- 优化:按顺序下载
11+
- 优化:离线视频记录播放位置
1112
- 新增:修改个人信息
1213
- 新增:修改密码
1314
- 新增:注册

0 commit comments

Comments
 (0)