2015-06-04 73 views
-4

請幫助我,重新加載youtubeplayer的活動?

我要當列表視圖用戶點擊項目 - >視頻刷新活動點擊(如YouTube應用程式android)

Pleale幫助我的想法。由於

You can view here

更新我的源代碼:

公共類YoutubePlayerActivity擴展AppCompatActivity 實現YouTubePlayer.OnInitializedListener {

private static final int RECOVERY_DIALOG_REQUEST = 1; 

YouTubePlayerFragment myYouTubePlayerFragment; 
private Toolbar mToolbar; 
private String name; 
private String countView; 
private String youtubeId = "fhWaJi1Hsfo"; 
private String cate; 
private RecyclerView mRecyclerView; 
private LinearLayoutManager mLayoutManager; 
private ClipAdapter mClipAdapter; 
private List<ClipItemData> clipItemDatas = new ArrayList<>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_youtube_player); 

    mRecyclerView = (RecyclerView) findViewById(R.id.youtube_recycler_view); 

    // use this setting to improve performance if you know that changes 
    // in content do not change the layout size of the RecyclerView 
    mRecyclerView.setHasFixedSize(true); 

    // use a linear layout manager 
    mLayoutManager = new LinearLayoutManager(this); 
    mRecyclerView.setLayoutManager(mLayoutManager); 
    final LinearLayoutManager mLayoutManager; 
    mLayoutManager = new LinearLayoutManager(this); 
    mRecyclerView.setLayoutManager(mLayoutManager); 

    for (int i = 0; i < 50; i++) { 
     ClipItemData clipItemData = new ClipItemData("To0tJu75ugg", 
       "Test", 
       "123", 
       "XE", 
       "http://img.youtube.com/vi/" + "To0tJu75ugg" + "/0.jpg"); 

     clipItemDatas.add(clipItemData); 
    } 

    // specify an adapter (see also next example) 
    mClipAdapter = new ClipAdapter(this, clipItemDatas); 
    mRecyclerView.setAdapter(mClipAdapter); 

    Bundle getBundle = null; 
    getBundle = this.getIntent().getExtras(); 
    youtubeId = getBundle.getString("id"); 
    name = getBundle.getString("name"); 
    countView = getBundle.getString("count_view"); 
    cate = getBundle.getString("cate"); 
    myYouTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager() 
      .findFragmentById(R.id.youtubeplayerfragment); 
    myYouTubePlayerFragment.initialize(AppConfig.YOUTUBE_ANDROID_KEY, this); 

    mToolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(mToolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() { 
     @Override 
     public void onItemClick(View view, int position) { 
      youtubeId = "To0tJu75ugg"; 
      Toast.makeText(getApplicationContext(), youtubeId, Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onItemLongClick(View view, int position) { 
     } 
    })); 

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     // Respond to the action bar's Up/Home button 
     case android.R.id.home: 
      super.onBackPressed(); 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) { 
    if (!wasRestored) { 
     youTubePlayer.loadVideo(youtubeId); 
    } 

} 

@Override 
public void onInitializationFailure(YouTubePlayer.Provider provider, 
            YouTubeInitializationResult errorReason) { 
    if (errorReason.isUserRecoverableError()) { 
     errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show(); 
    } else { 
     String errorMessage = String.format(
       "There was an error initializing the YouTubePlayer (%1$s)", 
       errorReason.toString()); 
     Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); 
    } 
} 

@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(AppConfig.YOUTUBE_ANDROID_KEY, this); 
    } 
} 

protected YouTubePlayer.Provider getYouTubePlayerProvider() { 
    return (YouTubePlayerView) findViewById(R.id.youtubeplayerfragment); 
} 

}

回答

0

這可以在不同的完成方法。

  1. 您可以使用簡單的WebView加載這些視頻。

  2. 您還可以使用官方的 「Android版YouTube播放器API」

YouTube Android Player API

實施的Youtube的API是容易的,

首先建立在XML一個YouTubePlayerView

<com.google.android.youtube.player.YouTubePlayerView 
     android:id="@+id/youtubeplayerview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

然後顯示一個vi DEO這個樣子,

public class MainActivity extends YouTubeBaseActivity implements 
YouTubePlayer.OnInitializedListener{ 

public static final String API_KEY = "YOUR_API_KEY"; 
public static final String VIDEO_ID = "YOUR_VIDEO_ID"; 

    @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

謝謝Prokash薩卡, 我要重新加載視頻時,我點擊項列表視圖,然後在當前活動 P/S重裝:對於第一個我的問題的不便深表歉意 – huynn109

+0

你可以爲您的ListView項目添加一個onClickListener()。點擊一個ListView項目後,根據點擊它的位置獲取URL,然後簡單地爲您的活動的WebView/YouTubePlayerView設置一個新的URL。如果您不知道如何使用ListView的自定義適配器並檢測單個List項目的單擊,請查看此處,http://www.learn2crack.com/2013/10/android-custom-listview-images-text -example.html –

+0

謝謝,我解決了。 我不能爲你投票。 – huynn109

相關問題