如何在ListView中刷新Youtube播放器视图onItemClickListener()

我能够使用cuePlaylist()播放Youtubevideo,但我也想让用户点击任何列表项目,然后我想刷新YoutubePlayerView与video用户刚刚点击

我正在使用cuePlaylist()因此获得上一个和下一个button作为Youtube播放器的默认function

那么我可以用我在ListViewselect的那个刷新YoutubePlayerView吗?

这里是我完整的代码,仍然当我点击任何列表项,没有得到任何YoutubePlayerView变化,但能够Log我刚刚点击一个ListViewvideoID …

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { strVideoID = flowerList.get(i).getUrl(); Log.d("url:", strVideoID); // getting particular video id youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() { // YouTubeプレーヤーの初期化成功@Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { if (!wasRestored) { player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT); player.loadVideo(strVideoID); player.play(); } } // YouTubeプレーヤーの初期化失敗@Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) { // YouTube error String errorMessage = error.toString(); Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_LONG).show(); Log.d("errorMessage:", errorMessage); } }); } }); } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { if (!b) { youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT); youTubePlayer.cuePlaylist(PLAYLIST_ID); } } 

Related of "如何在ListView中刷新Youtube播放器视图onItemClickListener()"

你在这里使用了错误的方法,你不需要每次调用YoutubePlayerFragment初始化,第一次初始化就足够了,你在onCreate方法

 YouTubePlayerFragment youTubePlayerFragment = YouTubePlayerFragment.newInstance(); youTubePlayerFragment.initialize(API_KEY, this); 

在Activity中实现的初始化监听器中,您应该像这样将YoutubePlayer的引用保存在类级别属性中

  //your class level attribute to keep reference of player YouTubePlayer mYoutubePlayer; @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { if (!b) { youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT); youTubePlayer.cuePlaylist(PLAYLIST_ID); //Save reference of initialized player in class level attribute mYoutubePlayer = youTubePlayer; } } 

并使用此播放器属性加载onItemClick内的video,而不是再次调用YoutubePlayerFragment与新的监听器初始化

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { strVideoID = flowerList.get(i).getUrl(); Log.d("url:", strVideoID); // getting particular video id if(mYoutubePlayer!=null){ mYoutubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT); mYoutubePlayer.loadVideo(strVideoID); mYoutubePlayer.play(); } } }); 

一步打任何你pipevideo

1在xml文件中创build一个你pipe的视图。

 <com.google.android.youtube.player.YouTubePlayerView android:id="@+id/youtube_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="30dp" /> 
  1. 里面的java文件

     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(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(DEVELOPER_KEY, this); } } private YouTubePlayer.Provider getYouTubePlayerProvider() { return (YouTubePlayerView) findViewById(R.id.youtube_view); } }