Skip to content

Commit

Permalink
fix: changing subtitle type now works without app restart
Browse files Browse the repository at this point in the history
fix: subtitle view margins get adjusted for .ass subtitles
  • Loading branch information
Adolar0042 committed Oct 17, 2022
1 parent 06568cb commit 4bded6d
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions app/src/main/java/ani/saikou/anime/ExoplayerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import androidx.media.session.MediaButtonReceiver
import ani.saikou.*
import ani.saikou.R
import ani.saikou.anilist.Anilist
import ani.saikou.databinding.ActivityExoplayerBinding
import ani.saikou.media.Media
Expand All @@ -61,11 +62,8 @@ import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
import com.google.android.exoplayer2.ui.CaptionStyleCompat
import com.google.android.exoplayer2.ui.*
import com.google.android.exoplayer2.ui.CaptionStyleCompat.*
import com.google.android.exoplayer2.ui.StyledPlayerView
import com.google.android.exoplayer2.ui.TrackSelectionDialogBuilder
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.HttpDataSource
import com.google.android.exoplayer2.upstream.cache.CacheDataSource
Expand Down Expand Up @@ -101,6 +99,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
private lateinit var exoSource: ImageButton
private lateinit var exoSettings: ImageButton
private lateinit var exoSubtitle: ImageButton
private lateinit var exoSubtitleView: SubtitleView
private lateinit var exoRotate: ImageButton
private lateinit var exoQuality: ImageButton
private lateinit var exoSpeed: ImageButton
Expand Down Expand Up @@ -304,6 +303,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
exoSource = playerView.findViewById(R.id.exo_source)
exoSettings = playerView.findViewById(R.id.exo_settings)
exoSubtitle = playerView.findViewById(R.id.exo_sub)
exoSubtitleView = playerView.findViewById(R.id.exo_subtitles)
exoRotate = playerView.findViewById(R.id.exo_rotate)
exoSpeed = playerView.findViewById(R.id.exo_playback_speed)
exoScreen = playerView.findViewById(R.id.exo_screen)
Expand Down Expand Up @@ -748,6 +748,15 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
epChanging = !it
}

// Subtitle View Margin fix for Kamyroll
if(model.watchSources!!.names[media.selected!!.source] == "Kamyroll" && settings.kamySubType == 0){
val marginInt = -19 // This gets rounded to -18dp
val margin = (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, marginInt.toFloat(), getResources().getDisplayMetrics())).roundToInt()
exoSubtitleView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = margin
}
}

//Anime Title
animeTitle.text = media.userPreferredName

Expand Down Expand Up @@ -1022,12 +1031,30 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
subClick()
}

var kamySubUrl = ""
if(subtitle != null){
// Change Kamyroll Subtitle Type
if (model.watchSources!!.names[media.selected!!.source] == "Kamyroll") {
if (settings.kamySubType == 0) kamySubUrl =
subtitle!!.url.url.replace("&out=vtt", "&out=ass").replace("&out=srt", "&out=ass")
if (settings.kamySubType == 1) kamySubUrl =
subtitle!!.url.url.replace("&out=ass", "&out=vtt").replace("&out=srt", "&out=vtt")
if (settings.kamySubType == 2) kamySubUrl =
subtitle!!.url.url.replace("&out=ass", "&out=srt").replace("&out=vtt", "&out=srt")
}
}

val sub = if (subtitle != null)
MediaItem.SubtitleConfiguration
.Builder(Uri.parse(subtitle!!.url.url))
.Builder(Uri.parse(if(kamySubUrl != "") kamySubUrl else subtitle!!.url.url))
.setSelectionFlags(C.SELECTION_FLAG_FORCED)
.setMimeType(
when (subtitle?.type) {
if(kamySubUrl != "") when(settings.kamySubType){
0 -> MimeTypes.TEXT_SSA
1 -> MimeTypes.TEXT_VTT
2 -> MimeTypes.APPLICATION_SUBRIP
else -> MimeTypes.TEXT_UNKNOWN
} else when (subtitle?.type) {
SubtitleType.VTT -> MimeTypes.TEXT_VTT
SubtitleType.ASS -> MimeTypes.TEXT_SSA
SubtitleType.SRT -> MimeTypes.APPLICATION_SUBRIP
Expand Down

0 comments on commit 4bded6d

Please sign in to comment.