Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exo player in view pager2 #7313

Open
develophead opened this issue Apr 29, 2020 · 4 comments
Open

exo player in view pager2 #7313

develophead opened this issue Apr 29, 2020 · 4 comments

Comments

@develophead
Copy link

develophead commented Apr 29, 2020

I am getting issues like multiple players are working.
I tried to solve this issue
-by removing the player view if it exists and setting the player view to a frame layout in onBindViewHolder

  • then I tried wit release player before setting player view
    in both scenarios, I am failed to achieve the expected result

code:-

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.releasePlayer()
        holder.initializeExoPlayer(mData[position].content)
    }


//ViewHolder internal class
fun initializeExoPlayer(content: Content) {
            videoSurfaceView = PlayerView(itemView.context)
            videoSurfaceView!!.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM)
            bandwidthMeter = DefaultBandwidthMeter()
            videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
            trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
            videoPlayer =
                ExoPlayerFactory.newSimpleInstance(itemView.context, trackSelector)
            videoSurfaceView!!.useController = false
            videoSurfaceView!!.player = videoPlayer
            prepareExoPlayer(content)
            playerListener()
        }

 fun prepareExoPlayer(urlDataSet: Content) {
            val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(
                mContext, Util.getUserAgent(mContext, "playbacktestforfoodiee")
            )

            if (!urlDataSet.hls.isNullOrEmpty()) {
                var Url: String? = null
                var found=false
                for (url in urlDataSet.hls) {
                    if (url.substringBefore(".m3u8", url).takeLast(3).contains("360")) {
                        Url = url
                        found=true
                        break
                    }
                }
                if (!found){
                    Url=urlDataSet.hls[0]
                }
                val hlsMediaSource: HlsMediaSource = HlsMediaSource.Factory(dataSourceFactory)
                    .setAllowChunklessPreparation(true)
                    .createMediaSource(Uri.parse(Url))
                videoPlayer!!.prepare(hlsMediaSource)
                videoPlayer!!.seekTo(playBackPosition)
                videoPlayer!!.playWhenReady = true
                addVideoView()

            } else {
                val videoSource: MediaSource = ExtractorMediaSource.Factory(dataSourceFactory)
                    .createMediaSource(Uri.parse(urlDataSet.mp4))
                videoPlayer!!.prepare(videoSource)
                videoPlayer!!.seekTo(playBackPosition)
                videoPlayer!!.playWhenReady = true
                addVideoView()
            }
        }
        fun releasePlayer() {
            if (videoPlayer != null) {
                videoPlayer!!.release()
                videoPlayer = null
            }
        }
@christosts
Copy link
Contributor

Can you please clarify what is the expected result, and what exactly is failing? I cannot follow from the code snippet what exactly you are trying to work around.

@develophead
Copy link
Author

sorry about that.

use case
the user has to be swipe up/down to get the next/previous video playback.

for this scenario, I am trying to implement Exo player in viewpager2 inside a fragment
here I am using recyclerView adapter.

the issues what I am facing
the player gets multiplying while scrolling and I lose control over the player.

can you please advise me how to implement it in viewPager2

@develophead
Copy link
Author

hi christosts,
hope you doing well. finally, I tried with FragmentStateAdapter so I think that issue is fixed and video playback while scrolling up/down looks working fine

now I am facing small issue like if we navigating from this fragment(video playback happens here) to others and coming back to this fragment again. I can see that the video rotated even the app only supports portrait mode.
Screen Shot 2020-05-02 at 12 40 36 AM

I am expecting your advice so it could be a great pleasure to know more about the best practices.

thank you

@develophead
Copy link
Author

now this issue become serious can you please help me
issue.mov.zip
this is the video of my issue please check

thankyou

@ojw28 ojw28 assigned icbaker and unassigned ojw28 Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants