YouTube Player Fragment in Plugin

I am trying to get a YouTube player to work in a Unity app.

I am using the Android API code and the YouTubePlayerFragment.

The player shows up just fine and plays the video, sort of. You can hear the audio and see the progress spinner, but when the video itself starts streaming all you see if the background image in Unity. If you sleep the phone and then wake it up you can then see the video.

If I do this exact same thing in an actual Android app it works fine.

I have tried everything I can think of. I have made the layout it is in have a background color and all kinds of stuff. I just can’t get the video to show up right off.

Here is my code:

    private View getLeafView(View view) {
        if (view instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup)view;
            for (int i = 0; i < vg.getChildCount(); ++i) {
                View chview = vg.getChildAt(i);
                View result = getLeafView(chview);
                if (result != null) 
                    return result;
            return null;
        else {
            return view;
	public void Init(final String videoId, String objectName, final int heightOffset) {
		UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
			public void run() {
				_rootView = (ViewGroup)UnityPlayer.currentActivity.findViewById(;
                // find the first leaf view (i.e. a view without children)
                // the leaf view represents the topmost view in the view stack
                View topMostView = getLeafView(_rootView);
                if (topMostView != null) {
	                // let's add a sibling to the leaf view
	                ViewGroup leafParent = (ViewGroup)topMostView.getParent();
	                if (leafParent != null) {
	                	LinearLayout.LayoutParams lpView = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	                	lpView.weight = 1.0f;
	                	lpView.gravity = Gravity.CENTER;
	                	View view = new View(_context);
	                	_layout = new LinearLayout(_context);
	                	LinearLayout.LayoutParams lpLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, leafParent.getWidth());
	                	lpLayout.weight = 1.0f;
	                	lpLayout.gravity = Gravity.CENTER;
						YouTubePlayerFragment fragment = YouTubePlayerFragment.newInstance();
					    FragmentManager fragmentManager = UnityPlayer.currentActivity.getFragmentManager();
					    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
					    fragmentTransaction.add(_layout.getId(), fragment);

					    _videoId = videoId;
						fragment.initialize(DEVELOPER_KEY,  _instance);

From the Android Team:

“If the YouTube player is going to cover the unity surface I would suggest you put the youtube widget either in a separate activity or inside a dialog frame. That way you can avoid sorting the surfaces and rely on android to do the correct thing”