我正在开发儿童应用,其中我在RecyclerView中显示了youtube视频缩略图。在点击任何列出的视频缩略图上,视频将全屏播放(使用YouTube API获取缩略图并在Youtube意图中播放视频)。我很成功,我做到了这一切。问题是我想在视频播放时禁用触摸屏事件。各种帮助表示赞赏。以下是我的代码:
mainactivity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical"
tools:context="com.example.pc.fkidshell.Main4Activity">
<include layout="@layout/toolbar"
android:id="@+id/my_thirdtoolbar"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/VideoList"
android:layout_below="@+id/my_thirdtoolbar"
android:paddingTop="20dp"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
video_row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/thumbnailView"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
mainactivity.java:
public class Main4Activity extends AppCompatActivity implements YouTubeThumbnailView.OnInitializedListener, YouTubeThumbnailLoader.OnThumbnailLoadedListener{
Toolbar third_toolbar;
YouTubeThumbnailView thumbnailView;
YouTubeThumbnailLoader thumbnailLoader;
RecyclerView VideoList;
RecyclerView.Adapter adapter;
List<Drawable> thumbnailViews;
List<String> VideoId;
String videoid;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
third_toolbar = (Toolbar) findViewById(R.id.my_thirdtoolbar);
setSupportActionBar(third_toolbar);
getSupportActionBar().setTitle(R.string.sectitle);
getSupportActionBar().setIcon(R.drawable.tblogo);
thumbnailViews = new ArrayList<>();
VideoList = (RecyclerView) findViewById(R.id.VideoList);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
VideoList.setLayoutManager(layoutManager);
adapter=new VideoListAdapter();
VideoList.setAdapter(adapter);
VideoId = new ArrayList<>();
thumbnailView = new YouTubeThumbnailView(this);
thumbnailView.initialize("API Key", this);
}
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
thumbnailLoader = youTubeThumbnailLoader;
youTubeThumbnailLoader.setOnThumbnailLoadedListener(Main4Activity.this);
thumbnailLoader.setPlaylist("PLXRActLQ03oY_6AQb-5EMuKFYQA_fDE40");
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
}
public void add() {
adapter.notifyDataSetChanged();
if (thumbnailLoader.hasNext())
thumbnailLoader.next();
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
thumbnailViews.add(youTubeThumbnailView.getDrawable());
VideoId.add(s);
add();
}
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
}
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.MyView>{
public class MyView extends RecyclerView.ViewHolder{
ImageView imageView;
public MyView(View itemView) {
super(itemView);
imageView= (ImageView) itemView.findViewById(R.id.thumbnailView);
}
}
@Override
public VideoListAdapter.MyView onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_row, parent, false);
return new MyView(itemView);
}
@Override
public void onBindViewHolder(VideoListAdapter.MyView holder, final int position) {
holder.imageView.setImageDrawable(thumbnailViews.get(position));
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
videoid=VideoId.get(position);
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v="+videoid+"&index="+position+"&list=PLXRActLQ03oY_6AQb-5EMuKFYQA_fDE40"));
intent.putExtra("force_fullscreen",true);
startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return thumbnailViews.size();
}
}
}
答案 0 :(得分:0)
我的解决方案,你应该YoutubePlayerActivity.java
public class YoutubePlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,
YouTubePlayer.PlaybackEventListener {
public static final String KEY_VIDEO_ID = "videoId";
private static final int RECOVERY_REQUEST = 1;
private static final String API_KEY = "your API_KEY";
private YouTubePlayerView youTubePlayerView;
private View topLayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube_player);
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(API_KEY, this);
topLayer = findViewById(R.id.top_layer);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
topLayer.setLayoutParams(new RelativeLayout.LayoutParams(dm.widthPixels - 1, dm.heightPixels - 1));
topLayer.setClickable(true);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer.setPlaybackEventListener(this);
youTubePlayer.loadVideo(getIntent().getStringExtra(KEY_VIDEO_ID));
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError()) {
youTubeInitializationResult.getErrorDialog(this, RECOVERY_REQUEST).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
// Retry initialization if user performed a recovery action
youTubePlayerView.initialize(API_KEY, this);
}
}
@Override
public void onPlaying() {
topLayer.setVisibility(View.VISIBLE);
}
@Override
public void onPaused() {
}
@Override
public void onStopped() {
topLayer.setVisibility(View.GONE);
}
@Override
public void onBuffering(boolean b) {
}
@Override
public void onSeekTo(int i) {
}
}
创建布局,我将其命名为activity_youtube_player.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false" />
<View
android:id="@+id/top_layer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:clickable="true" />
</RelativeLayout>
您可以使用
启动YoutubePlayerActivity
Intent openYoutubePlayerActivity = new Intent(context, YoutubePlayerActivity.class);
openYoutubePlayerActivity.putExtra(YoutubePlayerActivity.KEY_VIDEO_ID, videoId);
startActivity(openYoutubePlayerActivity);
那么,代码的作用是什么?它将自动播放YouTube视频,并在视频开始时在YouTubePlayerView
顶部显示透明图层。透明图层上的android:clickable="true"
属性会阻止任何直接点击YouTubePlayerView
。视频停止后(通过错误或视频结束),它将删除透明层。如果需要,您也可以直接在finish();
方法中致电onStopped()
。