1

我想在recylerview中顯示視頻。它顯示了其中的一些,當我點擊播放時,它不顯示視頻播放,但在後臺我可以聽到音頻。我想要顯示所有視頻。一些Youtube視頻顯示在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

+0

與回收站相關的代碼在哪裏? –

+0

@Ram我添加了代碼,請檢查。 – Ravi

+0

創建一個包含Recyclerview的活動,並將包含視圖的適配器設置爲YoutubePlayerFragment –

回答

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); 
     } 
    } 
} 
+0

爲什麼** Resources.KEY **,未解決@Carlos –

+0

@LutaayaHuzaifahIdris「Resources.Key」只不過是「您的API密鑰」 –

+0

Yah我懂了 –