Video is not playing in YoutubePlayer Fragment

六眼飞鱼酱① 提交于 2020-01-25 20:30:10

问题


I am making an app which uses Youtube player fragment. It is loaded but video is not getting played. Here is my code:

Youtube.java (Fragment)

public class Youtube extends YouTubePlayerFragment {


    public Youtube() {
    }

    public static Youtube newInstance(String url) {

        Youtube frag = new Youtube();

        Bundle b = new Bundle();
        b.putString("url", url);

        frag.setArguments(b);
        frag.init();

        return frag;
    }

    private void init() {

        initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
            }

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
                if (!wasRestored) {
                    player.cueVideo(getArguments().getString("url"));
                   // player.play();
                }
            }
        });
    }
}

Activity:

    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        Youtube f = Youtube.newInstance("https://www.youtube.com/watch?v=o49aHgzTOGw");
        FragmentManager fragmentManager=getFragmentManager();
        fragmentManager.beginTransaction().add(f,"Fragment").commit();
    }

Layout:

<FrameLayout>
    <fragment class="com.google.android.youtube.player.YouTubePlayerFragment"
              android:id="@+id/Fragment"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
</FrameLayout>

Where I am going wrong. I am not able to figure it out. Do I need to extend YoutubeBaseActivity? How to achieve this?


回答1:


I think you should write the code in official way. Take look at here, and download the zip file(sample applications included in the YouTubeAndroidAPIDemo package) here to get the sample. And take look at FragmentDemoActivity.

Basically, you need to do the Activity which include YoutubePlayerFragment as follows:

public class FragmentDemoActivity extends YouTubeFailureRecoveryActivity {

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

    setContentView(R.layout.fragments_demo);

    YouTubePlayerFragment youTubePlayerFragment =
        (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
    youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
  }

  @Override
  public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
      boolean wasRestored) {
    if (!wasRestored) {
      player.cueVideo("nCgQDjiotG0");
    }
  }

  @Override
  protected YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
  }
}

YouTubeFailureRecoveryActivity :

public abstract class YouTubeFailureRecoveryActivity extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener {

  private static final int RECOVERY_DIALOG_REQUEST = 1;

  @Override
  public void onInitializationFailure(YouTubePlayer.Provider provider,
      YouTubeInitializationResult errorReason) {
    if (errorReason.isUserRecoverableError()) {
      errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
    } else {
      String errorMessage = String.format(getString(R.string.error_player), 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(DeveloperKey.DEVELOPER_KEY, this);
    }
  }

  protected abstract YouTubePlayer.Provider getYouTubePlayerProvider();

}

And the fragments_demo.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <fragment
      android:name="com.google.android.youtube.player.YouTubePlayerFragment"
      android:id="@+id/youtube_fragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</LinearLayout>


来源:https://stackoverflow.com/questions/32218732/video-is-not-playing-in-youtubeplayer-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!