我找到了一个非常有用的教程,介绍如何创建YouTube视频的列表视图:
http://blog.blundell-apps.com/show-youtube-user-videos-in-a-listview/
然后我又按照另一个教程,使listview中的视频可点击:
http://blog.blundell-apps.com/click-item-in-a-listview-to-show-youtube-video/
我的问题是我不想用youtube应用程序播放视频 - 我想在我的应用程序中播放它们 - 所以我创建了一个youtube播放器的实例:
http://android-er.blogspot.com/2013/06/simple-example-using-youtube-android.html
我的问题是如何修改我当前的onClick监听器,以便在我的onClick事件期间将视频的ID传递给我的应用程序的内部播放器而不是youtube应用程序?
onClick Listener (currently passing to standalone YouTube app):
@Override
public void onVideoClicked(Video video) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(video.getUrl()));
startActivity(intent);
}
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener{
public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
public static final String VIDEO_ID = "o7VVHhK9zf0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(),
"onInitializationFailure()",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
}
答案 0 :(得分:1)
类似的东西:
@Override
public void onVideoClicked(Video video) {
Intent intent = new Intent(this, MainActivity.class); // Your youtubeplayer activity
intent.putExtra("video_id",video_id);
startActivity(intent);
}
在MainActivity
onCreate()中,您获得了ID并将其传递给您的Youtube播放器:
getIntent().getStringExtra("video_id");