add offline player activity

This commit is contained in:
Bnyro 2022-09-09 15:40:10 +02:00
parent 97031be690
commit 226350e21c
7 changed files with 183 additions and 6 deletions

View File

@ -42,6 +42,10 @@
android:name=".activities.CommunityActivity"
android:label="@string/settings" />
<activity
android:name=".activities.OfflinePlayerActivity"
android:label="@string/player" />
<activity
android:name=".activities.MainActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"

View File

@ -0,0 +1,143 @@
package com.github.libretube.activities
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.github.libretube.BuildConfig
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.ActivityOfflinePlayerBinding
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
import com.github.libretube.extensions.BaseActivity
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.ui.StyledPlayerView
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.Util
import java.io.File
class OfflinePlayerActivity : BaseActivity() {
private lateinit var binding: ActivityOfflinePlayerBinding
private lateinit var fileName: String
private lateinit var player: ExoPlayer
private lateinit var playerView: StyledPlayerView
private lateinit var playerBinding: ExoStyledPlayerControlViewBinding
override fun onCreate(savedInstanceState: Bundle?) {
hideSystemBars()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
super.onCreate(savedInstanceState)
fileName = intent?.getStringExtra(IntentData.fileName)!!
binding = ActivityOfflinePlayerBinding.inflate(layoutInflater)
setContentView(binding.root)
initializePlayer()
playVideo()
}
private fun initializePlayer() {
player = ExoPlayer.Builder(this)
.build()
playerView = binding.player
playerView.player = player
playerBinding = binding.player.binding
playerBinding.fullscreen.visibility = View.GONE
playerBinding.closeImageButton.setOnClickListener {
finish()
}
binding.player.initialize(
supportFragmentManager,
null,
binding.doubleTapOverlay.binding,
null
)
}
private fun playVideo() {
val downloadDir = File(
getExternalFilesDir(null),
"video"
)
val file = File(
downloadDir,
fileName
)
setMediaSource(
Uri.fromFile(file)
)
player.prepare()
player.play()
}
private fun setMediaSource(uri: Uri) {
val userAgent = Util.getUserAgent(applicationContext, BuildConfig.APPLICATION_ID)
val dataSourceFactory: DataSource.Factory =
DefaultDataSourceFactory(applicationContext, userAgent)
val videoItem: MediaItem = MediaItem.Builder()
.setUri(uri)
.build()
val videoSource: MediaSource =
DefaultMediaSourceFactory(dataSourceFactory)
.createMediaSource(videoItem)
/*
val audioSource: MediaSource =
ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(audioUrl))
val mergeSource: MediaSource =
MergingMediaSource(videoSource, audioSource)
player.setMediaSource(mergeSource)
*/
player.setMediaSource(videoSource)
}
private fun hideSystemBars() {
window?.decorView?.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)
window.statusBarColor = Color.TRANSPARENT
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
)
val windowInsetsController =
WindowCompat.getInsetsController(window, window.decorView)
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController.hide(WindowInsetsCompat.Type.statusBars())
supportActionBar?.hide()
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}
}

View File

@ -1,12 +1,13 @@
package com.github.libretube.adapters
import android.annotation.SuppressLint
import android.util.Log
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.activities.OfflinePlayerActivity
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DownloadedMediaRowBinding
import com.github.libretube.extensions.TAG
import java.io.File
class DownloadsAdapter(
@ -27,6 +28,12 @@ class DownloadsAdapter(
holder.binding.apply {
fileName.text = file.name
fileSize.text = "${file.length() / (1024 * 1024)} MiB"
root.setOnClickListener {
val intent = Intent(root.context, OfflinePlayerActivity::class.java).also {
it.putExtra(IntentData.fileName, file.name)
}
root.context.startActivity(intent)
}
}
}

View File

@ -7,4 +7,5 @@ object IntentData {
const val playlistId = "playlistId"
const val timeStamp = "timeStamp"
const val position = "position"
const val fileName = "fileName"
}

View File

@ -1,7 +1,6 @@
package com.github.libretube.fragments
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -9,7 +8,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.adapters.DownloadsAdapter
import com.github.libretube.databinding.FragmentDownloadsBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.TAG
import java.io.File
class DownloadsFragment : BaseFragment() {

View File

@ -85,9 +85,9 @@ internal class CustomExoPlayerView(
fun initialize(
childFragmentManager: FragmentManager,
playerViewInterface: OnlinePlayerOptionsInterface,
playerViewInterface: OnlinePlayerOptionsInterface?,
doubleTapOverlayBinding: DoubleTapOverlayBinding,
trackSelector: TrackSelector
trackSelector: TrackSelector?
) {
this.childFragmentManager = childFragmentManager
this.onlinePlayerOptionsInterface = playerViewInterface

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.libretube.views.CustomExoPlayerView
android:id="@+id/player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
app:show_buffering="when_playing">
<com.github.libretube.views.DoubleTapOverlay
android:id="@+id/doubleTapOverlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" />
</com.github.libretube.views.CustomExoPlayerView>
</LinearLayout>