Skip to content

Commit

Permalink
fix: crash when trying to open a youtube video when offline (openedx#257
Browse files Browse the repository at this point in the history
)

* fix: crash when trying to open a youtube video when offline

* refactor: added utility functions inside EncodedVideos for videoUrls
  • Loading branch information
dixidroid authored Mar 15, 2024
1 parent f91927b commit 9b7f73f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
14 changes: 10 additions & 4 deletions core/src/main/java/org/openedx/core/domain/model/Block.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,19 @@ data class EncodedVideos(
|| fallback?.url != null

val videoUrl: String
get() = mobileHigh?.url
?: mobileLow?.url
?: desktopMp4?.url
get() = fallback?.url
?: hls?.url
?: fallback?.url
?: desktopMp4?.url
?: mobileHigh?.url
?: mobileLow?.url
?: ""

val hasVideoUrl: Boolean
get() = videoUrl.isNotEmpty()

val hasYoutubeUrl: Boolean
get() = youtube?.url?.isNotEmpty() == true

fun getPreferredVideoInfoForDownloading(preferredVideoQuality: VideoQuality): VideoInfo? {
var preferredVideoInfo = when (preferredVideoQuality) {
VideoQuality.OPTION_360P -> mobileLow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,17 @@ class CourseUnitContainerAdapter(

private fun unitBlockFragment(block: Block): Fragment {
return when {
(block.isVideoBlock && block.studentViewData?.encodedVideos != null) -> {
(block.isVideoBlock &&
(block.studentViewData?.encodedVideos?.hasVideoUrl == true ||
block.studentViewData?.encodedVideos?.hasYoutubeUrl == true)) -> {
val encodedVideos = block.studentViewData?.encodedVideos!!
val transcripts = block.studentViewData!!.transcripts
with(encodedVideos) {
var isDownloaded = false
val videoUrl = if (viewModel.getDownloadModelById(block.id) != null) {
isDownloaded = true
viewModel.getDownloadModelById(block.id)!!.path
} else if (fallback != null) {
fallback!!.url
} else if (hls != null) {
hls!!.url
} else if (desktopMp4 != null) {
desktopMp4!!.url
} else if (mobileHigh != null) {
mobileHigh!!.url
} else if (mobileLow != null) {
mobileLow!!.url
} else {
""
}
} else videoUrl
if (videoUrl.isNotEmpty()) {
VideoUnitFragment.newInstance(
block.id,
Expand All @@ -57,7 +47,7 @@ class CourseUnitContainerAdapter(
YoutubeVideoUnitFragment.newInstance(
block.id,
viewModel.courseId,
encodedVideos.youtube?.url!!,
encodedVideos.youtube?.url ?: "",
transcripts?.toMap() ?: emptyMap(),
block.displayName
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ class YoutubeVideoUnitFragment : Fragment(R.layout.fragment_youtube_video_unit)
}
}

override fun onStateChange(youTubePlayer: YouTubePlayer, state: PlayerConstants.PlayerState) {
override fun onStateChange(
youTubePlayer: YouTubePlayer,
state: PlayerConstants.PlayerState
) {
super.onStateChange(youTubePlayer, state)
viewModel.isPlaying = when (state) {
PlayerConstants.PlayerState.PLAYING -> true
Expand Down Expand Up @@ -191,11 +194,16 @@ class YoutubeVideoUnitFragment : Fragment(R.layout.fragment_youtube_video_unit)
binding.youtubePlayerView.setCustomPlayerUi(defPlayerUiController.rootView)
}

val videoId = viewModel.videoUrl.split("watch?v=")[1]
if (viewModel.isPlaying) {
youTubePlayer.loadVideo(videoId, viewModel.getCurrentVideoTime().toFloat() / 1000)
} else {
youTubePlayer.cueVideo(videoId, viewModel.getCurrentVideoTime().toFloat() / 1000)
viewModel.videoUrl.split("watch?v=").getOrNull(1)?.let { videoId ->
if (viewModel.isPlaying) {
youTubePlayer.loadVideo(
videoId, viewModel.getCurrentVideoTime().toFloat() / 1000
)
} else {
youTubePlayer.cueVideo(
videoId, viewModel.getCurrentVideoTime().toFloat() / 1000
)
}
}
youTubePlayer.addListener(youtubeTrackerListener)
}
Expand Down

0 comments on commit 9b7f73f

Please sign in to comment.