我在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 () {
}
}
我想捕获点击播放视频的事件。是否有办法捕捉播放视频的事件?
捕捉按钮喜欢播放,可以像这样设置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() {
}
};