问题
I'm attempting to build a very simple video player using the Youtube API for Android. So far I have one video (which can be clicked and played) and two thumbnails below it - my problem is: I need to figure out how I can (correctly) assign different videos to each thumbnail - then play them.
I've attempted assign the thumbnails so using:
public static final String VIDEO1_ID = "HNtBphqE_LA";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "Ak--L4bYly0";
But for some reason I get the correct thumbnail displayed and video played for both youTubeThumbnailLoader1 and youTubeThumbnailLoader2.
Any suggestions? I simply need to tweak/modify the code below to show unique videos/thumbnails (instead of the same one repeated multiple times as it is now) and play them.
Screenshot
Code:
@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) {
}
}
}
P.S.
I think this may have to do with the fact I'm using youTubeThumbnailView1 and youTubeThumbnailView2 to create multiple thumbnails - however I have a feeling this might not be the correct way of doing so.
回答1:
You didn't set the right video for your thumbnails. There is no difference inside each thumbnailView clickListener!
try something like this in your 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();
}
});
}
Note: I see a lot of messy stuff in your code.
@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);
}
This part shows that you lack basic understanding of programming. You could simply just write:
@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
thumbnailLoader.setVideo(VIDEO1_ID);
thumbnailLoader.setVideo(VIDEO2_ID);
}
which is clearly not what you intended. I guess you have to create different Listeners in your onCreate. Dont pass this
inside the initialze
method of your thumbnailViews and make the same switch I show you for the clickListener.
Also Try to nail down the problem you have and post only relating stuff.
来源:https://stackoverflow.com/questions/20103564/youtube-api-does-not-play-videos-as-expected