From e022fe5e964a503f7f6121c987ca23bd78f8d292 Mon Sep 17 00:00:00 2001 From: Ivan Agosto Date: Thu, 28 Mar 2024 17:29:32 -0600 Subject: [PATCH] Add fullscreen to exoplayer --- .../agosto/p2play/ReproductorActivity.kt | 54 ++++++++++++++++++- .../libre/agosto/p2play/helpers/mapSeconds.kt | 2 +- .../libre/agosto/p2play/models/VideoModel.kt | 20 +++++++ .../main/res/layout/activity_reproductor.xml | 37 +++++++++++-- .../res/layout/custom_player_controls.xml | 10 +--- app/src/main/res/layout/view_video.xml | 2 +- 6 files changed, 108 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt b/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt index b0934ff..8916288 100644 --- a/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt +++ b/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt @@ -5,6 +5,7 @@ import android.content.Intent import android.content.pm.ActivityInfo import android.graphics.Bitmap import android.graphics.BitmapFactory +import android.media.Image import android.os.AsyncTask import android.os.Bundle import android.os.Looper @@ -14,6 +15,7 @@ import android.view.WindowManager import android.webkit.WebChromeClient import android.widget.EditText import android.widget.FrameLayout +import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.media3.common.MediaItem @@ -43,11 +45,20 @@ class ReproductorActivity : AppCompatActivity() { private lateinit var viewAdapter: RecyclerView.Adapter<*> private lateinit var viewManager: RecyclerView.LayoutManager + // Exoplayer + private lateinit var player: ExoPlayer + + // Fullscreen info + private var isFullscreen = false + @SuppressLint("SetJavaScriptEnabled", "SetTextI18n") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_reproductor) + val fullscreenButton: ImageView = exoPlayer.findViewById(R.id.exo_fullscreen_custom) + val fullscreenButton2 = fullscreenPlayer.findViewById(R.id.exo_fullscreen_custom) + videoView.webChromeClient = WebClient() videoView.settings.javaScriptEnabled = true videoView.settings.allowContentAccess = true @@ -91,6 +102,8 @@ class ReproductorActivity : AppCompatActivity() { showMoreBtn.setOnClickListener { getDescription() } shareLayout.setOnClickListener { shareIntent() } reportLayout.setOnClickListener { reportIntent() } + fullscreenButton.setOnClickListener { toggleFullscreen() } + fullscreenButton2.setOnClickListener { toggleFullscreen() } userImg.setOnClickListener { val intent = Intent(this, ChannelActivity::class.java) @@ -103,7 +116,7 @@ class ReproductorActivity : AppCompatActivity() { val video = this.clientVideo.getVideo(this.video.uuid) runOnUiThread { try { - val player = ExoPlayer.Builder(this.baseContext).build() + player = ExoPlayer.Builder(this.baseContext).build() exoPlayer.player = player println("----- video --------") @@ -114,7 +127,7 @@ class ReproductorActivity : AppCompatActivity() { // Prepare the player. player.prepare() // Start the playback. - player.play() + // player.play() } catch(err: Exception) { err.printStackTrace() } @@ -332,6 +345,43 @@ class ReproductorActivity : AppCompatActivity() { } } + private fun toggleFullscreen () { + if (isFullscreen) { + nonFullScreen.visibility = View.VISIBLE + fullScreenExo.visibility = View.GONE + exoPlayer.player = player + fullscreenPlayer.player = null + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + + val attrs = window.attributes + attrs.flags = attrs.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv() + attrs.flags = attrs.flags and WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON.inv() + window.attributes = attrs + window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE + + isFullscreen = false + } + else { + val matchParent = WindowManager.LayoutParams.MATCH_PARENT + + nonFullScreen.visibility = View.GONE + fullScreenExo.visibility = View.VISIBLE + + exoPlayer.player = null + fullscreenPlayer.player = player + + setFullscreen(window) + + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE + isFullscreen = true + } + } + + override fun onDestroy() { + player.release() + super.onDestroy() + } + internal inner class WebClient: WebChromeClient() { private var mCustomView: View? = null private var mCustomViewCallback: WebChromeClient.CustomViewCallback? = null diff --git a/app/src/main/java/org/libre/agosto/p2play/helpers/mapSeconds.kt b/app/src/main/java/org/libre/agosto/p2play/helpers/mapSeconds.kt index fd99d08..8ab458f 100644 --- a/app/src/main/java/org/libre/agosto/p2play/helpers/mapSeconds.kt +++ b/app/src/main/java/org/libre/agosto/p2play/helpers/mapSeconds.kt @@ -7,7 +7,7 @@ fun mapSeconds (inputSeconds: Int): String { if (minutes > 60) { hours = minutes / 60 - minutes = minutes % 60 + minutes %= 60 } var result = minutes.toString().padStart(2, '0') + ":" + seconds.toString().padStart(2, '0') diff --git a/app/src/main/java/org/libre/agosto/p2play/models/VideoModel.kt b/app/src/main/java/org/libre/agosto/p2play/models/VideoModel.kt index 5062faa..f3f4a0f 100644 --- a/app/src/main/java/org/libre/agosto/p2play/models/VideoModel.kt +++ b/app/src/main/java/org/libre/agosto/p2play/models/VideoModel.kt @@ -69,6 +69,26 @@ class VideoModel( } data.endArray() } + "files" -> { + if (streamingData === null) { + data.beginArray() + if (data.hasNext()) { + data.beginObject() + while (data.hasNext()) { + val key2 = data.nextName() + when (key2.toString()) { + "fileDownloadUrl"->{ + streamingData = StreamingModel() + streamingData!!.playlistUrl = data.nextString() + } + else -> data.skipValue() + } + } + data.endObject() + } + data.endArray() + } + } "channel"->{ data.beginObject() while (data.hasNext()){ diff --git a/app/src/main/res/layout/activity_reproductor.xml b/app/src/main/res/layout/activity_reproductor.xml index c9fd31b..ae84106 100644 --- a/app/src/main/res/layout/activity_reproductor.xml +++ b/app/src/main/res/layout/activity_reproductor.xml @@ -10,11 +10,39 @@ android:id="@+id/fullScreen" android:layout_width="fill_parent" android:layout_height="fill_parent" - android:layout_alignParentTop="true" - android:layout_alignParentBottom="true" android:layout_alignParentStart="true" + android:layout_alignParentTop="true" android:layout_alignParentEnd="true" - android:visibility="gone" /> + android:layout_alignParentBottom="true" + android:visibility="gone" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + + + + + android:layout_weight="1" + android:visibility="gone" /> - - diff --git a/app/src/main/res/layout/view_video.xml b/app/src/main/res/layout/view_video.xml index db6d5d6..fcccb02 100644 --- a/app/src/main/res/layout/view_video.xml +++ b/app/src/main/res/layout/view_video.xml @@ -23,7 +23,7 @@