- Home /
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(android.R.id.content);
// 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);
view.setLayoutParams(lpView);
_layout = new LinearLayout(_context);
_layout.setOrientation(LinearLayout.VERTICAL);
_layout.setBackgroundColor(0xffffffff);
LinearLayout.LayoutParams lpLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, leafParent.getWidth());
lpLayout.weight = 1.0f;
lpLayout.gravity = Gravity.CENTER;
_layout.setLayoutParams(lpLayout);
_layout.addView(view);
_layout.setId(0x20348);
YouTubePlayerFragment fragment = YouTubePlayerFragment.newInstance();
FragmentManager fragmentManager = UnityPlayer.currentActivity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(_layout.getId(), fragment);
fragmentTransaction.commit();
_videoId = videoId;
fragment.initialize(DEVELOPER_KEY, _instance);
leafParent.addView(_layout);
}
}
}
});
UnitySend("Initialized");
}
Answer by Dave-Hampson · Mar 30, 2016 at 03:54 PM
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"
Your answer
Follow this Question
Related Questions
Homebase SDK android Plugin Help 0 Answers
Bad performance on quad core Android phones 0 Answers
Android Unity Plugin problem 0 Answers
Turn your Android device webcam (Camera) , flash light 3 Answers
android - native twitter connect 0 Answers