我試圖用Android的Youtube API構建一個非常簡單的視頻播放器。到目前爲止,我有一個視頻(可以點擊和播放)和兩個縮略圖下面 - 我的問題是:我需要弄清楚如何(正確)將不同的視頻分配給每個縮略圖 - 然後播放它們。YouTube API不按預期播放視頻
我已經嘗試這樣分配使用縮略圖:
public static final String VIDEO1_ID = "HNtBphqE_LA";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "Ak--L4bYly0";
但由於某些原因,我得到了正確的縮略圖顯示和視頻的播放都youTubeThumbnailLoader1和youTubeThumbnailLoader2。
有什麼建議嗎?我只需要調整/修改下面的代碼來顯示獨特的視頻/縮略圖(而不是像現在那樣重複多次)並播放它們。
代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
youTubeThumbnailView1 = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview1);
youTubeThumbnailView1.initialize(API_KEY, this);
youTubeThumbnailView2 = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview2);
youTubeThumbnailView2.initialize(API_KEY, this);
youTubeThumbnailView1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(youTubePlayer != null){
youTubePlayer.play();
}
}});
youTubeThumbnailView2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(youTubePlayer != null){
youTubePlayer.play();
}
}});
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
youTubePlayer = player;
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
@Override
public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
YouTubeInitializationResult error) {
}
@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
youTubeThumbnailLoader1 = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader1.setVideo(VIDEO1_ID);
youTubeThumbnailLoader2 = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader2.setVideo(VIDEO2_ID);
}
private final class ThumbnailLoadedListener implements
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
@Override
public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
}
}
}
附:
我認爲這可能與我使用youTubeThumbnailView1和youTubeThumbnailView2創建多個縮略圖的事實有關 - 但是我有一種感覺,這可能不是這樣做的正確方式。
任何人有任何建議嗎? – JavaNoob