Skip to content

Commit

Permalink
Working on fixing player
Browse files Browse the repository at this point in the history
  • Loading branch information
kl3jvi committed Jan 6, 2022
1 parent 537fe85 commit e1390bd
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 69 deletions.
Binary file removed app/release/app-x86_64-release.apk
Binary file not shown.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
android:name=".application.AnimityApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MySplash">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class EpisodeInfo(
var vidCdnUrl: String? = null,
var nextEpisodeUrl: String? = null,
var previousEpisodeUrl: String? = null
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class PlayerRepositoryImpl @Inject constructor(
}

override suspend fun fetchM3u8Url(header: Map<String, String>, url: String): String {
return parser.parseM3U8Url(apiClient.fetchM3u8Url(header, url).string()) ?: ""
return parser.parseM3U8Url(apiClient.fetchM3u8Url(header, url).string())?:""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import com.kl3jvi.animity.utils.parser.HtmlParser
interface PlayerRepository {
val parser: HtmlParser
suspend fun fetchEpisodeMediaUrl(header: Map<String, String>, url: String): EpisodeInfo
suspend fun fetchM3u8Url(header: Map<String, String>, url: String):String?
suspend fun fetchM3u8Url(header: Map<String, String>, url: String):String
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import com.google.android.exoplayer2.trackselection.MappingTrackSelector
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
import com.google.android.exoplayer2.ui.TrackSelectionDialogBuilder
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.util.MimeTypes
import com.google.android.exoplayer2.util.Util
import com.google.android.material.snackbar.Snackbar
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import com.kl3jvi.animity.R
import com.kl3jvi.animity.databinding.ActivityPlayerBinding
import com.kl3jvi.animity.data.model.Content
import com.kl3jvi.animity.data.model.EpisodeModel
import com.kl3jvi.animity.databinding.ActivityPlayerBinding
import com.kl3jvi.animity.utils.Constants
import com.kl3jvi.animity.utils.Constants.Companion.getDataSourceFactory
import com.kl3jvi.animity.utils.Constants.Companion.showSnack
Expand Down Expand Up @@ -69,7 +69,7 @@ class PlayerActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(viewBinding.root)
firebaseAnalytics = Firebase.analytics
savedInstanceState?.putString("test","12324")
savedInstanceState?.putString("test", "12324")
if (intent.hasExtra(Constants.EPISODE_DETAILS)) {
val getIntentData = intent.getParcelableExtra<EpisodeModel>(Constants.EPISODE_DETAILS)
animeTitlePassed = intent.getStringExtra(Constants.ANIME_TITLE).toString()
Expand All @@ -84,7 +84,7 @@ class PlayerActivity : AppCompatActivity() {

initialisePlayerLayout()
viewModel.updateEpisodeUrl(getIntentData?.episodeUrl.toString())
Log.e("Playback position",playbackPosition.toString())
Log.e("Playback position", playbackPosition.toString())
}
}

Expand Down Expand Up @@ -131,11 +131,94 @@ class PlayerActivity : AppCompatActivity() {

@ExperimentalCoroutinesApi
private fun initializePlayer() {
viewModel.videoUrlLiveData.observe(this, { res ->
// viewModel.videoUrlLiveData.observe(this, { res ->
// when (res) {
// is Resource.Success -> {
// val videoM3U8Url = "${res.data}"
// Log.e("Anime Url",res.data?:"akjdsfkadjsf")
// try {
// trackSelector = DefaultTrackSelector(this).apply {
// setParameters(buildUponParameters().setMaxVideoSizeSd())
// }
// val audioAttributes: AudioAttributes = AudioAttributes.Builder()
// .setUsage(C.USAGE_MEDIA)
// .setContentType(C.CONTENT_TYPE_MOVIE)
// .build()
//
// player = ExoPlayer.Builder(this)
// .setAudioAttributes(audioAttributes, true)
// .setTrackSelector(trackSelector!!)
// .setSeekBackIncrementMs(seekForwardSeconds)
// .setSeekForwardIncrementMs(seekForwardSeconds)
// .build()
// .also { exoPlayer ->
// viewBinding.videoView.player = exoPlayer
// val mdItem = MediaItem.fromUri(videoM3U8Url)
// val videoSource: MediaSource =
// buildMediaSource(mdItem)
// exoPlayer.setMediaSource(videoSource)
// exoPlayer.playWhenReady = playWhenReady
// exoPlayer.prepare()
// }
// viewModel.getPlaybackPosition(episodeUrlLocal).observe(this@PlayerActivity,{
// player?.seekTo(it)
// })
//
//
// player!!.addListener(object : Player.Listener {
// override fun onPlayerStateChanged(
// playWhenReady: Boolean,
// playbackState: Int
// ) {
// if (playbackState == ExoPlayer.STATE_READY) {
// val realDurationMillis: Long = player!!.duration
// content = Content().apply {
// episodeUrl = episodeUrlLocal
// animeName = animeTitlePassed
// episodeNumber = episodeNumberLocal
// watchedDuration = 0
// duration = realDurationMillis
// }
// }
// }
// })
//
//
// val skipIntro =
// viewBinding.videoView.findViewById<LinearLayout>(R.id.skipLayout)
// viewModel.audioProgress(player).observe(this, { currentProgress ->
// currentProgress?.let {
// currentTime = it
// if (currentTime < 300000) {
// skipIntro.setOnClickListener {
// player?.seekTo(currentTime + Constants.INTRO_SKIP_TIME)
// skipIntro.visibility = View.GONE
// }
// } else {
// skipIntro.visibility = View.GONE
// }
// }
// })
// } catch (e: ExoPlaybackException) {
// showSnack(viewBinding.root,e.localizedMessage)
// } catch (e: Exception) {
// e.printStackTrace()
// }
// viewBinding.loadingOverlay.visibility = View.GONE
// }
// is Resource.Loading -> {
// viewBinding.loadingOverlay.visibility = View.VISIBLE
// }
// is Resource.Error -> {
// showSnack(viewBinding.root,res.message)
// }
// }
// })

viewModel.videoUrlLiveData.observe(this) { res ->
when (res) {
is Resource.Success -> {
val videoM3U8Url = "${res.data}"
Log.e("Anime Url",res.data?:"akjdsfkadjsf")
val videoM3U8Url = res.data.toString()
try {
trackSelector = DefaultTrackSelector(this).apply {
setParameters(buildUponParameters().setMaxVideoSizeSd())
Expand All @@ -156,13 +239,15 @@ class PlayerActivity : AppCompatActivity() {
val mdItem = MediaItem.fromUri(videoM3U8Url)
val videoSource: MediaSource =
buildMediaSource(mdItem)

exoPlayer.setMediaSource(videoSource)
exoPlayer.playWhenReady = playWhenReady
exoPlayer.prepare()
}
viewModel.getPlaybackPosition(episodeUrlLocal).observe(this@PlayerActivity,{
player?.seekTo(it)
})
viewModel.getPlaybackPosition(episodeUrlLocal)
.observe(this@PlayerActivity, {
player?.seekTo(it)
})


player!!.addListener(object : Player.Listener {
Expand Down Expand Up @@ -199,21 +284,23 @@ class PlayerActivity : AppCompatActivity() {
}
}
})

} catch (e: ExoPlaybackException) {
showSnack(viewBinding.root,e.localizedMessage)
showSnack(viewBinding.root, e.localizedMessage)
} catch (e: Exception) {
e.printStackTrace()
}
viewBinding.loadingOverlay.visibility = View.GONE
}
is Resource.Loading -> {
viewBinding.loadingOverlay.visibility = View.VISIBLE

}
is Resource.Error -> {
showSnack(viewBinding.root,res.message)

}
is Resource.Loading -> {

}
}
})
}
}

override fun onBackPressed() {
Expand Down Expand Up @@ -292,7 +379,6 @@ class PlayerActivity : AppCompatActivity() {
}



private fun showDialogForSpeedSelection() {
val builder = AlertDialog.Builder(this, R.style.MaterialThemeDialog)
builder.apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kl3jvi.animity.ui.activities.player

import android.util.Log
import androidx.lifecycle.*
import com.google.android.exoplayer2.ExoPlayer
import com.kl3jvi.animity.data.model.Content
Expand All @@ -26,6 +27,7 @@ class PlayerViewModel @Inject constructor(

fun updateEpisodeUrl(vidUrl: String) {
_vidUrl.value = vidUrl
Log.e("videoUrl",vidUrl)
}

@ExperimentalCoroutinesApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.kl3jvi.animity.domain.use_cases.GetAnimeDetailsUseCase
import com.kl3jvi.animity.domain.use_cases.GetEpisodeInfoUseCase
import com.kl3jvi.animity.persistence.AnimeRepository
import com.kl3jvi.animity.persistence.EpisodeDao
import com.kl3jvi.animity.utils.pmap
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down
22 changes: 4 additions & 18 deletions app/src/main/java/com/kl3jvi/animity/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import android.graphics.Color
import android.view.View
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource
import com.google.android.material.snackbar.Snackbar
import com.kl3jvi.animity.data.model.AnimeMetaModel
import com.kl3jvi.animity.utils.parser.HtmlParser

class Constants {
companion object {
Expand All @@ -19,10 +17,10 @@ class Constants {
const val SAVE_DIRECTORY = "Animity"

// Base URLS
var BASE_URL = "https://gogoanime.wiki"
var BASE_URL = "https://gogoanime.film"
const val EPISODE_LOAD_URL = "https://ajax.gogocdn.net/ajax/load-list-episode"
const val SEARCH_URL = "/search.html"
const val ANIME_SCHEDULE="https://animeschedule.net/anime"
const val ANIME_SCHEDULE = "https://animeschedule.net/anime"

// Model Type
const val TYPE_RECENT_SUB = 1
Expand All @@ -43,8 +41,8 @@ class Constants {
private const val USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"

private const val ORIGIN = "https://gogoanime.wiki"
private const val REFERER = "https://gogoanime.wiki/"
private const val ORIGIN = "https://gogoanime.film"
private const val REFERER = "https://gogoanime.film/"

fun getHeader(): Map<String, String> {
return mapOf(
Expand All @@ -69,16 +67,6 @@ class Constants {
return ColorStateList.valueOf(color)
}

fun parseList(response: String, typeValue: Int): ArrayList<AnimeMetaModel> {
return when (typeValue) {
TYPE_RECENT_DUB -> HtmlParser.parseRecentSubOrDub(response, typeValue)
TYPE_RECENT_SUB -> HtmlParser.parseRecentSubOrDub(response, typeValue)
TYPE_POPULAR_ANIME -> HtmlParser.parsePopular(response, typeValue)
TYPE_MOVIE -> HtmlParser.parseMovie(response, typeValue)
TYPE_NEW_SEASON -> HtmlParser.parseMovie(response, typeValue)
else -> ArrayList()
}
}

fun getDataSourceFactory(): DefaultHttpDataSource.Factory {
return DefaultHttpDataSource.Factory().apply {
Expand All @@ -96,8 +84,6 @@ class Constants {
}
}

fun getRandomId(): Int = System.currentTimeMillis().toInt()

fun showSnack(view: View, message: String?) {
val snack =
Snackbar.make(view, message ?: "Error Occurred", Snackbar.LENGTH_LONG)
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/kl3jvi/animity/utils/ParallelCollections.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.kl3jvi.animity.utils

import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import java.util.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import kotlin.collections.ArrayList

fun <T, R> Iterable<T>.pmap(
numThreads: Int = maxOf(Runtime.getRuntime().availableProcessors() - 2, 1),
exec: ExecutorService = Executors.newFixedThreadPool(numThreads),
transform: (T) -> R,
): List<R> {

// default size is just an inlined version of kotlin.collections.collectionSizeOrDefault
val defaultSize = if (this is Collection<*>) this.size else 10
val destination = Collections.synchronizedList(ArrayList<R>(defaultSize))

for (item in this) {
exec.submit { destination.add(transform(item)) }
}

exec.shutdown()
exec.awaitTermination(1, TimeUnit.DAYS)

return ArrayList<R>(destination)
}

fun <A, B>List<A>.apmap(f: suspend (A) -> B): List<B> = runBlocking {
map { async { f(it) } }.map { it.await() }
}
Loading

0 comments on commit e1390bd

Please sign in to comment.