Merge pull request #105 from AbduAmeen/title_description_seperation

Added separation of title, description and views/date
This commit is contained in:
Farbod 2022-04-04 00:43:26 -07:00 committed by GitHub
commit 3d822947fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 287 additions and 248 deletions

View File

@ -4,7 +4,6 @@ import android.app.Activity
import android.content.Context import android.content.Context
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.content.res.Configuration import android.content.res.Configuration
import android.content.res.Resources
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -13,7 +12,6 @@ import android.os.Handler
import android.util.Log import android.util.Log
import android.view.* import android.view.*
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.FrameLayout
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
@ -21,21 +19,12 @@ import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.core.view.ViewCompat
import androidx.core.view.ViewCompat.getWindowInsetsController
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.view.WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.NavigationUI.onNavDestinationSelected
import androidx.navigation.ui.setupWithNavController import androidx.navigation.ui.setupWithNavController
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.material.color.DynamicColors import com.google.android.material.color.DynamicColors
import java.lang.Exception import java.lang.Exception
@ -44,10 +33,11 @@ class MainActivity : AppCompatActivity() {
lateinit var bottomNavigationView: BottomNavigationView lateinit var bottomNavigationView: BottomNavigationView
lateinit var toolbar: Toolbar lateinit var toolbar: Toolbar
lateinit var navController : NavController lateinit var navController : NavController
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
RetrofitInstance.url=sharedPreferences.getString("instance", "https://pipedapi.kavin.rocks/")!! RetrofitInstance.url = sharedPreferences.getString("instance", "https://pipedapi.kavin.rocks/")!!
DynamicColors.applyToActivitiesIfAvailable(application) DynamicColors.applyToActivitiesIfAvailable(application)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
when (sharedPreferences.getString("theme_togglee", "A")!!) { when (sharedPreferences.getString("theme_togglee", "A")!!) {

View File

@ -26,6 +26,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
@ -53,7 +54,6 @@ import java.io.IOException
import java.net.URLEncoder import java.net.URLEncoder
import kotlin.math.abs import kotlin.math.abs
var isFullScreen = false var isFullScreen = false
class PlayerFragment : Fragment() { class PlayerFragment : Fragment() {
@ -61,12 +61,12 @@ class PlayerFragment : Fragment() {
private var videoId: String? = null private var videoId: String? = null
private var param2: String? = null private var param2: String? = null
private var lastProgress: Float = 0.toFloat() private var lastProgress: Float = 0.toFloat()
private var sId: Int=0 private var sId: Int = 0
private var eId: Int=0 private var eId: Int = 0
private var paused =false private var paused = false
private var whichQuality = 0 private var whichQuality = 0
var isSubscribed: Boolean =false var isSubscribed: Boolean = false
private lateinit var relatedRecView: RecyclerView private lateinit var relatedRecView: RecyclerView
private lateinit var exoPlayerView: StyledPlayerView private lateinit var exoPlayerView: StyledPlayerView
@ -84,7 +84,8 @@ class PlayerFragment : Fragment() {
} }
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
// Inflate the layout for this fragment // Inflate the layout for this fragment
@ -94,48 +95,47 @@ class PlayerFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
hideKeyboard() hideKeyboard()
videoId = videoId!!.replace("/watch?v=","")
val playerDescription = view.findViewById<TextView>(R.id.player_description)
videoId = videoId!!.replace("/watch?v=", "")
relDownloadVideo = view.findViewById(R.id.relPlayer_download) relDownloadVideo = view.findViewById(R.id.relPlayer_download)
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.findViewById<FrameLayout>(R.id.container).visibility=View.VISIBLE mainActivity.findViewById<FrameLayout>(R.id.container).visibility = View.VISIBLE
val playerMotionLayout = view.findViewById<MotionLayout>(R.id.playerMotionLayout) val playerMotionLayout = view.findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout = playerMotionLayout motionLayout = playerMotionLayout
exoPlayerView = view.findViewById(R.id.player) exoPlayerView = view.findViewById(R.id.player)
view.findViewById<TextView>(R.id.player_description).text = videoId view.findViewById<TextView>(R.id.player_description).text = videoId
playerMotionLayout.addTransitionListener(object: MotionLayout.TransitionListener { playerMotionLayout.addTransitionListener(object : MotionLayout.TransitionListener {
override fun onTransitionStarted( override fun onTransitionStarted(
motionLayout: MotionLayout?, motionLayout: MotionLayout?,
startId: Int, startId: Int,
endId: Int endId: Int
) { ) {
} }
override fun onTransitionChange(motionLayout: MotionLayout?, startId: Int, endId: Int, progress: Float) { override fun onTransitionChange(motionLayout: MotionLayout?, startId: Int, endId: Int, progress: Float) {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
val mainMotionLayout = mainActivity.findViewById<MotionLayout>(R.id.mainMotionLayout) val mainMotionLayout = mainActivity.findViewById<MotionLayout>(R.id.mainMotionLayout)
mainMotionLayout.progress = abs(progress) mainMotionLayout.progress = abs(progress)
eId=endId eId = endId
sId=startId sId = startId
} }
override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) { override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
println(currentId) println(currentId)
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
val mainMotionLayout = mainActivity.findViewById<MotionLayout>(R.id.mainMotionLayout) val mainMotionLayout = mainActivity.findViewById<MotionLayout>(R.id.mainMotionLayout)
if (currentId==eId) { if (currentId == eId) {
view.findViewById<ImageButton>(R.id.quality_select).visibility =View.GONE view.findViewById<ImageButton>(R.id.quality_select).visibility = View.GONE
view.findViewById<ImageButton>(R.id.close_imageButton).visibility =View.GONE view.findViewById<ImageButton>(R.id.close_imageButton).visibility = View.GONE
view.findViewById<TextView>(R.id.quality_text).visibility =View.GONE view.findViewById<TextView>(R.id.quality_text).visibility = View.GONE
mainMotionLayout.progress = 1.toFloat() mainMotionLayout.progress = 1.toFloat()
}else if(currentId==sId){ } else if (currentId == sId) {
view.findViewById<ImageButton>(R.id.quality_select).visibility =View.VISIBLE view.findViewById<ImageButton>(R.id.quality_select).visibility = View.VISIBLE
view.findViewById<ImageButton>(R.id.close_imageButton).visibility =View.VISIBLE view.findViewById<ImageButton>(R.id.close_imageButton).visibility = View.VISIBLE
view.findViewById<TextView>(R.id.quality_text).visibility =View.VISIBLE view.findViewById<TextView>(R.id.quality_text).visibility = View.VISIBLE
mainMotionLayout.progress = 0.toFloat() mainMotionLayout.progress = 0.toFloat()
} }
} }
override fun onTransitionTrigger( override fun onTransitionTrigger(
@ -144,23 +144,20 @@ class PlayerFragment : Fragment() {
positive: Boolean, positive: Boolean,
progress: Float progress: Float
) { ) {
} }
}) })
playerMotionLayout.progress=1.toFloat() playerMotionLayout.progress = 1.toFloat()
playerMotionLayout.transitionToStart() playerMotionLayout.transitionToStart()
fetchJson(view) fetchJson(view)
view.findViewById<ImageView>(R.id.close_imageView).setOnClickListener{ view.findViewById<ImageView>(R.id.close_imageView).setOnClickListener {
motionLayout.transitionToEnd() motionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
mainActivity.supportFragmentManager.beginTransaction() mainActivity.supportFragmentManager.beginTransaction()
.remove(this) .remove(this)
.commit() .commit()
} }
view.findViewById<ImageButton>(R.id.close_imageButton).setOnClickListener{ view.findViewById<ImageButton>(R.id.close_imageButton).setOnClickListener {
motionLayout.transitionToEnd() motionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
@ -169,51 +166,52 @@ class PlayerFragment : Fragment() {
.commit() .commit()
} }
val playImageView = view.findViewById<ImageView>(R.id.play_imageView) val playImageView = view.findViewById<ImageView>(R.id.play_imageView)
playImageView.setOnClickListener{ playImageView.setOnClickListener {
paused = if(paused){ paused = if (paused) {
playImageView.setImageResource(R.drawable.ic_pause) playImageView.setImageResource(R.drawable.ic_pause)
exoPlayer.play() exoPlayer.play()
false false
}else { } else {
playImageView.setImageResource(R.drawable.ic_play) playImageView.setImageResource(R.drawable.ic_play)
exoPlayer.pause() exoPlayer.pause()
true true
} }
} }
//FullScreen button trigger
view.findViewById<ImageButton>(R.id.fullscreen).setOnClickListener{ view.findViewById<ConstraintLayout>(R.id.player_title_layout).setOnClickListener {
//remember to hide everything when new thing added var visible = playerDescription.isVisible
if (!isFullScreen){
playerDescription.visibility = if (visible) View.GONE else View.VISIBLE
}
// FullScreen button trigger
view.findViewById<ImageButton>(R.id.fullscreen).setOnClickListener {
// remember to hide everything when new thing added
if (!isFullScreen) {
with(motionLayout) { with(motionLayout) {
getConstraintSet(R.id.start).constrainHeight(R.id.player, -1) getConstraintSet(R.id.start).constrainHeight(R.id.player, -1)
enableTransition(R.id.yt_transition,false) enableTransition(R.id.yt_transition, false)
} }
view.findViewById<ConstraintLayout>(R.id.main_container).isClickable =true view.findViewById<ConstraintLayout>(R.id.main_container).isClickable = true
view.findViewById<LinearLayout>(R.id.linLayout).visibility=View.GONE view.findViewById<LinearLayout>(R.id.linLayout).visibility = View.GONE
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
isFullScreen=true isFullScreen=true
}else{ }else{
with(motionLayout) { with(motionLayout) {
getConstraintSet(R.id.start).constrainHeight(R.id.player, 0) getConstraintSet(R.id.start).constrainHeight(R.id.player, 0)
enableTransition(R.id.yt_transition,true) enableTransition(R.id.yt_transition, true)
} }
view.findViewById<ConstraintLayout>(R.id.main_container).isClickable =false view.findViewById<ConstraintLayout>(R.id.main_container).isClickable = false
view.findViewById<LinearLayout>(R.id.linLayout).visibility=View.VISIBLE view.findViewById<LinearLayout>(R.id.linLayout).visibility = View.VISIBLE
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
isFullScreen=false isFullScreen = false
} }
} }
relatedRecView = view.findViewById(R.id.player_recView) relatedRecView = view.findViewById(R.id.player_recView)
relatedRecView.layoutManager = GridLayoutManager(view.context, resources.getInteger(R.integer.grid_items)) relatedRecView.layoutManager = GridLayoutManager(view.context, resources.getInteger(R.integer.grid_items))
} }
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
} }
@ -222,16 +220,15 @@ class PlayerFragment : Fragment() {
super.onDestroy() super.onDestroy()
try { try {
exoPlayer.stop() exoPlayer.stop()
}catch (e: Exception){} } catch (e: Exception) {}
} }
private fun fetchJson(view: View) { private fun fetchJson(view: View) {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.getStreams(videoId!!) RetrofitInstance.api.getStreams(videoId!!)
} catch(e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
Toast.makeText(context,R.string.unknown_error, Toast.LENGTH_SHORT).show() Toast.makeText(context,R.string.unknown_error, Toast.LENGTH_SHORT).show()
@ -243,17 +240,20 @@ class PlayerFragment : Fragment() {
} }
var videosNameArray: Array<CharSequence> = arrayOf() var videosNameArray: Array<CharSequence> = arrayOf()
videosNameArray += "HLS" videosNameArray += "HLS"
for (vid in response.videoStreams!!){ for (vid in response.videoStreams!!) {
val name = vid.quality +" "+ vid.format val name = vid.quality + " " + vid.format
videosNameArray += name videosNameArray += name
} }
runOnUiThread { runOnUiThread {
var subtitle = mutableListOf<SubtitleConfiguration>() var subtitle = mutableListOf<SubtitleConfiguration>()
if(response.subtitles!!.isNotEmpty()){ if (response.subtitles!!.isNotEmpty()) {
subtitle?.add(SubtitleConfiguration.Builder(response.subtitles!![0].url!!.toUri()) subtitle?.add(
.setMimeType(response.subtitles!![0].mimeType!!) // The correct MIME type (required). SubtitleConfiguration.Builder(response.subtitles!![0].url!!.toUri())
.setLanguage(response.subtitles!![0].code) // The subtitle language (optional). .setMimeType(response.subtitles!![0].mimeType!!) // The correct MIME type (required).
.build())} .setLanguage(response.subtitles!![0].code) // The subtitle language (optional).
.build()
)
}
exoPlayer = ExoPlayer.Builder(view.context) exoPlayer = ExoPlayer.Builder(view.context)
.setSeekBackIncrementMs(5000) .setSeekBackIncrementMs(5000)
@ -262,7 +262,7 @@ class PlayerFragment : Fragment() {
exoPlayerView.setShowSubtitleButton(true) exoPlayerView.setShowSubtitleButton(true)
exoPlayerView.setShowNextButton(false) exoPlayerView.setShowNextButton(false)
exoPlayerView.setShowPreviousButton(false) exoPlayerView.setShowPreviousButton(false)
//exoPlayerView.controllerShowTimeoutMs = 1500 // exoPlayerView.controllerShowTimeoutMs = 1500
exoPlayerView.controllerHideOnTouch = true exoPlayerView.controllerHideOnTouch = true
exoPlayerView.player = exoPlayer exoPlayerView.player = exoPlayer
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
@ -284,13 +284,13 @@ class PlayerFragment : Fragment() {
.createMediaSource(videoItem) .createMediaSource(videoItem)
var audioSource: MediaSource = DefaultMediaSourceFactory(dataSourceFactory) var audioSource: MediaSource = DefaultMediaSourceFactory(dataSourceFactory)
.createMediaSource(fromUri(response.audioStreams!![0].url!!)) .createMediaSource(fromUri(response.audioStreams!![0].url!!))
if (response.videoStreams[index].quality=="720p" || response.videoStreams[index].quality=="1080p" || response.videoStreams[index].quality=="480p" ){ if (response.videoStreams[index].quality == "720p" || response.videoStreams[index].quality == "1080p" || response.videoStreams[index].quality == "480p") {
audioSource = ProgressiveMediaSource.Factory(dataSourceFactory) audioSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(fromUri(response.audioStreams!![getMostBitRate(response.audioStreams)].url!!)) .createMediaSource(fromUri(response.audioStreams!![getMostBitRate(response.audioStreams)].url!!))
} }
val mergeSource: MediaSource = MergingMediaSource(videoSource,audioSource) val mergeSource: MediaSource = MergingMediaSource(videoSource, audioSource)
exoPlayer.setMediaSource(mergeSource) exoPlayer.setMediaSource(mergeSource)
view.findViewById<TextView>(R.id.quality_text).text=videosNameArray[index+1] view.findViewById<TextView>(R.id.quality_text).text = videosNameArray[index + 1]
return@lit return@lit
}else if (index+1 == response.videoStreams.size){ }else if (index+1 == response.videoStreams.size){
val mediaItem: MediaItem = MediaItem.Builder() val mediaItem: MediaItem = MediaItem.Builder()
@ -301,7 +301,6 @@ class PlayerFragment : Fragment() {
} }
} }
} }
} }
response.hls != null -> { response.hls != null -> {
val mediaItem: MediaItem = MediaItem.Builder() val mediaItem: MediaItem = MediaItem.Builder()
@ -321,32 +320,34 @@ class PlayerFragment : Fragment() {
.createMediaSource(videoItem) .createMediaSource(videoItem)
var audioSource: MediaSource = DefaultMediaSourceFactory(dataSourceFactory) var audioSource: MediaSource = DefaultMediaSourceFactory(dataSourceFactory)
.createMediaSource(fromUri(response.audioStreams!![0].url!!)) .createMediaSource(fromUri(response.audioStreams!![0].url!!))
if (response.videoStreams[0].quality=="720p" || response.videoStreams[0].quality=="1080p" || response.videoStreams[0].quality=="480p" ){ if (response.videoStreams[0].quality == "720p" || response.videoStreams[0].quality == "1080p" || response.videoStreams[0].quality == "480p") {
audioSource = ProgressiveMediaSource.Factory(dataSourceFactory) audioSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(fromUri(response.audioStreams!![getMostBitRate(response.audioStreams)].url!!)) .createMediaSource(fromUri(response.audioStreams!![getMostBitRate(response.audioStreams)].url!!))
} }
val mergeSource: MediaSource = MergingMediaSource(videoSource,audioSource) val mergeSource: MediaSource = MergingMediaSource(videoSource, audioSource)
exoPlayer.setMediaSource(mergeSource) exoPlayer.setMediaSource(mergeSource)
view.findViewById<TextView>(R.id.quality_text).text=videosNameArray[1] view.findViewById<TextView>(R.id.quality_text).text = videosNameArray[1]
} }
} }
///exoPlayer.getMediaItemAt(5) // /exoPlayer.getMediaItemAt(5)
exoPlayer.prepare() exoPlayer.prepare()
exoPlayer.play() exoPlayer.play()
view.findViewById<TextView>(R.id.title_textView).text = response.title view.findViewById<TextView>(R.id.player_title).text = response.title
view.findViewById<TextView>(R.id.player_description).text = response.description
view.findViewById<ImageButton>(R.id.quality_select).setOnClickListener{ view.findViewById<ImageButton>(R.id.quality_select).setOnClickListener {
//Dialog for quality selection // Dialog for quality selection
val builder: AlertDialog.Builder? = activity?.let { val builder: AlertDialog.Builder? = activity?.let {
AlertDialog.Builder(it) AlertDialog.Builder(it)
} }
builder!!.setTitle(R.string.choose_quality_dialog) builder!!.setTitle(R.string.choose_quality_dialog)
.setItems(videosNameArray, .setItems(
videosNameArray,
DialogInterface.OnClickListener { _, which -> DialogInterface.OnClickListener { _, which ->
whichQuality = which whichQuality = which
if(response.subtitles!!.isNotEmpty()) { if (response.subtitles!!.isNotEmpty()) {
var subtitle = var subtitle =
mutableListOf<SubtitleConfiguration>() mutableListOf<SubtitleConfiguration>()
subtitle?.add( subtitle?.add(
@ -356,41 +357,44 @@ class PlayerFragment : Fragment() {
.build() .build()
) )
} }
if(which==0){ if (which == 0) {
val mediaItem: MediaItem = MediaItem.Builder() val mediaItem: MediaItem = MediaItem.Builder()
.setUri(response.hls) .setUri(response.hls)
.setSubtitleConfigurations(subtitle) .setSubtitleConfigurations(subtitle)
.build() .build()
exoPlayer.setMediaItem(mediaItem) exoPlayer.setMediaItem(mediaItem)
}else{ } else {
val dataSourceFactory: DataSource.Factory = val dataSourceFactory: DataSource.Factory =
DefaultHttpDataSource.Factory() DefaultHttpDataSource.Factory()
val videoItem: MediaItem = MediaItem.Builder() val videoItem: MediaItem = MediaItem.Builder()
.setUri(response.videoStreams[which-1].url) .setUri(response.videoStreams[which - 1].url)
.setSubtitleConfigurations(subtitle) .setSubtitleConfigurations(subtitle)
.build() .build()
val videoSource: MediaSource = DefaultMediaSourceFactory(dataSourceFactory) val videoSource: MediaSource = DefaultMediaSourceFactory(dataSourceFactory)
.createMediaSource(videoItem) .createMediaSource(videoItem)
var audioSource: MediaSource = DefaultMediaSourceFactory(dataSourceFactory) var audioSource: MediaSource = DefaultMediaSourceFactory(dataSourceFactory)
.createMediaSource(fromUri(response.audioStreams!![0].url!!)) .createMediaSource(fromUri(response.audioStreams!![0].url!!))
if (response.videoStreams[which-1].quality=="720p" || response.videoStreams[which-1].quality=="1080p" || response.videoStreams[which-1].quality=="480p" ){ if (response.videoStreams[which - 1].quality == "720p" || response.videoStreams[which - 1].quality == "1080p" || response.videoStreams[which - 1].quality == "480p") {
audioSource = ProgressiveMediaSource.Factory(dataSourceFactory) audioSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(fromUri(response.audioStreams!![getMostBitRate(response.audioStreams)].url!!)) .createMediaSource(fromUri(response.audioStreams!![getMostBitRate(response.audioStreams)].url!!))
} }
val mergeSource: MediaSource = MergingMediaSource(videoSource,audioSource) val mergeSource: MediaSource = MergingMediaSource(videoSource, audioSource)
exoPlayer.setMediaSource(mergeSource) exoPlayer.setMediaSource(mergeSource)
} }
view.findViewById<TextView>(R.id.quality_text).text=videosNameArray[which] view.findViewById<TextView>(R.id.quality_text).text = videosNameArray[which]
}) }
)
val dialog: AlertDialog? = builder?.create() val dialog: AlertDialog? = builder?.create()
dialog?.show() dialog?.show()
} }
//Listener for play and pause icon change // Listener for play and pause icon change
exoPlayer!!.addListener(object : com.google.android.exoplayer2.Player.Listener { exoPlayer!!.addListener(object : com.google.android.exoplayer2.Player.Listener {
override fun onPlayerStateChanged(playWhenReady: Boolean,playbackState: Int) { override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
exoPlayerView.keepScreenOn = !(playbackState == Player.STATE_IDLE || playbackState == Player.STATE_ENDED || exoPlayerView.keepScreenOn = !(
!playWhenReady) playbackState == Player.STATE_IDLE || playbackState == Player.STATE_ENDED ||
!playWhenReady
)
if (playWhenReady && playbackState == Player.STATE_READY) { if (playWhenReady && playbackState == Player.STATE_READY) {
// media actually playing // media actually playing
@ -412,144 +416,145 @@ class PlayerFragment : Fragment() {
} else { } else {
Html.fromHtml(response.description) Html.fromHtml(response.description)
} }
view.findViewById<TextView>(R.id.player_sub).text = response.views.formatShort() + " views • "+response.uploadDate view.findViewById<TextView>(R.id.player_views_info).text = response.views.formatShort() + " views • " + response.uploadDate
view.findViewById<TextView>(R.id.textLike).text = response.likes.formatShort() view.findViewById<TextView>(R.id.textLike).text = response.likes.formatShort()
val channelImage = view.findViewById<ImageView>(R.id.player_channelImage) val channelImage = view.findViewById<ImageView>(R.id.player_channelImage)
Picasso.get().load(response.uploaderAvatar).into(channelImage) Picasso.get().load(response.uploaderAvatar).into(channelImage)
view.findViewById<TextView>(R.id.player_channelName).text=response.uploader view.findViewById<TextView>(R.id.player_channelName).text = response.uploader
view.findViewById<RelativeLayout>(R.id.player_channel).setOnClickListener { view.findViewById<RelativeLayout>(R.id.player_channel).setOnClickListener {
val activity = view.context as MainActivity val activity = view.context as MainActivity
val bundle = bundleOf("channel_id" to response.uploaderUrl) val bundle = bundleOf("channel_id" to response.uploaderUrl)
activity.navController.navigate(R.id.channel,bundle) activity.navController.navigate(R.id.channel, bundle)
activity.findViewById<MotionLayout>(R.id.mainMotionLayout).transitionToEnd() activity.findViewById<MotionLayout>(R.id.mainMotionLayout).transitionToEnd()
view.findViewById<MotionLayout>(R.id.playerMotionLayout).transitionToEnd() view.findViewById<MotionLayout>(R.id.playerMotionLayout).transitionToEnd()
} }
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
if(sharedPref?.getString("token","")!=""){ if (sharedPref?.getString("token", "") != "") {
val channelId = response.uploaderUrl?.replace("/channel/","") val channelId = response.uploaderUrl?.replace("/channel/", "")
val subButton = view.findViewById<MaterialButton>(R.id.player_subscribe) val subButton = view.findViewById<MaterialButton>(R.id.player_subscribe)
isSubscribed(subButton, channelId!!) isSubscribed(subButton, channelId!!)
} }
//share button // share button
view.findViewById<RelativeLayout>(R.id.relPlayer_share).setOnClickListener { view.findViewById<RelativeLayout>(R.id.relPlayer_share).setOnClickListener {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
val intent= Intent() val intent = Intent()
intent.action=Intent.ACTION_SEND intent.action = Intent.ACTION_SEND
var url = "https://piped.kavin.rocks/watch?v=$videoId" var url = "https://piped.kavin.rocks/watch?v=$videoId"
val instance = sharedPreferences.getString("instance", "https://pipedapi.kavin.rocks")!! val instance = sharedPreferences.getString("instance", "https://pipedapi.kavin.rocks")!!
if (instance != "https://pipedapi.kavin.rocks") if (instance != "https://pipedapi.kavin.rocks")
url += "&instance=${URLEncoder.encode(instance, "UTF-8")}" url += "&instance=${URLEncoder.encode(instance, "UTF-8")}"
intent.putExtra(Intent.EXTRA_TEXT, url) intent.putExtra(Intent.EXTRA_TEXT, url)
intent.type="text/plain" intent.type = "text/plain"
startActivity(Intent.createChooser(intent,"Share Url To:")) startActivity(Intent.createChooser(intent, "Share Url To:"))
} }
//check if livestream // check if livestream
if (response.duration!!>0){ if (response.duration!!> 0) {
//download clicked // download clicked
relDownloadVideo.setOnClickListener { relDownloadVideo.setOnClickListener {
if(!IS_DOWNLOAD_RUNNING){ if (!IS_DOWNLOAD_RUNNING) {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
Log.e(TAG,"download button clicked!") Log.e(TAG, "download button clicked!")
if (SDK_INT >= Build.VERSION_CODES.R) { if (SDK_INT >= Build.VERSION_CODES.R) {
Log.d("myz", "" + SDK_INT) Log.d("myz", "" + SDK_INT)
if (!Environment.isExternalStorageManager()) { if (!Environment.isExternalStorageManager()) {
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
mainActivity, arrayOf( mainActivity,
Manifest.permission.READ_EXTERNAL_STORAGE, arrayOf(
Manifest.permission.MANAGE_EXTERNAL_STORAGE Manifest.permission.READ_EXTERNAL_STORAGE,
), 1 Manifest.permission.MANAGE_EXTERNAL_STORAGE
) //permission request code is just an int ),
} 1
} else { ) // permission request code is just an int
if (ActivityCompat.checkSelfPermission( }
requireContext(), } else {
Manifest.permission.READ_EXTERNAL_STORAGE if (ActivityCompat.checkSelfPermission(
) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( requireContext(),
requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE
Manifest.permission.WRITE_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
) != PackageManager.PERMISSION_GRANTED requireContext(),
) { Manifest.permission.WRITE_EXTERNAL_STORAGE
ActivityCompat.requestPermissions( ) != PackageManager.PERMISSION_GRANTED
mainActivity, ) {
arrayOf( ActivityCompat.requestPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE, mainActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE arrayOf(
), Manifest.permission.READ_EXTERNAL_STORAGE,
1 Manifest.permission.WRITE_EXTERNAL_STORAGE
) ),
1
)
}
}
var vidName = arrayListOf<String>()
vidName.add("No video")
var vidUrl = arrayListOf<String>()
vidUrl.add("")
for (vid in response.videoStreams!!) {
val name = vid.quality + " " + vid.format
vidName.add(name)
vidUrl.add(vid.url!!)
}
var audioName = arrayListOf<String>()
audioName.add("No audio")
var audioUrl = arrayListOf<String>()
audioUrl.add("")
for (audio in response.audioStreams!!) {
val name = audio.quality + " " + audio.format
audioName.add(name)
audioUrl.add(audio.url!!)
}
val newFragment = DownloadDialog()
var bundle = Bundle()
bundle.putStringArrayList("videoName", vidName)
bundle.putStringArrayList("videoUrl", vidUrl)
bundle.putStringArrayList("audioName", audioName)
bundle.putStringArrayList("audioUrl", audioUrl)
bundle.putString("videoId", videoId)
bundle.putInt("duration", response.duration)
newFragment.arguments = bundle
newFragment.show(childFragmentManager, "Download")
} else {
Toast.makeText(context, R.string.dlisinprogress, Toast.LENGTH_SHORT)
.show()
} }
} }
var vidName = arrayListOf<String>() } else {
vidName.add("No video") Toast.makeText(context, R.string.cannotDownload, Toast.LENGTH_SHORT).show()
var vidUrl = arrayListOf<String>() }
vidUrl.add("") if (response.hls != null) {
for (vid in response.videoStreams!!){ view.findViewById<RelativeLayout>(R.id.relPlayer_vlc).setOnClickListener {
val name = vid.quality +" "+ vid.format exoPlayer.pause()
vidName.add(name) try {
vidUrl.add(vid.url!!) val vlcRequestCode = 42
} val uri: Uri = Uri.parse(response.hls)
var audioName = arrayListOf<String>() val vlcIntent = Intent(Intent.ACTION_VIEW)
audioName.add("No audio") vlcIntent.setPackage("org.videolan.vlc")
var audioUrl = arrayListOf<String>() vlcIntent.setDataAndTypeAndNormalize(uri, "video/*")
audioUrl.add("") vlcIntent.putExtra("title", response.title)
for (audio in response.audioStreams!!){ vlcIntent.putExtra("from_start", false)
val name = audio.quality +" "+ audio.format vlcIntent.putExtra("position", exoPlayer.currentPosition)
audioName.add(name) startActivityForResult(vlcIntent, vlcRequestCode)
audioUrl.add(audio.url!!) } catch (e: Exception) {
} Toast.makeText(context, R.string.vlcerror, Toast.LENGTH_SHORT)
val newFragment = DownloadDialog() .show()
var bundle = Bundle() }
bundle.putStringArrayList("videoName",vidName) }
bundle.putStringArrayList("videoUrl",vidUrl)
bundle.putStringArrayList("audioName",audioName)
bundle.putStringArrayList("audioUrl",audioUrl)
bundle.putString("videoId",videoId)
bundle.putInt("duration",response.duration)
newFragment.arguments = bundle
newFragment.show(childFragmentManager, "Download")
}else{
Toast.makeText(context, R.string.dlisinprogress, Toast.LENGTH_SHORT)
.show()
}
}
}else{
Toast.makeText(context,R.string.cannotDownload, Toast.LENGTH_SHORT).show()
} }
if (response.hls!=null){
view.findViewById<RelativeLayout>(R.id.relPlayer_vlc).setOnClickListener {
exoPlayer.pause()
try{
val vlcRequestCode = 42
val uri: Uri = Uri.parse(response.hls)
val vlcIntent = Intent(Intent.ACTION_VIEW)
vlcIntent.setPackage("org.videolan.vlc")
vlcIntent.setDataAndTypeAndNormalize(uri, "video/*")
vlcIntent.putExtra("title", response.title)
vlcIntent.putExtra("from_start", false)
vlcIntent.putExtra("position", exoPlayer.currentPosition)
startActivityForResult(vlcIntent, vlcRequestCode)
}catch(e: Exception){
Toast.makeText(context, R.string.vlcerror, Toast.LENGTH_SHORT)
.show()
}
}}
} }
} }
} }
run() run()
} }
private fun isSubscribed(button: MaterialButton, channel_id: String){ private fun isSubscribed(button: MaterialButton, channel_id: String) {
@SuppressLint("ResourceAsColor") @SuppressLint("ResourceAsColor")
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
RetrofitInstance.api.isSubscribed(channel_id,sharedPref?.getString("token","")!!) RetrofitInstance.api.isSubscribed(channel_id, sharedPref?.getString("token", "")!!)
}catch(e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated return@launchWhenCreated
@ -561,46 +566,48 @@ class PlayerFragment : Fragment() {
(context as Activity).theme.resolveAttribute( (context as Activity).theme.resolveAttribute(
android.R.attr.colorPrimary, android.R.attr.colorPrimary,
colorPrimary, colorPrimary,
true) true
)
val ColorText = TypedValue() val ColorText = TypedValue()
(context as Activity).theme.resolveAttribute( (context as Activity).theme.resolveAttribute(
R.attr.colorOnSurface, R.attr.colorOnSurface,
ColorText, ColorText,
true) true
)
runOnUiThread { runOnUiThread {
if (response.subscribed==true){ if (response.subscribed == true) {
isSubscribed=true isSubscribed = true
button.text=getString(R.string.unsubscribe) button.text = getString(R.string.unsubscribe)
button.setTextColor(ColorText.data) button.setTextColor(ColorText.data)
} }
if(response.subscribed!=null){ if (response.subscribed != null) {
button.setOnClickListener { button.setOnClickListener {
if(isSubscribed){ if (isSubscribed) {
unsubscribe(channel_id) unsubscribe(channel_id)
button.text=getString(R.string.subscribe) button.text = getString(R.string.subscribe)
button.setTextColor(colorPrimary.data) button.setTextColor(colorPrimary.data)
} else {
}else{ subscribe(channel_id)
subscribe(channel_id) button.text = getString(R.string.unsubscribe)
button.text=getString(R.string.unsubscribe) button.setTextColor(colorPrimary.data)
button.setTextColor(colorPrimary.data) }
} }
}} }
} }
} }
} }
run() run()
} }
private fun subscribe(channel_id: String){ private fun subscribe(channel_id: String) {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
RetrofitInstance.api.subscribe(sharedPref?.getString("token","")!!, Subscribe(channel_id)) RetrofitInstance.api.subscribe(sharedPref?.getString("token", "")!!, Subscribe(channel_id))
}catch(e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated return@launchWhenCreated
@ -608,18 +615,18 @@ class PlayerFragment : Fragment() {
Log.e(TAG, "HttpException, unexpected response$e") Log.e(TAG, "HttpException, unexpected response$e")
return@launchWhenCreated return@launchWhenCreated
} }
isSubscribed=true isSubscribed = true
} }
} }
run() run()
} }
private fun unsubscribe(channel_id: String){ private fun unsubscribe(channel_id: String) {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
RetrofitInstance.api.unsubscribe(sharedPref?.getString("token","")!!, Subscribe(channel_id)) RetrofitInstance.api.unsubscribe(sharedPref?.getString("token", "")!!, Subscribe(channel_id))
}catch(e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated return@launchWhenCreated
@ -627,26 +634,25 @@ class PlayerFragment : Fragment() {
Log.e(TAG, "HttpException, unexpected response") Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated return@launchWhenCreated
} }
isSubscribed=false isSubscribed = false
} }
} }
run() run()
} }
private fun Fragment?.runOnUiThread(action: () -> Unit) { private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return this ?: return
if (!isAdded) return // Fragment not attached to an Activity if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action) activity?.runOnUiThread(action)
} }
private fun getMostBitRate(audios: List<PipedStream>):Int{ private fun getMostBitRate(audios: List<PipedStream>): Int {
var bitrate =0 var bitrate = 0
var index = 0 var index = 0
for ((i, audio) in audios.withIndex()){ for ((i, audio) in audios.withIndex()) {
val q = audio.quality!!.replace(" kbps","").toInt() val q = audio.quality!!.replace(" kbps", "").toInt()
if (q>bitrate){ if (q> bitrate) {
bitrate=q bitrate = q
index = i index = i
} }
} }
@ -656,5 +662,4 @@ class PlayerFragment : Fragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
} }
}
}

View File

@ -29,24 +29,68 @@
> >
<TextView <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/player_description" android:id="@+id/player_info_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text=""
android:textSize="15sp"
android:layout_margin="7dp"
android:textStyle="bold" />
<TextView
android:id="@+id/player_sub"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content">
android:paddingLeft="8dp"
android:paddingRight="8dp" <androidx.constraintlayout.widget.ConstraintLayout
android:text="" /> android:id="@+id/player_title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/player_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="Title"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/player_description_arrow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/player_views_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="TextView"
android:textSize="12sp"
app:layout_constraintEnd_toStartOf="@id/player_description_arrow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/player_title" />
<ImageView
android:id="@+id/player_description_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:paddingLeft="16dp"
android:src="@drawable/ic_arrow_down"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/player_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/player_title_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"