-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e1fab1
commit bcb28b5
Showing
9 changed files
with
103 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 32 additions & 48 deletions
80
app/src/main/java/com/scholar/center/ui/materials/PdfFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,71 @@ | ||
package com.scholar.center.ui.materials | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.View | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import androidx.navigation.fragment.findNavController | ||
import com.scholar.center.R | ||
import com.scholar.center.databinding.FragmentPdfBinding | ||
import com.scholar.center.ui.dialogs.LoadingDialogFragment | ||
import com.scholar.center.unit.Constants.BASE_URL | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import java.net.URL | ||
|
||
|
||
@AndroidEntryPoint | ||
class PdfFragment : Fragment(R.layout.fragment_pdf) { | ||
private val viewModel: MaterialContentVM by viewModels() | ||
private lateinit var binding: FragmentPdfBinding | ||
|
||
@SuppressLint("SetJavaScriptEnabled") | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
val binding = FragmentPdfBinding.bind(view) | ||
private lateinit var launcher: ActivityResultLauncher<Intent> | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
binding = FragmentPdfBinding.bind(view) | ||
|
||
launcher = | ||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { data -> | ||
findNavController().popBackStack() | ||
} | ||
val loadingDialog = LoadingDialogFragment() | ||
loadingDialog.show(parentFragmentManager, "loading_dialog") | ||
viewLifecycleOwner.lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
viewModel.material.collect { | ||
viewModel.material.collectLatest { | ||
it?.let { materialWithDetail -> | ||
Log.d("LOadPDF", "${materialWithDetail.material}") | ||
val material = materialWithDetail.material | ||
material.content?.let { link -> | ||
Log.d("LOadPDF", "${BASE_URL}${link}") | ||
|
||
|
||
|
||
binding.webView.settings.javaScriptEnabled = true | ||
binding.webView.settings.builtInZoomControls = true | ||
binding.webView.settings.displayZoomControls = false | ||
val pdfUrl = "${BASE_URL}${link}" | ||
|
||
binding.webView.webViewClient = object : WebViewClient() { | ||
override fun onPageFinished(view: WebView, url: String) { | ||
binding.webView.loadUrl( | ||
"javascript:(function() { " + | ||
"document.querySelector('[role=\"toolbar\"]').remove();})()" | ||
) | ||
|
||
} | ||
} | ||
|
||
|
||
binding.webView.loadUrl("$pdfUrl") | ||
val pdfLink = "${BASE_URL}${link}" | ||
openPdfByIntent(pdfLink) | ||
} | ||
if (loadingDialog.isVisible) { | ||
loadingDialog.dismiss() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
viewLifecycleOwner.lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
viewModel.byteArray.collect { | ||
it?.let { pdfArray -> | ||
Log.d("LOadPDF", "${pdfArray}") | ||
|
||
// binding.pdfView.fromFile(pdfArray) | ||
// .defaultPage(0) | ||
// .enableSwipe(true) | ||
// .swipeHorizontal(false) | ||
// .load() | ||
} | ||
} | ||
} | ||
} | ||
private fun openPdfByIntent(pdfLink: String) { | ||
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(pdfLink)) | ||
launcher.launch(browserIntent) | ||
} | ||
|
||
private fun openPdfByWebView(pdfLink: String) { | ||
// binding.webView.settings.javaScriptEnabled = true | ||
// binding.webView.settings.builtInZoomControls = true | ||
// binding.webView.settings.displayZoomControls = false | ||
// val pdfUrl = "${BASE_URL}${link}" | ||
// binding.webView.loadUrl("https://docs.google.com/gview?embedded=true&url=pdfLink") | ||
} | ||
} |
133 changes: 37 additions & 96 deletions
133
app/src/main/java/com/scholar/center/ui/materials/VideoFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,61 @@ | ||
package com.scholar.center.ui.materials | ||
|
||
import android.net.Uri | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.MediaController | ||
import android.widget.RelativeLayout | ||
import androidx.fragment.app.Fragment | ||
import com.google.android.exoplayer2.ExoPlayer | ||
import com.google.android.exoplayer2.MediaItem | ||
import com.google.android.exoplayer2.source.ProgressiveMediaSource | ||
import com.google.android.exoplayer2.upstream.DefaultDataSource | ||
import com.google.android.exoplayer2.util.MimeTypes | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import com.scholar.center.R | ||
import com.scholar.center.databinding.FragmentVideoBinding | ||
import com.scholar.center.unit.Constants.BASE_URL | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.launch | ||
|
||
|
||
@AndroidEntryPoint | ||
class VideoFragment : Fragment(R.layout.fragment_video) { | ||
// private val viewModel : MaterialContentVM by viewModels() | ||
|
||
private var player: ExoPlayer? = null | ||
private val isPlaying get() = player?.isPlaying ?: false | ||
private val viewModel : MaterialContentVM by viewModels() | ||
private lateinit var binding: FragmentVideoBinding | ||
|
||
private val link = "https://media.geeksforgeeks.org/wp-content/uploads/20201217192146/Screenrecorder-2020-12-17-19-17-36-828.mp4?_=1" | ||
private val link = | ||
"https://media.geeksforgeeks.org/wp-content/uploads/20201217192146/Screenrecorder-2020-12-17-19-17-36-828.mp4?_=1" | ||
private val localLink = "http://192.168.1.101:8000/media/1/promo.mp4" | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
binding = FragmentVideoBinding.bind(view) | ||
|
||
val v = "https://4422-146-70-174-67.ngrok-free.app/media/1/cc.mp4" | ||
|
||
|
||
// Uri object to refer the | ||
// resource from the videoUrl | ||
// Uri object to refer the | ||
// resource from the videoUrl | ||
val uri = Uri.parse(v) | ||
|
||
// sets the resource from the | ||
// videoUrl to the videoView | ||
|
||
// sets the resource from the | ||
// videoUrl to the videoView | ||
binding.videoView.setVideoURI(uri) | ||
|
||
// creating object of | ||
// media controller class | ||
viewLifecycleOwner.lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
viewModel.material.collect { it -> | ||
val material = it?.material | ||
material?.content?.let { link -> | ||
val url = "${BASE_URL}$link" | ||
binding.andExoPlayerView.setSource(url) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// creating object of | ||
// media controller class | ||
/* | ||
private fun playVideoWithVideoView(){ | ||
val uri = Uri.parse(localLink) | ||
val mediaController = MediaController(requireContext()) | ||
|
||
// sets the anchor view | ||
// anchor view for the videoView | ||
|
||
// sets the anchor view | ||
// anchor view for the videoView | ||
mediaController.setAnchorView(binding.videoView) | ||
|
||
// sets the media player to the videoView | ||
|
||
// sets the media player to the videoView | ||
mediaController.setMediaPlayer(binding.videoView) | ||
|
||
// sets the media controller to the videoView | ||
|
||
// sets the media controller to the videoView | ||
binding.videoView.setMediaController(mediaController) | ||
|
||
binding.videoView.layoutParams = RelativeLayout.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, | ||
ViewGroup.LayoutParams.MATCH_PARENT) | ||
|
||
// starts the video | ||
|
||
// starts the video | ||
binding.videoView.start() | ||
|
||
// viewLifecycleOwner.lifecycleScope.launch { | ||
// repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
// viewModel.material.collect { it -> | ||
// val material = it?.material | ||
// material?.content?.let { link -> | ||
// mediaController.setAnchorView(binding.videoView) | ||
// mediaController.setMediaPlayer(binding.videoView) | ||
// binding.videoView.setMediaController(mediaController) | ||
// binding.videoView.setVideoURI(uri) | ||
// binding.progressBar.visibility = View.VISIBLE | ||
// binding.videoView.setOnPreparedListener {mp: MediaPlayer-> | ||
// mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING) | ||
// binding.progressBar.visibility = View.GONE | ||
// mp.setScreenOnWhilePlaying(false) | ||
// | ||
// } | ||
// } | ||
// } | ||
// } | ||
// binding.videoView.start() | ||
} | ||
*/ | ||
|
||
private fun initializePlayer() { | ||
player = ExoPlayer.Builder(requireContext()) // <- context | ||
.build() | ||
|
||
// create a media item. | ||
val mediaItem = MediaItem.Builder() | ||
.setUri("https://storage.googleapis.com/exoplayer-test-media-0/BigBuckBunny_320x180.mp4") | ||
.setMimeType(MimeTypes.APPLICATION_MP4) | ||
.build() | ||
|
||
// Create a media source and pass the media item | ||
val mediaSource = ProgressiveMediaSource.Factory( | ||
DefaultDataSource.Factory(requireContext()) // <- context | ||
) | ||
.createMediaSource(mediaItem) | ||
|
||
// Finally assign this media source to the player | ||
player!!.apply { | ||
setMediaSource(mediaSource) | ||
playWhenReady = true // start playing when the exoplayer has setup | ||
seekTo(0, 0L) // Start from the beginning | ||
prepare() // Change the state from idle. | ||
}.also { | ||
// Do not forget to attach the player to the view | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
android:gravity="center"> | ||
android:gravity="center" | ||
> | ||
|
||
<VideoView | ||
android:id="@+id/videoView" | ||
<!-- <VideoView--> | ||
<!-- android:id="@+id/videoView"--> | ||
<!-- android:layout_width="match_parent"--> | ||
<!-- android:layout_height="wrap_content"--> | ||
<!-- />--> | ||
|
||
<com.potyvideo.library.AndExoPlayerView | ||
android:id="@+id/andExoPlayerView" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
|
||
<ProgressBar | ||
android:id="@+id/progressBar" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" /> | ||
android:layout_centerInParent="true" | ||
android:visibility="gone"/> | ||
|
||
</LinearLayout> | ||
</RelativeLayout> |
Oops, something went wrong.