2013-11-20 222 views
1

我試圖用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。

有什麼建議嗎?我只需要調整/修改下面的代碼來顯示獨特的視頻/縮略圖(而不是像現在那樣重複多次)並播放它們。

Screenshot

代碼:

@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創建多個縮略圖的事實有關 - 但是我有一種感覺,這可能不是這樣做的正確方式。

+0

任何人有任何建議嗎? – JavaNoob

回答

1

您沒有爲縮略圖設置正確的視頻。每個thumbnailView clickListener都沒有區別!

嘗試在您的onCreate

public void onCreate(Bundle icicle){ 
    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(){ 
     if(youTubePlayer != null){ 
      youTubePlayer.loadVideo(VIDEO1_ID) 
      youTubePlayer.play(); 
     } 
    }); 

    youTubeThumbnailView2.setOnClickListener(new OnClickListener(){ 
     if(youTubePlayer != null){ 
      youTubePlayer.loadVideo(VIDEO2_ID) 
      youTubePlayer.play(); 
     } 
    }); 

} 

說明是這樣的:我看到了很多亂七八糟的東西,在你的代碼。

@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); 
} 

這部分顯示您對編程缺乏基本的瞭解。你可以簡單地只寫:

@Override 
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView, 
    YouTubeThumbnailLoader thumbnailLoader) { 

    thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener()); 
    thumbnailLoader.setVideo(VIDEO1_ID); 
    thumbnailLoader.setVideo(VIDEO2_ID); 

} 

,這顯然是不什麼打算。我想你必須在你的onCreate中創建不同的監聽器。不要在你的thumbnailViews的initialze方法內部通過this,並使我向clickListener顯示的相同開關。

另請嘗試確定您擁有的問題併發布相關內容。