0
votes

I've implemented a youtube player (fragment) in my android project. I'm tryin' to play the video in chromeless player style, while implementing player control on my own. Play and pause buttons are working fine, however seekbar isn't moving as the video is progressing. Though, I'm able to skip video through it (play video ahead), but the seekbar isn't moving on it's own. Where did I go wrong?

    @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            if (!b) {
                YPlayer = youTubePlayer;
                displayCurrentTime();      // It displays current progress of video in --:-- format. It's working fine...
                YPlayer.loadVideo(id);     // id -> YouTube video id
                YPlayer.play();
                YPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
                YPlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
                    @Override
                    public void onLoading() {
                    }

                    @Override
                    public void onLoaded(String s) {
                    }

                    @Override
                    public void onAdStarted() {
                    }

                    @Override
                    public void onVideoStarted() {
                        seekBar.setMax(YPlayer.getDurationMillis()/1000);
                    }

                    @Override
                    public void onVideoEnded() {
                    }

                    @Override
                    public void onError(YouTubePlayer.ErrorReason errorReason) {
                    }
                });

                YPlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
                    @Override
                    public void onPlaying() {
                        handler.postDelayed(runnable, 100);   //Handler's instance initialized as instance variable
                        displayCurrentTime();
                    }

                    @Override
                    public void onPaused() {
                        handler.removeCallbacks(runnable);
                    }

                    @Override
                    public void onStopped() {
                    }

                    @Override
                    public void onBuffering(boolean b) {
                        handler.postDelayed(runnable, 1000);
                    }

                    @Override
                    public void onSeekTo(int i) {
                        handler.removeCallbacks(runnable);
                    }
                });
                playerControl.setVisibility(View.VISIBLE);   //Layout with play (and pause) button, Seekbar (which isn't moving along with the video), and timer (string being returned by the above displayCurrentTime() method)
            }
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        }
    });
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            long lengthPlayed = (YPlayer.getDurationMillis() * progress) / 100;
            YPlayer.seekToMillis((int) lengthPlayed);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            while(YPlayer != null && seekBar.getMax() > YPlayer.getCurrentTimeMillis())
            {
                int videoProgressed = 0;
                try
                {
                    Thread.sleep(1000);
                    videoProgressed = YPlayer.getCurrentTimeMillis();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                seekBar.setProgress(videoProgressed);
            }
        }
    });


    private void displayCurrentTime() {
        if (YPlayer != null) {
            String formattedTime = formatTime(YPlayer.getCurrentTimeMillis());
            playTime.setText(formattedTime);
        }
    }

    private String formatTime(int millis) {
        int seconds = millis / 1000;
        int minutes = seconds / 60;

        return String.format("%02d:%02d", minutes % 60, seconds % 60);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if(YPlayer != null) {
                displayCurrentTime();
            }
            handler.postDelayed(this, 1000);
        }
    };
3

3 Answers

1
votes

USE THIS CODE

Custom youtube player CHROMELESS style - set SeekBar Progress and Seek Bar TrackingTouch Forward/backward,

mSeekBar = (SeekBar) findViewById(R.id.video_seekbar);

mSeekBar.setOnSeekBarChangeListener(mVideoSeekBarChangeListener)

SeekBar.OnSeekBarChangeListener mVideoSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        long lengthPlayed = (mYouTubePlayer.getDurationMillis() * seekBar.getProgress()) / 100;
        mYouTubePlayer.seekToMillis((int) lengthPlayed);
    }
};

private Runnable runnable = new Runnable() {
    @Override
    public void run(){
        displayCurrentTime();
        mHandler.postDelayed(this, 100);
    }
};


 private void displayCurrentTime() {
    if (null == mYouTubePlayer) return;
    String formattedTime = formatTime(mYouTubePlayer.getDurationMillis() - mYouTubePlayer.getCurrentTimeMillis());
    play_timetv.setText(formattedTime);
    playPercent = (int) (((float) mYouTubePlayer.getCurrentTimeMillis()/(float) mYouTubePlayer.getDurationMillis()) * 100);
    System.out.println("get youtube displayTime 2 : "+playPercent);
    // update live progress
    mSeekBar.setProgress(playPercent, true);
}

Custom youtube player

0
votes

Check this tutorial if it can help you. This tutorial is all about Android Building Audio Player. It discuss here on how to build a simple audio player with basic controls like play, pause, forward, backward, next, previous, playlist and seekbar.

Just check the code in where the tutorial explain on how to update progress bar timer implemented in a background thread which runs in background using a Handler. You can check here how the seekbar being implemented.

For more information, check this related SO question.

0
votes

Add youtubeSeekBar.setProgress(yPlayer.getCurrentTimeMillis()/100); in on playing() event

YPlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
                @Override
                public void onPlaying() {
                    handler.postDelayed(runnable, 100);   //Handler's instance initialized as instance variable
                    displayCurrentTime();

youtubeSeekBar.setProgress(yPlayer.getCurrentTimeMillis()/100); //this is your key of success
                }

                @Override
                public void onPaused() {
                    handler.removeCallbacks(runnable);
                }

                @Override
                public void onStopped() {
                }

                @Override
                public void onBuffering(boolean b) {
                    handler.postDelayed(runnable, 1000);
                }

                @Override
                public void onSeekTo(int i) {
                    handler.removeCallbacks(runnable);
                }
            });
            playerControl.setVisibility(View.VISIBLE);   //Layout with play (and pause) button, Seekbar (which isn't moving along with the video), and timer (string being returned by the above displayCurrentTime() method)
        }