如何捕获适用于Android的YouTube播放器中的播放视频操作

时间:2016-09-21 02:07:40

标签: android youtube-api

我在Android应用程序中嵌入了YouTube播放器。我使用以下课程播放视频:

public class YoutubePlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

    private static final String TAG =  YoutubePlayerActivity.class.getSimpleName();

    private YouTubePlayerView playerView;
    private Toolbar toolbar;
    private int minuto;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Log.d(TAG , "onCreate(Bundle) - Ini ");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        toolbar= (Toolbar) findViewById(R.id.app_bar);
      //  setSupportActionBar(toolbar);
        toolbar.setTitle(R.string.app_name);
        toolbar.setTitleTextColor(getResources().getColor(R.color.com_facebook_button_background_color_focused));

        playerView = (YouTubePlayerView) findViewById(R.id.player_view);
        playerView.initialize(DeveloperKey.DEVELOPER_KEY, this);


        Log.d(TAG , "onCreate(Bundle) - Fi ");
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

        Log.d(TAG , "onInitializationSuccess(Provider, YouTubePlayer, boolean ) - Ini ");

        if(!b) {
            youTubePlayer.play();
            youTubePlayer.cueVideo(getIntent().getStringExtra("VIDEO_ID"));
            minuto =  youTubePlayer.getCurrentTimeMillis();



        }

        Log.d(TAG , "onInitializationSuccess(Provider, YouTubePlayer, boolean ) - Fi ");

    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        Log.d(TAG , "onInitializationFailure(Provider, YouTubeInitializationResult) - Ini ");

        Toast.makeText(this, getString(R.string.player_failed), Toast.LENGTH_LONG).show();

        Log.d(TAG , "onInitializationFailure(Provider, YouTubeInitializationResult) - Fi ");
    }

    @Override
    public void onBackPressed () {

    }
}

我想捕捉点击播放视频的事件。有没有办法捕捉播放视频的事件?

1 个答案:

答案 0 :(得分:2)

捕捉按钮就像播放一样,暂停你可以像这样设置youtube播放

playerView.setPlaybackEventListener(playbackEventListener);

其中播放侦听器的声明如下:

private PlaybackEventListener playbackEventListener = new PlaybackEventListener()    {

    @Override
    public void onBuffering(boolean arg0) {
    }

    @Override
    public void onPaused() {
    }

    @Override
    public void onPlaying() {
    }

    @Override
    public void onSeekTo(int arg0) {
    }

    @Override
    public void onStopped() {
    }

};