I have the following navigation structure in my app:
MainActivity -> VideoListFragment -> VideoShowingFragment
(only activity, (shows a list of Video (gets the URL of the video to
container for items via a Recycler- show and plays the video via
all the other View, the URL of the ExoPlayer. From here, the user
fragments) Video is passed to can enter PiP mode)
next Fragment)
So, when the app starts, a list of Video
items are shown to the user. When the user clicks on an item, the URL is passed to the VideoShowingFragment
which displays the Video
content to the user via the PlayerView
of the ExoPlayer
library.
On top of the PlayerView
, there is an icon so that the user can enter PiP mode, when he clicks on it.
Entering PiP works and is all fine but my question is how can I show the list of Videos after going to PiP mode?
What I mean: When the user is in the VideoShowingFragment
stage and decides to minimize the Video
content by entering the PiP mode, how can I show him the list of Video
s (VideoListFragment
) so that he can browse through the list while still watching the content in PiP mode ?
The situation right now is the following: The user enters PiP mode but the user sees the Start Screen of his device while the video content is minimized in the right bottom corner.
Here are some relevant parts of my project:
<!-- AndroidManifest.xml -->
<application
...>
<activity
android:name=".MainActivity"
android:supportsPictureInPicture="true"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
As mentioned, the VideoListFragment
navigates to ShowingVideoFragment
is clicked:
videoListFragmentViewModel.openVideoEvent.observe(viewLifecycleOwner, EventObserver{ videoUri -> findNavController().navigate(VideoListFragmentDirections.actionVideoListFragmentToVideoShowingFragment(videoUri))})
The ShowingFragment
then gets the data needed for displaying the video. I will not dive into that because it is just the typical ExoPlayer
stuff.
Also in ShowingFragment
, we have the logic for going to PiP mode:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentVideoShowingBinding.inflate(inflater, container, false)
// listener on icon with ID 'pipMode'
binding.pipMode.setOnClickListener{ enablePipMode() }
return binding.root
}
private fun enablePipMode() {
// set aspect ratio
picInPicParamsBuilder.setAspectRatio(Rational(binding.playerView.width, binding.playerView.height))
// enter the PiP mode
activity?.enterPictureInPictureMode(picInPicParamsBuilder.build())
}
// override to make the icon on top of PlayerView eiter visible or invisible
override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode)
if(isInPictureInPictureMode){
binding.pipMode.visibility = View.GONE
}
else{
binding.pipMode.visibility = View.VISIBLE
}
}
Do I need to host the VideoShowingFragment
in a 2nd activity when I want to let the user watch the selected video in PiP mode while browsing through the VideoListFragment
? Or is there maybe another way to achieve what I want ?