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

tracker is not updating and is returning 0.0 as duration initially #1186

Open
AsifPatan opened this issue Nov 24, 2024 · 3 comments
Open

tracker is not updating and is returning 0.0 as duration initially #1186

AsifPatan opened this issue Nov 24, 2024 · 3 comments
Labels

Comments

@AsifPatan
Copy link

Question / Problem

the tracker for youtube is returning 0.0 as video duration when song is being played

Summary

In my code i initialized youtube player widgit where i used onready() and onStateChanged()
in onready() i am adding tracker to the youtubeplayer

but in onstatechanged if i get duration of the video playing at first it return zero but if we change the state i.e. if we pause then it gets updated
also when i change the song i.e. play other then it stores the duration of previous video untill pause is clicked

same happens with current second duration (i.e. played duration)

What I've tried

i tried to remove the previous listener if any and add the listener every time after the song has been played then also it didn't work

Flow of code

platform : Android

on create --> Initialize youtube player (but will not play anything) --> on button click load the video into youtube player view

during initialization i have added the tracker

@PierfrancescoSoffritti
Copy link
Owner

Hey, could you provide the code? if this is a bug, i'd suggest finding a way to replicate it in the sample app.

@AsifPatan AsifPatan reopened this Nov 27, 2024
@AsifPatan
Copy link
Author

AsifPatan commented Nov 27, 2024

Hey, could you provide the code? if this is a bug, i'd suggest finding a way to replicate it in the sample app.

@PierfrancescoSoffritti Thanks for your reply, I have recreated that with a example here is the code

code

class default_activity : AppCompatActivity() {
    lateinit var youTubePlayerView: YouTubePlayerView
    lateinit var tracker: YouTubePlayerTracker
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_default)

        youTubePlayerView = findViewById<YouTubePlayerView>(R.id.youtube_player_view)
        youTubePlayerView.enableAutomaticInitialization = false
        youTubePlayerView.enableBackgroundPlayback(true)
        lifecycle.addObserver(youTubePlayerView)

        initializeYTPlayer()
    }

    private fun initializeYTPlayer() {
        val customPlayerUi =
            youTubePlayerView.inflateCustomPlayerUi(R.layout.custom_player_ui_default)

        val listener: YouTubePlayerListener = object : AbstractYouTubePlayerListener() {
            override fun onReady(youTubePlayer: YouTubePlayer) {

                val customPlayerUiController = CustomPlayerUiController2(customPlayerUi, youTubePlayer, youTubePlayerView)
                youTubePlayer.addListener(customPlayerUiController)

                tracker=YouTubePlayerTracker()
                youTubePlayer.addListener(tracker)

                youTubePlayer.loadOrCueVideo(lifecycle, "M_x7zq92lc8", 0f)
            }

            override fun onStateChange(youTubePlayer: YouTubePlayer, state: PlayerConstants.PlayerState) {
                var array=arrayOf("unknown","unstarted","ended","playing","paused","buffering","video cued")
                printCurrent(array[state.ordinal])
                super.onStateChange(youTubePlayer, state)
     
                if(state==PlayerConstants.PlayerState.PAUSED){
                    
                }
                if(state==PlayerConstants.PlayerState.PLAYING){
                  
                }
            }

            override fun onVideoDuration(youTubePlayer: YouTubePlayer, duration: Float) {
                super.onVideoDuration(youTubePlayer, duration)
                printCurrent("On video duration")

            }
        }

        val options: IFramePlayerOptions = IFramePlayerOptions.Builder().controls(0).build()
        youTubePlayerView.initialize(listener, options)
    }

    fun printCurrent(s: String) {
        Log.d("current","Now $s The current duration is ${tracker.currentSecond.toString()} and total duration is ${tracker.videoDuration.toString()}")
    }

}

output I got

10:18:53.337  D  Now unstarted The current duration is 0.0 and total duration is 0.0
10:18:53.339  D  Now buffering The current duration is 0.0 and total duration is 0.0
10:18:54.210  D  Now playing The current duration is 0.0 and total duration is 0.0
10:18:54.211  D  Now On video duration The current duration is 0.0 and total duration is 0.0
10:19:36.170  D  Now ended The current duration is 41.5487 and total duration is 41.441

here on videoDuration method is called after the video is being played but yet i got total duration as 0.0

Hope you respond soon @PierfrancescoSoffritti

@AsifPatan
Copy link
Author

@PierfrancescoSoffritti have you got the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants