fix tianmao-mohe
This commit is contained in:
parent
872f668c59
commit
83a70291d4
|
@ -113,4 +113,8 @@ dependencies {
|
||||||
implementation 'com.github.bumptech.glide:glide:4.11.0'
|
implementation 'com.github.bumptech.glide:glide:4.11.0'
|
||||||
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.2"
|
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.2"
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0-RC")
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0-RC")
|
||||||
|
|
||||||
|
implementation 'com.google.android.exoplayer:exoplayer-ui:2.13.3'
|
||||||
|
implementation 'com.google.android.exoplayer:exoplayer-core:2.13.3'
|
||||||
|
implementation 'com.google.android.exoplayer:exoplayer-hls:2.13.3'
|
||||||
}
|
}
|
|
@ -3,6 +3,8 @@ package com.lizongying.mytv
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
|
import android.view.SurfaceHolder
|
||||||
|
import android.view.SurfaceView
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.view.ViewTreeObserver
|
import android.view.ViewTreeObserver
|
||||||
|
@ -15,24 +17,39 @@ import androidx.media3.common.VideoSize
|
||||||
import androidx.media3.common.util.UnstableApi
|
import androidx.media3.common.util.UnstableApi
|
||||||
import androidx.media3.exoplayer.ExoPlayer
|
import androidx.media3.exoplayer.ExoPlayer
|
||||||
import androidx.media3.ui.PlayerView
|
import androidx.media3.ui.PlayerView
|
||||||
|
import com.google.android.exoplayer2.SimpleExoPlayer
|
||||||
import com.lizongying.mytv.databinding.PlayerBinding
|
import com.lizongying.mytv.databinding.PlayerBinding
|
||||||
import com.lizongying.mytv.models.TVViewModel
|
import com.lizongying.mytv.models.TVViewModel
|
||||||
|
|
||||||
|
|
||||||
class PlayerFragment : Fragment() {
|
class PlayerFragment : Fragment(), SurfaceHolder.Callback {
|
||||||
|
|
||||||
private var _binding: PlayerBinding? = null
|
private var _binding: PlayerBinding? = null
|
||||||
private var playerView: PlayerView? = null
|
private var playerView: PlayerView? = null
|
||||||
private var tvViewModel: TVViewModel? = null
|
private var tvViewModel: TVViewModel? = null
|
||||||
private val aspectRatio = 16f / 9f
|
private val aspectRatio = 16f / 9f
|
||||||
|
|
||||||
|
|
||||||
|
private lateinit var surfaceView: SurfaceView
|
||||||
|
private lateinit var surfaceHolder: SurfaceHolder
|
||||||
|
private var exoPlayer: SimpleExoPlayer? = null
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater, container: ViewGroup?,
|
inflater: LayoutInflater, container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View {
|
): View {
|
||||||
_binding = PlayerBinding.inflate(inflater, container, false)
|
_binding = PlayerBinding.inflate(inflater, container, false)
|
||||||
playerView = _binding!!.playerView
|
|
||||||
(activity as MainActivity).playerFragment = this
|
if (Utils.isTmallDevice()) {
|
||||||
|
_binding!!.playerView.visibility = View.GONE
|
||||||
|
surfaceView = _binding!!.surfaceView
|
||||||
|
surfaceHolder = surfaceView.holder
|
||||||
|
surfaceHolder.addCallback(this)
|
||||||
|
} else {
|
||||||
|
_binding!!.surfaceView.visibility = View.GONE
|
||||||
|
playerView = _binding!!.playerView
|
||||||
|
}
|
||||||
|
|
||||||
playerView?.viewTreeObserver?.addOnGlobalLayoutListener(object :
|
playerView?.viewTreeObserver?.addOnGlobalLayoutListener(object :
|
||||||
ViewTreeObserver.OnGlobalLayoutListener {
|
ViewTreeObserver.OnGlobalLayoutListener {
|
||||||
override fun onGlobalLayout() {
|
override fun onGlobalLayout() {
|
||||||
|
@ -78,6 +95,10 @@ class PlayerFragment : Fragment() {
|
||||||
setMediaItem(MediaItem.fromUri(tvViewModel.getVideoUrlCurrent()))
|
setMediaItem(MediaItem.fromUri(tvViewModel.getVideoUrlCurrent()))
|
||||||
prepare()
|
prepare()
|
||||||
}
|
}
|
||||||
|
exoPlayer?.run {
|
||||||
|
setMediaItem(com.google.android.exoplayer2.MediaItem.fromUri(tvViewModel.getVideoUrlCurrent()))
|
||||||
|
prepare()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
|
@ -86,8 +107,10 @@ class PlayerFragment : Fragment() {
|
||||||
if (playerView != null && playerView!!.player?.isPlaying == false) {
|
if (playerView != null && playerView!!.player?.isPlaying == false) {
|
||||||
Log.i(TAG, "replay")
|
Log.i(TAG, "replay")
|
||||||
playerView!!.player?.prepare()
|
playerView!!.player?.prepare()
|
||||||
} else {
|
}
|
||||||
Log.i(TAG, "playing")
|
if (exoPlayer?.isPlaying == false) {
|
||||||
|
Log.i(TAG, "replay")
|
||||||
|
exoPlayer?.prepare()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,6 +124,9 @@ class PlayerFragment : Fragment() {
|
||||||
if (playerView != null && playerView!!.player?.isPlaying == true) {
|
if (playerView != null && playerView!!.player?.isPlaying == true) {
|
||||||
playerView!!.player?.stop()
|
playerView!!.player?.stop()
|
||||||
}
|
}
|
||||||
|
if (exoPlayer?.isPlaying == true) {
|
||||||
|
exoPlayer?.stop()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
@ -108,6 +134,7 @@ class PlayerFragment : Fragment() {
|
||||||
if (playerView != null) {
|
if (playerView != null) {
|
||||||
playerView!!.player?.release()
|
playerView!!.player?.release()
|
||||||
}
|
}
|
||||||
|
exoPlayer?.release()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroyView() {
|
override fun onDestroyView() {
|
||||||
|
@ -118,4 +145,16 @@ class PlayerFragment : Fragment() {
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "PlaybackVideoFragment"
|
private const val TAG = "PlaybackVideoFragment"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun surfaceCreated(holder: SurfaceHolder) {
|
||||||
|
exoPlayer = SimpleExoPlayer.Builder(requireContext()).build()
|
||||||
|
exoPlayer?.setVideoSurfaceHolder(surfaceHolder)
|
||||||
|
exoPlayer?.playWhenReady = true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun surfaceDestroyed(holder: SurfaceHolder) {
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package com.lizongying.mytv
|
package com.lizongying.mytv
|
||||||
|
|
||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
|
import android.os.Build
|
||||||
import android.util.TypedValue
|
import android.util.TypedValue
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.lizongying.mytv.api.TimeResponse
|
import com.lizongying.mytv.api.TimeResponse
|
||||||
|
@ -71,4 +72,6 @@ object Utils {
|
||||||
TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(), Resources.getSystem().displayMetrics
|
TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(), Resources.getSystem().displayMetrics
|
||||||
).toInt()
|
).toInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isTmallDevice() = Build.MANUFACTURER.equals("Tmall", ignoreCase = true)
|
||||||
}
|
}
|
|
@ -7,6 +7,14 @@
|
||||||
android:background="@color/black"
|
android:background="@color/black"
|
||||||
android:keepScreenOn="true">
|
android:keepScreenOn="true">
|
||||||
|
|
||||||
|
<SurfaceView
|
||||||
|
android:id="@+id/surface_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
app:resize_mode="fill"
|
||||||
|
app:use_controller="false" />
|
||||||
|
|
||||||
<androidx.media3.ui.PlayerView
|
<androidx.media3.ui.PlayerView
|
||||||
android:id="@+id/player_view"
|
android:id="@+id/player_view"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|
Loading…
Reference in New Issue