目前正在开发集成youtube播放器的应用程序,所有工作正常,我遇到的唯一问题是我无法在youtubePlayerView上获得onClick回调。当用户点击youtube播放器时,我试图为youtube视频标题干杯。它在youtube播放器中没有加载视频时工作正常,但是一旦我初始化youtubePlayerView并且loadVideo clickListener开始不起作用。如何在youtube播放器中处理clickListener?
这是我的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:onClick="onClick"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/player">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtubePlayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:background="@color/black"/>
</RelativeLayout>
<include layout="@layout/video_player_bottom_sheet"/>
</android.support.design.widget.CoordinatorLayout>
我播放视频的代码:
@Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return mYouTubePlayerView;
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
mYoutubePlayer = youTubePlayer;
mYoutubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
if (!wasRestored) {
mYoutubePlayer.loadVideo("0g0sy2x_vX4");
}
}
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),videoTitle,Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:0)
从您为方法@Override
定义的方法中删除onClick()
,并将其添加到xml
android:clickable="true"