在活动中重新加载youtubeplayer?

时间:2015-06-04 03:07:15

标签: android listview youtube-api

请帮助我,

我想在用户点击列表视图中的项目时使用>点击视频重新加载活动(像youtube app 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);
}

}

1 个答案:

答案 0 :(得分:0)

这可以通过不同的方式完成。

  1. 您可以使用简单的WebView加载这些视频。

  2. 您还可以使用官方&#34; YouTube Android播放器API&#34;

  3. 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"/>
    

    然后显示这样的视频,

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