Add resumable action
This commit is contained in:
parent
9e53504ab5
commit
67d6cc43ab
|
@ -1,7 +1,6 @@
|
|||
package org.libre.agosto.p2play
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.ComponentName
|
||||
import android.content.Intent
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.graphics.Bitmap
|
||||
|
@ -10,7 +9,6 @@ import android.net.Uri
|
|||
import android.os.AsyncTask
|
||||
import android.os.Bundle
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import android.webkit.WebChromeClient
|
||||
|
@ -26,10 +24,8 @@ import androidx.media3.exoplayer.DefaultLoadControl
|
|||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import androidx.media3.exoplayer.upstream.DefaultAllocator
|
||||
import androidx.media3.session.MediaController
|
||||
import androidx.media3.session.SessionToken
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.common.util.concurrent.MoreExecutors
|
||||
import com.squareup.picasso.Picasso
|
||||
import kotlinx.android.synthetic.main.activity_reproductor.*
|
||||
import org.libre.agosto.p2play.adapters.CommentariesAdapter
|
||||
|
@ -39,7 +35,6 @@ import org.libre.agosto.p2play.ajax.Videos
|
|||
import org.libre.agosto.p2play.helpers.setFullscreen
|
||||
import org.libre.agosto.p2play.models.CommentaryModel
|
||||
import org.libre.agosto.p2play.models.VideoModel
|
||||
import org.libre.agosto.p2play.services.PlaybackService
|
||||
import org.libre.agosto.p2play.singletons.PlaybackSingleton
|
||||
|
||||
@Suppress("NAME_SHADOWING")
|
||||
|
@ -64,25 +59,8 @@ class ReproductorActivity : AppCompatActivity() {
|
|||
// Fullscreen info
|
||||
private var isFullscreen = false
|
||||
|
||||
fun onStart2() {
|
||||
val sessionToken = SessionToken(this, ComponentName(this, PlaybackService::class.java))
|
||||
val controllerFuture = MediaController.Builder(this, sessionToken).buildAsync()
|
||||
|
||||
controllerFuture.addListener(
|
||||
{
|
||||
// Call controllerFuture.get() to retrieve the MediaController.
|
||||
// MediaController implements the Player interface, so it can be
|
||||
// attached to the PlayerView UI component.
|
||||
// playerView.setPlayer(controllerFuture.get())
|
||||
mediaControl = controllerFuture.get()
|
||||
|
||||
if (mediaControl.isPlaying) {
|
||||
mediaControl.stop()
|
||||
}
|
||||
},
|
||||
MoreExecutors.directExecutor(),
|
||||
)
|
||||
}
|
||||
// Resume info
|
||||
private var isResume = false
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled", "SetTextI18n")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
@ -102,7 +80,15 @@ class ReproductorActivity : AppCompatActivity() {
|
|||
videoView.settings.domStorageEnabled = true
|
||||
|
||||
try {
|
||||
video = this.intent.extras?.getSerializable("video") as VideoModel
|
||||
val resume = this.intent.extras?.getSerializable("resume")
|
||||
if (resume == null) {
|
||||
video = this.intent.extras?.getSerializable("video") as VideoModel
|
||||
isResume = false
|
||||
} else {
|
||||
video = PlaybackSingleton.video!!
|
||||
isResume = true
|
||||
}
|
||||
|
||||
tittleVideoTxt.text = this.video.name
|
||||
viewsTxt.text = "${this.video.views} ${getString(R.string.view_text)}"
|
||||
userTxt.text = this.video.username
|
||||
|
@ -166,22 +152,19 @@ class ReproductorActivity : AppCompatActivity() {
|
|||
|
||||
println("----- video --------")
|
||||
println(video.streamingData?.playlistUrl)
|
||||
val mediaItem = MediaItem.Builder()
|
||||
.setUri(videoPlayback.streamingData?.playlistUrl!!)
|
||||
.setMediaMetadata(
|
||||
MediaMetadata.Builder()
|
||||
.setArtist(videoPlayback.username)
|
||||
.setTitle(videoPlayback.name)
|
||||
.setArtworkUri(Uri.parse("https://${ManagerSingleton.url}${videoPlayback.thumbUrl}"))
|
||||
.build(),
|
||||
).build()
|
||||
|
||||
Log.d("htum", "https://${ManagerSingleton.url}${videoPlayback.thumbUrl}")
|
||||
// Set the media item to be played.
|
||||
// player.setMediaItem(mediaItem)
|
||||
// Prepare the player.
|
||||
// player.prepare()
|
||||
PlaybackSingleton.setData(mediaItem, videoPlayback)
|
||||
if (!isResume) {
|
||||
val mediaItem = MediaItem.Builder()
|
||||
.setUri(videoPlayback.streamingData?.playlistUrl!!)
|
||||
.setMediaMetadata(
|
||||
MediaMetadata.Builder()
|
||||
.setArtist(videoPlayback.username)
|
||||
.setTitle(videoPlayback.name)
|
||||
.setArtworkUri(Uri.parse("https://${ManagerSingleton.url}${videoPlayback.thumbUrl}"))
|
||||
.build(),
|
||||
).build()
|
||||
PlaybackSingleton.setData(mediaItem, video)
|
||||
}
|
||||
// Start the playback.
|
||||
// player.play()
|
||||
} catch (err: Exception) {
|
||||
|
|
|
@ -71,8 +71,8 @@ class VideoModel(
|
|||
data.endArray()
|
||||
}
|
||||
"files" -> {
|
||||
data.beginArray()
|
||||
if (streamingData === null) {
|
||||
data.beginArray()
|
||||
if (data.hasNext()) {
|
||||
data.beginObject()
|
||||
while (data.hasNext()) {
|
||||
|
@ -87,8 +87,8 @@ class VideoModel(
|
|||
}
|
||||
data.endObject()
|
||||
}
|
||||
data.endArray()
|
||||
}
|
||||
data.endArray()
|
||||
}
|
||||
"channel" -> {
|
||||
data.beginObject()
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package org.libre.agosto.p2play.services
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import androidx.media3.session.MediaSession
|
||||
import androidx.media3.session.MediaSessionService
|
||||
import org.libre.agosto.p2play.ReproductorActivity
|
||||
import org.libre.agosto.p2play.singletons.PlaybackSingleton
|
||||
|
||||
class PlaybackService : MediaSessionService() {
|
||||
|
@ -13,18 +16,31 @@ class PlaybackService : MediaSessionService() {
|
|||
val player = PlaybackSingleton.player!!
|
||||
mediaSession = MediaSession.Builder(this, player)
|
||||
.build()
|
||||
val contentIntent = Intent(this, ReproductorActivity::class.java)
|
||||
contentIntent.putExtra("resume", true)
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
this,
|
||||
0,
|
||||
contentIntent,
|
||||
PendingIntent.FLAG_MUTABLE,
|
||||
)
|
||||
mediaSession!!.setSessionActivity(pendingIntent)
|
||||
}
|
||||
|
||||
// Remember to release the player and media session in onDestroy
|
||||
override fun onDestroy() {
|
||||
mediaSession?.run {
|
||||
player.release()
|
||||
release()
|
||||
mediaSession = null
|
||||
}
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||
this.mediaSession!!.player.stop()
|
||||
super.onTaskRemoved(rootIntent)
|
||||
}
|
||||
|
||||
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? {
|
||||
return mediaSession
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import org.libre.agosto.p2play.services.PlaybackService
|
|||
|
||||
object PlaybackSingleton {
|
||||
var player: ExoPlayer? = null
|
||||
private var video: VideoModel? = null
|
||||
var video: VideoModel? = null
|
||||
private var withMediaSession = false
|
||||
|
||||
fun setData(mediaItem: MediaItem, video: VideoModel): ExoPlayer {
|
||||
|
|
Loading…
Reference in New Issue