VideoView inside Fragment, How do i stop video from play when i swipe away?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



VideoView inside Fragment, How do i stop video from play when i swipe away?



I have a viewPager where i display both images and Video.
However, when a user plays a video and immediately swipes to the next fragment, the previous Video continues to PLAY!



Here's how i played the video


videoView!!.setVideoURI(uri)
videoView!!.setMediaController(MediaController(context))
videoView!!.requestFocus()

play.setOnClickListener(View.OnClickListener {
if (!videoView!!.isPlaying())
videoView!!.start()
videoView!!.setVisibility(View.VISIBLE)



I have tried inserting


if (videoView!!.isPlaying())
videoView!!.setMediaController(null)
videoView?.stopPlayback()



in onStop, onPause and onDetach of the viewPager's fragment but it is not working.



Your help would be invaluable!




3 Answers
3



A Simple Interface:


interface Playable

fun play()

fun stop()



fragments containing VideoView must implement this interface


class VideoFragment : Fragment(), Playable
...
override fun play()
//play logic


override fun stop()
//stop logic

...



PagerAdapter:


class PagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm)

private val instantiatedItems = SparseArrayCompat<WeakReference<Any>>()
private var selectedPosition: Int by Delegates.observable(-1) _, oldPosition, newPosition ->
fun playableAt(position: Int) = instantiatedItems.get(position)?.get() as? Playable
playableAt(oldPosition)?.stop()
playableAt(newPosition)?.play()

...
override fun instantiateItem(container: ViewGroup, position: Int): Any
return super.instantiateItem(container, position).also
instantiatedItems.put(position, WeakReference(it))



override fun destroyItem(container: ViewGroup, position: Int, `object`: Any)
instantiatedItems.remove(position)
super.destroyItem(container, position, `object`)


fun onPageSelected(position: Int)
selectedPosition = position




finally:


...
pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener()
override fun onPageSelected(position: Int)
adapter.onPageSelected(position)//this method is declared in PagerAdapter

.also
pager.post it.onPageSelected(pager.currentItem)
)
...





Bravo my friend, It worked!
– Micklo_Nerd
Aug 12 at 7:44



It is because ViewPager keeps offscreen fragments started. For instance you have a fragment visible to the user. ViewPager will try to keep the previous fragment (on the left side) and the next fragment (on the right side) started. This allows ViewPagerperforming smooth sliding when user decides to change the page, because the next and the previous pages are already prepared.


ViewPager


ViewPager


ViewPager



In your case the video player is not visible (offscreen), but ViewPager keeps it started as due to the behaviour described above. You can use setOffscreenPageLimit() method to change this behaviour. If you set page limit to 0, then offscreen fragments will be paused immediately. Unfortunately they will not only be paused, but stopped and detached from the activity too. This means when you return back to your fragment, it will recreate the whole layout anew. That's why you can try to override either Fragment.setUserVisibleHint() or Fragment.onHiddenChanged() and execute your pause/play logic there. ViewPager will update hidden state of a fragment depending on whether the fragment is actually visible to user or not.


ViewPager


0


Fragment.setUserVisibleHint()


Fragment.onHiddenChanged()


ViewPager



Hope this helps.





thanks, but 1. mViewPager?.offscreenPageLimit = 0 does not work. 2 even if found away to make it work, it would make my app less responsive. 3 i'd love to keep FragmentStatePagerAdapter and also find a way to fix this unwanted behaviour
– Micklo_Nerd
Aug 11 at 20:01



mViewPager?.offscreenPageLimit = 0



You can apply


if (videoView!!.isPlaying()) { videoView!!.setMediaController(null) videoView?.stopPlayback() ;



In the pager.addOnPageChangeListener();.


pager.addOnPageChangeListener();






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered