自定义youtube播放器不会更新搜索栏

时间:2017-04-10 07:36:13

标签: android android-mediaplayer

我创建了自己的播放器布局,并使用Youtube api显示视频内容,但是当视频启动时,我的搜索栏不会更新并保持修复状态。我认为问题出在mSeekBarChangeListener内,但我不确定。

这是我的代码:

  public class ItemDetails extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,View.OnClickListener {

    private final static String TAG = "ItemDetails";

    @BindView(R.id.item_title)TextView itemTitle;
    @BindView(R.id.item_date)TextView itemDate;
    @BindView(R.id.item_plot)TextView itemPlot;
    @BindView(R.id.item_short_description)TextView itemShortDescription;
    @BindView(R.id.backdop_image_details)ImageView imageBackdrop;
    @BindView(R.id.tv_more)TextView tvSeeMore;
    @BindView(R.id.trailer_video)YouTubePlayerView traileVideo;
    @BindView(R.id.video_control)LinearLayout videoControlLayout;
    @BindView(R.id.play_time)TextView mPlayTimeTextView;
    @BindView(R.id.video_seekbar)SeekBar mSeekBar;

    YouTubePlayer mPlayer;

    private Handler mHandler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_details);

        ButterKnife.bind(this);

        findViewById(R.id.play_video).setOnClickListener(this);
        findViewById(R.id.pause_video).setOnClickListener(this);

        mSeekBar.setMax(100);
        mSeekBar.setProgress(0);
        mSeekBar.setOnSeekBarChangeListener(mVideoSeekBarChangeListener);
        mHandler = new Handler();

    }

    @OnClick(R.id.back_arrow_detail)void backArrowClick() {
        onBackPressed();
    }


    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
        if(youTubePlayer != null) {
            mPlayer = youTubePlayer;

            displayCurrentTime();

            // start buffering
            if(!b) {
                youTubePlayer.cueVideo(trailerKey);
            }

            // set style and show control layout
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
            videoControlLayout.setVisibility(View.VISIBLE);

            youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
            youTubePlayer.setPlaybackEventListener(playbackEventListener);
        }
    }

    private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
        @Override
        public void onPlaying() {
            mHandler.postDelayed(runnable,100);
            displayCurrentTime();
        }

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

        @Override
        public void onStopped() {
            mHandler.removeCallbacks(runnable);
        }

        @Override
        public void onBuffering(boolean b) {

        }

        @Override
        public void onSeekTo(int i) {
            mHandler.postDelayed(runnable,100);
        }
    };

    private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
        @Override
        public void onLoading() {

        }

        @Override
        public void onLoaded(String s) {

        }

        @Override
        public void onAdStarted() {

        }

        @Override
        public void onVideoStarted() {
            displayCurrentTime();
        }

        @Override
        public void onVideoEnded() {

        }

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

        }
    };

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        Log.d(TAG,youTubeInitializationResult.toString());
    }


    SeekBar.OnSeekBarChangeListener mVideoSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            long lengthPlayed = (mPlayer.getDurationMillis() * progress) / 100;
            mPlayer.seekToMillis((int) lengthPlayed);
            seekBar.setProgress(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };


    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.play_video:
                if(mPlayer != null && !mPlayer.isPlaying()) {
                    mPlayer.play();
                }
                break;
            case R.id.pause_video:
                if(mPlayer != null && mPlayer.isPlaying()) {
                    mPlayer.pause();;
                }
                break;
        }
    }

    private void displayCurrentTime() {
        if (null == mPlayer) return;
        String formattedTime = formatTime(mPlayer.getDurationMillis() - mPlayer.getCurrentTimeMillis());
        mPlayTimeTextView.setText(formattedTime);
    }

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

        return (hours == 0 ? "--:" : hours + ":") + String.format("%02d:%02d", minutes % 60, seconds % 60);
    }


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

2 个答案:

答案 0 :(得分:0)

对于所有寻求解决方案的人:

  1. 在可运行对象内设置搜索栏进度:

     private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            displayCurrentTime();

            //Progress in percentage 
            int progress = mPlayer.getCurrentTimeMillis() * 100 / mPlayer.getDurationMillis();
            mSeekBar.setProgress(progress);

            mHandler.postDelayed(this, 100);
        }
    };
  1. 由于这将启动OnSeekBarChangeListener,因此在以下情况下您需要添加其他内容:
SeekBar.OnSeekBarChangeListener mVideoSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            //Only when the user change the progress
            if(fromUser){
                long lengthPlayed = (mPlayer.getDurationMillis() * progress) / 100;
                mPlayer.seekToMillis((int) lengthPlayed);
            }
       }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    };

;)

答案 1 :(得分:0)

自定义youtube播放器无色样式-设置SeekBar进度和SeekBar跟踪触摸前进/后退

library(stargazer)

stargazer(mtcars, type = 'text', title = "Statistiques descriptives",digits = 1, out="table1.txt")

Custom youtube player