Youtube Player を Fragment に埋め込む

今や公開されてるAPIも多すぎて...

プレーヤーを WebView なしで埋め込みたいだけなのに...

YouTube_—_Google_Developers

YouTube — Google Developers

いちいち調べるのが面倒なので手順メモ.

・ libs/ 以下にタウンドードした jar を置く.

dependencies {
    compile files('libs/YouTubeAndroidPlayerApi.jar')
}

・ API11+ であれば全て使える.
DEVELOPER_KEY を取得する必要がある.

・ 画面の回転時の再読み込みを AndroidManifest.xml で考慮.

    <activity
        ...
        android:screenOrientation="nosensor"
        android:configChanges="orientation|screenSize|keyboardHidden”>

・ 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();

}

・ Activity の親に YouTubeFailureRecoveryActivity を.

public class PlayerViewDemoActivity extends YouTubeFailureRecoveryActivity {

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

  @Override
  public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
      boolean wasRestored) {
    if (!wasRestored) {
      player.cueVideo("wKJ9KzGQq0w”); // 動画ID
    }
  }

  @Override
  protected YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view); // プレーヤーID
  }

}

・ レイアウト (layout/playerview_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">

  <TextView
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:textAppearance="@android:style/TextAppearance.Small"
      android:gravity="center"
      android:text="@string/playerview_text"/>

  <com.google.android.youtube.player.YouTubePlayerView
      android:id="@+id/youtube_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</LinearLayout>

という雰囲気.

Fragment 内に埋め込むなら, Activity が固定されてるものとして,

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     int section = getArguments().getInt(ARG_SECTION_NUMBER);
     View rootView = inflater.inflate(R.layout.playerview_demo, container, false);


     // 処理開始
     YouTubePlayerView youTubeView = (YouTubePlayerView) rootview.findViewById(R.id.youtube_view);

     youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY,
                                      (YouTubePlayer.OnInitializedListener)getActivity());
   }

YouTube Android Player API - YouTube — Google Developers
サンプルアプリケーション - YouTube — Google Developers


関連ワード:  AndroidAndroidStudio開発