Skip to content

Commit

Permalink
suport video cross protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed May 18, 2024
1 parent 1777eb0 commit 2db4392
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 更新日志

### v1.2.3

* 修复一些无法播放的问题

### v1.2.2

* 修复一些无法播放的问题
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.datasource.TransferListener
import androidx.media3.exoplayer.DefaultRenderersFactory
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.hls.HlsMediaSource
import androidx.media3.exoplayer.mediacodec.MediaCodecSelector
import androidx.media3.exoplayer.mediacodec.MediaCodecUtil
import com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_PERIOD_TRANSITION
Expand Down Expand Up @@ -149,6 +150,8 @@ class PlayerFragment : Fragment(), SurfaceHolder.Callback {
player?.run {
IgnoreSSLCertificate.ignore()
val httpDataSource = DefaultHttpDataSource.Factory()
httpDataSource.setKeepPostFor302Redirects(true)
httpDataSource.setAllowCrossProtocolRedirects(true)
httpDataSource.setTransferListener(object : TransferListener {
override fun onTransferInitializing(
source: DataSource,
Expand Down
26 changes: 20 additions & 6 deletions app/src/main/java/com/lizongying/mytv0/models/TVModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import androidx.media3.exoplayer.dash.DashMediaSource
import androidx.media3.exoplayer.hls.HlsMediaSource
import androidx.media3.exoplayer.rtsp.RtspMediaSource
import androidx.media3.exoplayer.source.MediaSource
import androidx.media3.exoplayer.source.ProgressiveMediaSource
import com.lizongying.mytv0.SP

//import com.google.android.exoplayer2.source.MediaSource
//import com.google.android.exoplayer2.source.MediaSourceFactory
import java.util.Locale

class TVModel(var tv: TV) : ViewModel() {
private val _position = MutableLiveData<Int>()
Expand Down Expand Up @@ -102,6 +101,8 @@ class TVModel(var tv: TV) : ViewModel() {

var userAgent = ""
val httpDataSource = DefaultHttpDataSource.Factory()
httpDataSource.setKeepPostFor302Redirects(true)
httpDataSource.setAllowCrossProtocolRedirects(true)
tv.headers?.let {
httpDataSource.setDefaultRequestProperties(it)
it.forEach { (key, value) ->
Expand All @@ -123,13 +124,26 @@ class TVModel(var tv: TV) : ViewModel() {
} else {
RtspMediaSource.Factory().setUserAgent(userAgent).createMediaSource(mediaItem)
}
} else if (isVideoFile(uri)) {
ProgressiveMediaSource.Factory(httpDataSource).createMediaSource(mediaItem)
} else {
null
HlsMediaSource.Factory(httpDataSource).createMediaSource(mediaItem)
}
}

// fun buildSource2(): com.google.android.exoplayer2.source.MediaSource {
// }
private fun isVideoFile(uri: Uri): Boolean {
val path = uri.lastPathSegment ?: return false
return if (path.contains(".")) {
val fileExtension =
path.substring(path.lastIndexOf('.') + 1)
.lowercase(
Locale.getDefault()
)
fileExtension == "mp4" || fileExtension == "avi" || fileExtension == "mov" || fileExtension == "wmv" || fileExtension == "mkv" || fileExtension == "flv" || fileExtension == "mpeg" || fileExtension == "webm"
} else {
false
}
}

companion object {
private const val TAG = "TVModel"
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version_code": 16908800, "version_name": "v1.2.2"}
{"version_code": 16909056, "version_name": "v1.2.3"}

0 comments on commit 2db4392

Please sign in to comment.