5

I am trying to have two YouTubePlayerViews on top of each other. They both show up, but only one seems to cue any videos. The second one (youTubeView2) is just black. No errors. Any idea what I am doing wrong?

JSONObject data;
JSONArray array;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playerview_demo);

YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);
YouTubePlayerView youTubeView2 = (YouTubePlayerView) findViewById(R.id.youtube_view2);
youTubeView2.initialize(DeveloperKey.DEVELOPER_KEY, this);



//JSONObject json = getJSONfromURL("http://gdata.youtube.com/feeds/api/users/rhettandlink2/uploads?v=2&alt=jsonc");

try{
    JSONObject json = new RetreiveFeedTask().execute().get();
    data = json.getJSONObject("data");
    array = data.getJSONArray("items");
    //Log.d("num vids", ""+id[1].toString());
    for (int i = 0; i < array.length(); i++) {
        final JSONObject item = array.getJSONObject(i);
        final JSONObject player = item.getJSONObject("player");
        final String url = player.getString("default");
        // The url is that of the video to be played
        //Log.d("GMM Videos", url.toString());


        final String id = item.getString("id");


    }}
catch(JSONException e) {
    Log.e("GMM Videos", "Error parsing data "+e.toString());
 } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  } catch (ExecutionException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }



}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
  boolean wasRestored) {
if (!wasRestored) {
    List<String> myList = new ArrayList<String>();
    try{
    for (int i = 0; i < array.length(); i++) {
        final JSONObject item = array.getJSONObject(i);
        final JSONObject jplayer = item.getJSONObject("player");
        final String url = jplayer.getString("default");
        // The url is that of the video to be played
        //Log.d("GMM Videos", url.toString());


        final String id = item.getString("id");
        //allVideos[i] = player.cueVideo(id);
        myList.add(id);

    }}
    catch(JSONException e) {
        Log.e("GMM Videos", "Error parsing data "+e.toString());
   }



  player.cueVideos(myList);
}
}
Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
  • `onInitializationSuccess` is called for each of the `YouTubePlayerViews`? – gipi Jun 15 '13 at 17:38
  • Yes, it is. But, only one *seems* to initialize. – Eric Cochran Jun 15 '13 at 21:15
  • try to add the callback for the [errors](https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer.OnInitializedListener#onInitializationFailure(com.google.android.youtube.player.YouTubePlayer.Provider, com.google.android.youtube.player.YouTubeInitializationResult)) – gipi Jun 16 '13 at 09:13
  • @EricCochran did you solve this issue? – Eliran Tutia May 24 '16 at 17:48

2 Answers2

12

My understanding is that this is the intended behavior. (It may or may not have something to do with limitations associated with hardware accelerated video playback.) What you're seeing is the YouTube Android Player SDK enforcing a one-video-at-a-time limitation.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • 2
    Yeah, it looks like you are right. The trick is to use multiple thumbnails that open a single player (fragment, in my case and the case of the api demos). Thanks. – Eric Cochran Jun 18 '13 at 19:34
  • The restriction is still there, is there any proper workaround for this ? I want to play multiple you-tube videos in a single screen but due to this i can't. – KunalK Aug 08 '16 at 04:15
0

You don't even need to load two videos at once, just use the function cueVideos(), and put a list string variable inside, and it will play one video after another. Extremely convenient :)

Kelsey
  • 174
  • 14