2026-05-24 21:09:05 +08:00
|
|
|
package com.videoapp.tv
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2026-05-24 21:30:37 +08:00
|
|
|
import android.os.Handler
|
|
|
|
|
import android.os.Looper
|
2026-05-24 21:09:05 +08:00
|
|
|
import android.view.View
|
|
|
|
|
import android.webkit.WebView
|
|
|
|
|
import android.webkit.WebViewClient
|
|
|
|
|
import android.widget.Button
|
2026-05-24 21:19:34 +08:00
|
|
|
import android.widget.ImageButton
|
2026-05-24 21:30:37 +08:00
|
|
|
import android.widget.LinearLayout
|
2026-05-24 21:09:05 +08:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2026-05-24 21:14:19 +08:00
|
|
|
import androidx.core.content.ContextCompat
|
2026-05-24 21:09:05 +08:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
|
import androidx.media3.common.MediaItem
|
|
|
|
|
import androidx.media3.exoplayer.ExoPlayer
|
|
|
|
|
import androidx.media3.ui.PlayerView
|
2026-05-24 22:14:50 +08:00
|
|
|
import com.videoapp.tv.data.AppDatabase
|
2026-05-24 21:09:05 +08:00
|
|
|
import com.videoapp.tv.data.ConfigRepository
|
2026-05-24 22:14:50 +08:00
|
|
|
import com.videoapp.tv.data.PlayHistory
|
2026-05-24 21:09:05 +08:00
|
|
|
import com.videoapp.tv.engine.Episode
|
2026-05-24 21:30:37 +08:00
|
|
|
import com.videoapp.tv.engine.PlaySource
|
2026-05-24 21:09:05 +08:00
|
|
|
import com.videoapp.tv.engine.VideoExtractor
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
|
|
class PlayerActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
private lateinit var playerView: PlayerView
|
|
|
|
|
private lateinit var playerWebView: WebView
|
2026-05-24 21:30:37 +08:00
|
|
|
private lateinit var controlPanel: View
|
|
|
|
|
private lateinit var sourceList: LinearLayout
|
|
|
|
|
private lateinit var episodeList: LinearLayout
|
2026-05-24 21:09:05 +08:00
|
|
|
private lateinit var loadingIndicator: View
|
|
|
|
|
private lateinit var errorText: android.widget.TextView
|
2026-05-24 21:19:34 +08:00
|
|
|
private lateinit var btnClose: ImageButton
|
2026-05-24 21:09:05 +08:00
|
|
|
|
|
|
|
|
private var exoPlayer: ExoPlayer? = null
|
|
|
|
|
private val videoExtractor = VideoExtractor()
|
|
|
|
|
private val configRepo by lazy { ConfigRepository(this) }
|
2026-05-24 22:14:50 +08:00
|
|
|
private val playHistoryDao by lazy { AppDatabase.getInstance(this).playHistoryDao() }
|
2026-05-24 21:09:05 +08:00
|
|
|
|
2026-05-24 21:30:37 +08:00
|
|
|
private var sources: List<PlaySource> = emptyList()
|
|
|
|
|
private var currentSourceIndex = 0
|
|
|
|
|
private var currentEpisode: Episode? = null
|
|
|
|
|
|
|
|
|
|
private val hideHandler = Handler(Looper.getMainLooper())
|
|
|
|
|
private val hideRunnable = Runnable { hideControls() }
|
|
|
|
|
private var controlsVisible = true
|
2026-05-24 21:09:05 +08:00
|
|
|
|
2026-05-24 22:14:50 +08:00
|
|
|
// 用于保存播放历史的信息
|
|
|
|
|
private var videoTitle: String = ""
|
|
|
|
|
private var videoCategory: String? = null
|
|
|
|
|
private var coverUrl: String? = null
|
|
|
|
|
private var detailUrl: String = ""
|
|
|
|
|
|
2026-05-24 21:09:05 +08:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
setContentView(R.layout.activity_player)
|
|
|
|
|
|
|
|
|
|
playerView = findViewById(R.id.player_view)
|
|
|
|
|
playerWebView = findViewById(R.id.player_webview)
|
2026-05-24 21:30:37 +08:00
|
|
|
controlPanel = findViewById(R.id.control_panel)
|
|
|
|
|
sourceList = findViewById(R.id.source_list)
|
2026-05-24 21:09:05 +08:00
|
|
|
episodeList = findViewById(R.id.episode_list)
|
|
|
|
|
loadingIndicator = findViewById(R.id.loading_indicator)
|
|
|
|
|
errorText = findViewById(R.id.error_text)
|
2026-05-24 21:19:34 +08:00
|
|
|
btnClose = findViewById(R.id.btn_close)
|
2026-05-24 21:09:05 +08:00
|
|
|
|
2026-05-24 22:14:50 +08:00
|
|
|
detailUrl = intent.getStringExtra("detail_url") ?: ""
|
|
|
|
|
videoTitle = intent.getStringExtra("title") ?: ""
|
|
|
|
|
videoCategory = intent.getStringExtra("category")
|
|
|
|
|
coverUrl = intent.getStringExtra("cover_url")
|
2026-05-24 21:09:05 +08:00
|
|
|
|
2026-05-24 21:19:34 +08:00
|
|
|
btnClose.setOnClickListener { finish() }
|
|
|
|
|
|
2026-05-24 21:09:05 +08:00
|
|
|
initExoPlayer()
|
2026-05-24 21:30:37 +08:00
|
|
|
loadSources(detailUrl)
|
2026-05-24 21:19:34 +08:00
|
|
|
setupTouchListeners()
|
2026-05-24 21:09:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun initExoPlayer() {
|
|
|
|
|
exoPlayer = ExoPlayer.Builder(this).build().also { player ->
|
|
|
|
|
playerView.player = player
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 21:30:37 +08:00
|
|
|
private fun loadSources(detailUrl: String) {
|
2026-05-24 21:09:05 +08:00
|
|
|
showLoading(true)
|
|
|
|
|
val config = configRepo.getConfig()
|
|
|
|
|
|
|
|
|
|
lifecycleScope.launch {
|
|
|
|
|
try {
|
2026-05-24 21:30:37 +08:00
|
|
|
sources = videoExtractor.extractVideos(detailUrl, config)
|
2026-05-24 21:09:05 +08:00
|
|
|
|
2026-05-24 21:30:37 +08:00
|
|
|
if (sources.isNotEmpty()) {
|
|
|
|
|
buildSourceUI()
|
|
|
|
|
selectSource(0)
|
|
|
|
|
resetAutoHide()
|
2026-05-24 21:09:05 +08:00
|
|
|
} else {
|
|
|
|
|
tryPlayDirectly(detailUrl, config)
|
|
|
|
|
}
|
|
|
|
|
} catch (e: Exception) {
|
2026-05-24 21:30:37 +08:00
|
|
|
showError("加载失败: ${e.message}")
|
2026-05-24 21:09:05 +08:00
|
|
|
showLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 21:30:37 +08:00
|
|
|
private fun buildSourceUI() {
|
|
|
|
|
sourceList.removeAllViews()
|
|
|
|
|
sources.forEachIndexed { index, source ->
|
|
|
|
|
val btn = Button(this).apply {
|
|
|
|
|
text = source.name
|
|
|
|
|
setBackgroundResource(R.drawable.episode_selector)
|
|
|
|
|
setTextColor(ContextCompat.getColor(this@PlayerActivity, R.color.text_primary))
|
|
|
|
|
textSize = 13f
|
|
|
|
|
minWidth = 0
|
|
|
|
|
setPadding(16, 8, 16, 8)
|
|
|
|
|
isFocusable = true
|
|
|
|
|
isFocusableInTouchMode = true
|
|
|
|
|
setOnClickListener {
|
|
|
|
|
selectSource(index)
|
|
|
|
|
resetAutoHide()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sourceList.addView(btn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun selectSource(index: Int) {
|
|
|
|
|
if (index !in sources.indices) return
|
|
|
|
|
currentSourceIndex = index
|
|
|
|
|
|
|
|
|
|
// Highlight selected source
|
|
|
|
|
for (i in 0 until sourceList.childCount) {
|
|
|
|
|
sourceList.getChildAt(i).isSelected = (i == index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build episode list for this source
|
|
|
|
|
val source = sources[index]
|
|
|
|
|
buildEpisodeUI(source.episodes)
|
|
|
|
|
|
|
|
|
|
// Auto-play first episode
|
|
|
|
|
if (source.episodes.isNotEmpty()) {
|
|
|
|
|
playEpisode(source.episodes.first())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 21:09:05 +08:00
|
|
|
private fun buildEpisodeUI(eps: List<Episode>) {
|
|
|
|
|
episodeList.removeAllViews()
|
2026-05-24 21:30:37 +08:00
|
|
|
eps.forEach { ep ->
|
2026-05-24 21:09:05 +08:00
|
|
|
val btn = Button(this).apply {
|
|
|
|
|
text = ep.title
|
|
|
|
|
setBackgroundResource(R.drawable.episode_selector)
|
2026-05-24 21:14:19 +08:00
|
|
|
setTextColor(ContextCompat.getColor(this@PlayerActivity, R.color.text_primary))
|
2026-05-24 21:30:37 +08:00
|
|
|
textSize = 12f
|
2026-05-24 21:09:05 +08:00
|
|
|
minWidth = 0
|
2026-05-24 21:30:37 +08:00
|
|
|
setPadding(14, 6, 14, 6)
|
2026-05-24 21:09:05 +08:00
|
|
|
isFocusable = true
|
|
|
|
|
isFocusableInTouchMode = true
|
|
|
|
|
setOnClickListener {
|
|
|
|
|
highlightEpisode(it)
|
|
|
|
|
playEpisode(ep)
|
2026-05-24 21:30:37 +08:00
|
|
|
resetAutoHide()
|
2026-05-24 21:09:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
episodeList.addView(btn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun playEpisode(ep: Episode) {
|
2026-05-24 21:30:37 +08:00
|
|
|
currentEpisode = ep
|
2026-05-24 21:09:05 +08:00
|
|
|
showLoading(true)
|
|
|
|
|
val config = configRepo.getConfig()
|
|
|
|
|
|
2026-05-24 22:14:50 +08:00
|
|
|
// 保存播放历史
|
|
|
|
|
savePlayHistory(ep.title)
|
|
|
|
|
|
2026-05-24 21:09:05 +08:00
|
|
|
lifecycleScope.launch {
|
|
|
|
|
val (directUrl, iframeUrl) = videoExtractor.extractFromPlayPage(ep.playUrl, config)
|
|
|
|
|
|
|
|
|
|
if (directUrl != null) {
|
|
|
|
|
playWithExoPlayer(directUrl)
|
|
|
|
|
} else if (iframeUrl != null) {
|
|
|
|
|
playWithWebView(iframeUrl)
|
|
|
|
|
} else {
|
|
|
|
|
playWithWebView(ep.playUrl)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 22:14:50 +08:00
|
|
|
private fun savePlayHistory(episodeName: String?) {
|
|
|
|
|
lifecycleScope.launch {
|
|
|
|
|
try {
|
|
|
|
|
// 检查是否已存在相同的播放记录
|
|
|
|
|
val existing = playHistoryDao.findByDetailUrl(detailUrl)
|
|
|
|
|
if (existing != null) {
|
|
|
|
|
// 更新现有记录的播放时间和剧集
|
|
|
|
|
playHistoryDao.updatePlayTime(existing.id, System.currentTimeMillis(), episodeName)
|
|
|
|
|
} else {
|
|
|
|
|
// 插入新记录
|
|
|
|
|
val playHistory = PlayHistory(
|
|
|
|
|
title = videoTitle,
|
|
|
|
|
episodeName = episodeName,
|
|
|
|
|
detailUrl = detailUrl,
|
|
|
|
|
coverUrl = coverUrl,
|
|
|
|
|
category = videoCategory,
|
|
|
|
|
playTime = System.currentTimeMillis()
|
|
|
|
|
)
|
|
|
|
|
playHistoryDao.insert(playHistory)
|
|
|
|
|
}
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
// 保存播放历史失败不影响播放功能
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 21:09:05 +08:00
|
|
|
private fun tryPlayDirectly(detailUrl: String, config: com.videoapp.tv.data.SiteConfig) {
|
2026-05-24 22:14:50 +08:00
|
|
|
// 保存播放历史(直接播放时使用标题作为剧集名)
|
|
|
|
|
savePlayHistory(videoTitle)
|
|
|
|
|
|
2026-05-24 21:09:05 +08:00
|
|
|
lifecycleScope.launch {
|
|
|
|
|
val (directUrl, iframeUrl) = videoExtractor.extractFromPlayPage(detailUrl, config)
|
|
|
|
|
if (directUrl != null) {
|
2026-05-24 21:30:37 +08:00
|
|
|
controlPanel.visibility = View.GONE
|
2026-05-24 21:09:05 +08:00
|
|
|
playWithExoPlayer(directUrl)
|
|
|
|
|
} else if (iframeUrl != null) {
|
2026-05-24 21:30:37 +08:00
|
|
|
controlPanel.visibility = View.GONE
|
2026-05-24 21:09:05 +08:00
|
|
|
playWithWebView(iframeUrl)
|
|
|
|
|
} else {
|
2026-05-24 21:30:37 +08:00
|
|
|
controlPanel.visibility = View.GONE
|
2026-05-24 21:09:05 +08:00
|
|
|
playWithWebView(detailUrl)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun playWithExoPlayer(url: String) {
|
|
|
|
|
playerWebView.visibility = View.GONE
|
|
|
|
|
playerView.visibility = View.VISIBLE
|
|
|
|
|
showLoading(false)
|
|
|
|
|
|
|
|
|
|
val mediaItem = MediaItem.fromUri(url)
|
|
|
|
|
exoPlayer?.apply {
|
|
|
|
|
setMediaItem(mediaItem)
|
|
|
|
|
prepare()
|
|
|
|
|
playWhenReady = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun playWithWebView(url: String) {
|
|
|
|
|
playerView.visibility = View.GONE
|
|
|
|
|
playerWebView.visibility = View.VISIBLE
|
|
|
|
|
showLoading(false)
|
|
|
|
|
|
|
|
|
|
playerWebView.settings.javaScriptEnabled = true
|
|
|
|
|
playerWebView.settings.domStorageEnabled = true
|
|
|
|
|
playerWebView.settings.mediaPlaybackRequiresUserGesture = false
|
|
|
|
|
playerWebView.webViewClient = object : WebViewClient() {
|
|
|
|
|
override fun onPageFinished(view: WebView, url: String) {
|
|
|
|
|
super.onPageFinished(view, url)
|
|
|
|
|
showLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
playerWebView.loadUrl(url)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun highlightEpisode(view: View) {
|
|
|
|
|
for (i in 0 until episodeList.childCount) {
|
|
|
|
|
episodeList.getChildAt(i).isSelected = (episodeList.getChildAt(i) == view)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun showLoading(show: Boolean) {
|
|
|
|
|
loadingIndicator.visibility = if (show) View.VISIBLE else View.GONE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun showError(msg: String) {
|
|
|
|
|
errorText.text = msg
|
|
|
|
|
errorText.visibility = View.VISIBLE
|
|
|
|
|
showLoading(false)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 21:19:34 +08:00
|
|
|
private fun setupTouchListeners() {
|
2026-05-24 21:30:37 +08:00
|
|
|
val listener = View.OnClickListener { toggleControls() }
|
|
|
|
|
playerView.setOnClickListener(listener)
|
|
|
|
|
playerWebView.setOnClickListener(listener)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun toggleControls() {
|
|
|
|
|
if (controlsVisible) {
|
|
|
|
|
hideControls()
|
|
|
|
|
} else {
|
|
|
|
|
showControls()
|
|
|
|
|
resetAutoHide()
|
2026-05-24 21:09:05 +08:00
|
|
|
}
|
2026-05-24 21:30:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun showControls() {
|
|
|
|
|
controlsVisible = true
|
|
|
|
|
btnClose.visibility = View.VISIBLE
|
|
|
|
|
controlPanel.visibility = View.VISIBLE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun hideControls() {
|
|
|
|
|
controlsVisible = false
|
|
|
|
|
btnClose.visibility = View.GONE
|
|
|
|
|
controlPanel.visibility = View.GONE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun resetAutoHide() {
|
|
|
|
|
hideHandler.removeCallbacks(hideRunnable)
|
|
|
|
|
if (!controlsVisible) {
|
|
|
|
|
showControls()
|
2026-05-24 21:09:05 +08:00
|
|
|
}
|
2026-05-24 21:30:37 +08:00
|
|
|
hideHandler.postDelayed(hideRunnable, 4000)
|
2026-05-24 21:09:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
|
super.onPause()
|
|
|
|
|
exoPlayer?.playWhenReady = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
|
super.onDestroy()
|
2026-05-24 21:30:37 +08:00
|
|
|
hideHandler.removeCallbacks(hideRunnable)
|
2026-05-24 21:09:05 +08:00
|
|
|
exoPlayer?.release()
|
|
|
|
|
exoPlayer = null
|
|
|
|
|
}
|
|
|
|
|
}
|