Stack Overflow Asked by mukesh on December 1, 2021
I have to develop an app that has a list of videos and also support PIP mode. I have used ViewPager2 with vertical orientation and ExoPlayer. FragmentStateAdapter is use for ViewPager2 adapter
Problems.
Code –
2. HomeActivity
override fun onUserLeaveHint() {
super.onUserLeaveHint()
if (fragment != null && isPIPSupport() && !isInPictureInPictureMode) {
enterPIPMode()
}
}
fun enterPIPMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
) {
val aspectRatio = Rational(
3, 4
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val params =
PictureInPictureParams.Builder()
.setAspectRatio(aspectRatio)
.build()
enterPictureInPictureMode(params)
} else {
enterPictureInPictureMode()
}
}
}
3. Adapter
class ViewPagerStateAdapter(
private val productList: ArrayList<Product>,
private val fragmentActivity: Fragment, val frgManager: FragmentManager
) : FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int {
return productList.size
}
override fun createFragment(position: Int): Fragment {
return VideoDetailFragment.newInstance(position)
}
}
4. Fragment
class VideoDetailFragment : BaseFragment() , GestureListener {
private var position:Int=0
private lateinit var mExoPlayerManager: ExoPlayerManager
private lateinit var mDetector: GestureDetectorCompat
private lateinit var mHomeActivity: HomeActivity
companion object {
fun newInstance(position: Int): Fragment {
val bundle = Bundle()
bundle.putInt("position", position)
val frg = VideoDetailFragment()
frg.arguments = bundle
return frg
}
}
override fun getToolbarId(): Int {
return -1
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mHomeActivity=activity as HomeActivity
position= arguments?.getInt("position")?:0
mExoPlayerManager = ExoPlayerManager(requireContext())
initializeGestureDetector()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_show_detail, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
manageExoPlayer(view)
}
private fun manageExoPlayer(view:View)
{
val exoPlayerView = view.findViewById<PlayerView>(R.id.exo_player_clp)
managePlayer(exoPlayerView,position,SLPList.productList[position])
}
fun managePlayer(exoPlayerView: PlayerView, position: Int, product: Product) {
product.getVideoUrl()?.let {
mExoPlayerManager.setPlayerView(exoPlayerView)
exoPlayerView.player = mExoPlayerManager.getPlayer()
Log.v("LiveVideoActivity", mExoPlayerManager.getCurrentTime().toString())
val mergingMediaSource = mExoPlayerManager.getMediaSource(it)
mExoPlayerManager.prepare(mergingMediaSource, true, true)
mExoPlayerManager.setPlayWhenReady(false)
}
}
override fun onDestroyView() {
mExoPlayerManager.releasePlayer()
super.onDestroyView()
}
override fun onPause() {
super.onPause()
if (!mHomeActivity.isInPIP()) {
mExoPlayerManager.onPause()
}
}
override fun onResume() {
super.onResume()
if (!mHomeActivity.isInPIP()) {
mExoPlayerManager.onResume()
}
}
private fun initializeGestureDetector() {
val customGesture = CustomGestureDetector(this)
mDetector = GestureDetectorCompat(requireContext(), customGesture)
}
}
6. Set up Adapter
private fun setupViewPager() {
liveShowAdapter = ViewPagerStateAdapter(SLPList.productList, this, childFragmentManager)
mLiveShowViewPager?.apply {
adapter = liveShowAdapter
offscreenPageLimit =
3
registerOnPageChangeCallback(registerChangeCallBack)
}
}
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP