一些Youtube视频显示在RecylerView中

时间:2016-04-05 13:12:05

标签: android android-recyclerview youtube-data-api recycler-adapter

我正在尝试在recylerview中显示视频。它显示了其中一些,当我点击播放时,它不显示视频播放但在后台我可以听到音频。我想要显示所有视频。

package com.example.videosample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.PlayerStyle;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

public class MainActivity extends YouTubeBaseActivity implements
 YouTubePlayer.OnInitializedListener {

private static final int RECOVERY_DIALOG_REQUEST = 1;

// YouTube player view
private YouTubePlayerView youTubeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_main);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);

 // Initializing video player with developer key
 youTubeView.initialize(Config.DEVELOPER_KEY, this);
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
 YouTubeInitializationResult errorReason) {
 if (errorReason.isUserRecoverableError()) {
 errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show(); 
 } else {
 String errorMessage = String.format(
        getString(R.string.error_player), errorReason.toString());
 Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {

// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
player.loadVideo(Config.YOUTUBE_VIDEO_CODE);

// Hiding player controls
 player.setPlayerStyle(PlayerStyle.CHROMELESS);
 }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent       data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
 getYouTubePlayerProvider().initialize(Config.DEVELOPER_KEY, this);
  }
  }
  private YouTubePlayer.Provider getYouTubePlayerProvider() {
  return (YouTubePlayerView) findViewById(R.id.youtube_view);
 }
}

RecylcerActivity.java

 public class RecyclerActivity extends YouTubeBaseActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_layout);
    RecyclerView recyclerView=(RecyclerView)findViewById(R.id.list);
    recyclerView.setHasFixedSize(true);
    //to use RecycleView, you need a layout manager. default is       LinearLayoutManager
    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    RecyclerAdapter1 adapter=new RecyclerAdapter1(RecyclerActivity.this);
    recyclerView.setAdapter(adapter);
 }

 @Override
 protected void onStop()
 {
    super.onStop();
 }
}

see snapshot here

1 个答案:

答案 0 :(得分:1)

我会使用这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="@drawable/image_ripple"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="0dp"
        android:layout_marginTop="10dp"
        card_view:cardBackgroundColor="@android:color/black"
        android:background="@drawable/border_single_pixel"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardUseCompatPadding="true">


    <com.google.android.youtube.player.YouTubeThumbnailView
        android:id="@+id/youtube_thumbnail"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType="centerCrop"
        android:visibility="visible"/>

    <RelativeLayout android:id="@+id/relativeLayout_over_youtube_thumbnail"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@color/color_background_transparent"
        android:visibility="visible">

        <ImageView android:id="@+id/btnYoutube_player"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center"
            android:src="@mipmap/ic_youtube_play_button"/>

    </RelativeLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>

RecyclerView适配器

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.VideoInfoHolder> {

    //these ids are the unique id for each video
    String[] VideoID = {"ndBdf1_oOGA", "7V-fIGMDsmE", "f7ihSQ44WO0"};
    Context ctx;

    public RecyclerAdapter(Context context) {
        this.ctx = context;
    }

    @Override
    public VideoInfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new VideoInfoHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final VideoInfoHolder holder, final int position) {


        final YouTubeThumbnailLoader.OnThumbnailLoadedListener  onThumbnailLoadedListener = new YouTubeThumbnailLoader.OnThumbnailLoadedListener(){
            @Override
            public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {

            }

            @Override
            public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
                youTubeThumbnailView.setVisibility(View.VISIBLE);
                holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
            }
        };

        holder.youTubeThumbnailView.initialize(Resources.KEY, new YouTubeThumbnailView.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {

                youTubeThumbnailLoader.setVideo(VideoID[position]);
                youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
            }

            @Override
            public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
                 //In case of failure
            }
        });
    }

    @Override
    public int getItemCount() {
        return VideoID.length;
    }

    public class VideoInfoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        protected RelativeLayout relativeLayoutOverYouTubeThumbnailView;
        YouTubeThumbnailView youTubeThumbnailView;
        protected ImageView playButton;

        public VideoInfoHolder(View itemView) {
            super(itemView);
            playButton=(ImageView)itemView.findViewById(R.id.btnYoutube_player);
            playButton.setOnClickListener(this);
            relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
            youTubeThumbnailView = (YouTubeThumbnailView) itemView.findViewById(R.id.youtube_thumbnail);
        }

        @Override
        public void onClick(View v) {

            Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) ctx, Resources.KEY, VideoID[getLayoutPosition()]);
            ctx.startActivity(intent);
        }
    }
}