ExoPlayer デモ アプリケーション

ExoPlayer のメインのデモアプリには、主に次の 2 つの目的があります。

  1. ExoPlayer の使用方法の完全な例を提供するため。デモアプリは、独自のアプリを開発するための便利な出発点として使用できます。
  2. ExoPlayer を試すため。デモアプリを使用すると、付属のサンプルに加えて、独自のコンテンツの再生をテストできます。

このページでは、デモアプリを取得、コンパイル、実行する方法について説明します。また、デモアプリを使用して独自のメディアを再生する方法についても説明します。

コードの取得

メインのデモアプリのソースコードは、GitHub プロジェクトの demos/main フォルダにあります。まだクローンを作成していない場合は、プロジェクトをローカル ディレクトリにクローンします。

git clone https://github.com/androidx/media.git

次に、Android Studio でプロジェクトを開きます。Android プロジェクト ビューに次のように表示されます(デモアプリの関連フォルダが展開されています)。

Android Studio のプロジェクト
図 1. Android Studio のプロジェクト。

コンパイルと実行

デモアプリをコンパイルして実行するには、Android Studio で demo 構成を選択して実行します。デモアプリがインストールされ、接続された Android 搭載デバイスで実行されます。可能であれば、実機を使用することをおすすめします。代わりにエミュレータを使用する場合は、サポートされているデバイスのエミュレータのセクションを読み、仮想デバイスが API レベル 23 以上のシステム イメージを使用していることを確認してください。

ExoPlayer デモアプリの SampleChooserActivity と PlayerActivity
図 2. SampleChooserActivity と PlayerActivity。

デモアプリには、サンプル(SampleChooserActivity)のリストが表示されます。サンプルを選択すると、再生用の 2 つ目のアクティビティ(PlayerActivity)が開きます。このデモには、再生コントロールとトラック選択機能が用意されています。また、ExoPlayer の EventLogger ユーティリティ クラスを使用して、有用なデバッグ情報をシステムログに出力します。このロギングは、次のコマンドを使用して(他のタグのエラーレベルのロギングとともに)表示できます。

adb logcat EventLogger:V *:E

バンドルされたデコーダの有効化

ExoPlayer には、AV1、VP9、Opus、FLAC、FFmpeg(音声のみ)などのバンドルされたソフトウェア デコーダを使用できる拡張機能が多数あります。デモアプリは、次のように、これらの拡張機能を含めて使用するようにビルドできます。

  1. 含める各拡張機能を作成します。これは手動プロセスです。手順については、各拡張機能の README.md ファイルをご覧ください。
  2. Android Studio の [Build Variants] ビューで、次の画像に示すように、デモ モジュールのビルド バリアントを withDecoderExtensionsDebug または withDecoderExtensionsRelease に設定します。

    デモの「withDecoderExtensionsDebug」ビルド バリアントを選択する

  3. 通常どおりに demo 構成をコンパイル、インストール、実行します。

デフォルトでは、拡張機能デコーダは、適切なプラットフォーム デコーダが存在しない場合にのみ使用されます。以下のセクションで説明するように、拡張デコーダを優先するように指定できます。

独自のコンテンツを再生する

デモアプリで独自のコンテンツを再生する方法は複数あります。

1. assets/media.exolist.json の編集

デモアプリに表示されるサンプルは、assets/media.exolist.json から読み込まれます。この JSON ファイルを編集することで、デモアプリにサンプルを追加したり、デモアプリからサンプルを削除したりできます。スキーマは次のとおりです。[O] はオプションの属性を示します。

[
  {
    "name": "Name of heading",
    "samples": [
      {
        "name": "Name of sample",
        "uri": "The URI of the sample",
        "extension": "[O] Sample type hint. Cannot be combined with mime_type. Values: mpd, ism, m3u8",
        "clip_start_position_ms": "[O] A start point to which the sample should be clipped, in milliseconds",
        "clip_end_position_ms": "[O] An end point from which the sample should be clipped, in milliseconds",
        "drm_scheme": "[O] Drm scheme if protected. Values: widevine, playready, clearkey",
        "drm_license_uri": "[O] URI of the license server if protected",
        "drm_force_default_license_uri": "[O] Whether to force use of "drm_license_uri" for key requests that include their own license URI",
        "drm_key_request_properties": "[O] Key request headers if protected",
        "drm_session_for_clear_content": "[O] Whether to attach a DRM session to clear video and audio tracks",
        "drm_multi_session": "[O] Enables key rotation if protected",
        "drm_force_software_crypto": "[O] Whether to force DRM to use the software crypto library regardless of the device's hardware crypto capability if supported by the DRM scheme",
        "mime_type": "[O] The MIME type of the sample. Cannot be combined with extension.",
        "subtitle_uri": "[O] The URI of a subtitle sidecar file",
        "subtitle_mime_type": "[O] The MIME type of subtitle_uri (required if subtitle_uri is set)",
        "subtitle_language": "[O] The BCP47 language code of the subtitle file (ignored if subtitle_uri is not set)",
        "ad_tag_uri": "[O] The URI of an ad tag to load via the IMA extension"
      },
      ...etc
    ]
  },
  ...etc
]

サンプルのプレイリストは、次のスキーマを使用して指定できます。

[
  {
    "name": "Name of heading",
    "samples": [
      {
        "name": "Name of playlist sample",
        "playlist": [
          {
            "uri": "The URI of the first sample in the playlist",
            "extension": "[O] Sample type hint. Cannot be combined with mime_type. Values: mpd, ism, m3u8",
            "clip_start_position_ms": "[O] A start point to which the sample should be clipped, in milliseconds",
            "clip_end_position_ms": "[O] An end point from which the sample should be clipped, in milliseconds",
            "drm_scheme": "[O] Drm scheme if protected. Values: widevine, playready, clearkey",
            "drm_license_uri": "[O] URI of the license server if protected",
            "drm_force_default_license_uri": "[O] Whether to force use of "drm_license_uri" for key requests that include their own license URI",
            "drm_key_request_properties": "[O] Key request headers if protected",
            "drm_session_for_clear_content": "[O] Whether to attach a DRM session to clear video and audio tracks",
            "drm_multi_session": "[O] Enables key rotation if protected",
            "drm_force_software_crypto": "[O] Whether to force DRM to use the software crypto library regardless of the device's hardware crypto capability if supported by the DRM scheme",
            "mime_type": "[O] The MIME type of the sample. Cannot be combined with extension.",
            "subtitle_uri": "[O] The URI of a subtitle sidecar file",
            "subtitle_mime_type": "[O] The MIME type of subtitle_uri (required if subtitle_uri is set)",
            "subtitle_language": "[O] The BCP47 language code of the subtitle file (ignored if subtitle_uri is not set)"
          },
          {
            "uri": "The URI of the second sample in the playlist",
            ...etc
          },
          ...etc
        ]
      },
      ...etc
    ]
  },
  ...etc
]

必要に応じて、キー リクエスト ヘッダーを、各ヘッダーの文字列属性を含むオブジェクトとして指定します。

"drm_key_request_properties": {
  "name1": "value1",
  "name2": "value2",
  ...etc
}

サンプル選択アクティビティのオーバーフロー メニューには、拡張デコーダを優先するかどうかを指定するオプションがあります。

ローカル ファイルの URI と対象範囲別ストレージの制限

ローカル ファイルの URI を指定すると、デモアプリはこれらのファイルを読み取るために必要なストレージ アクセス権限をリクエストします。ただし、Android 13 以降では、一般的なメディア ファイル拡張子(.mp4 など)で終わらない任意のファイルを読み込むことはできません。そのようなファイルを読み込む必要がある場合は、アクセス制限のないデモアプリ固有のストレージ ディレクトリに配置できます。通常、これは /sdcard/Android/data/androidx.media3.demo.main/files にあります。

2. 外部の exolist.json ファイルを読み込む

デモアプリは、上記のスキーマを使用して、*.exolist.json 規則に従って名前が付けられた外部 JSON ファイルを読み込むことができます。たとえば、https://yourdomain.com/samples.exolist.json でこのようなファイルをホストしている場合は、次のコマンドを使用してデモアプリで開くことができます。

adb shell am start -a android.intent.action.VIEW \
    -d https://yourdomain.com/samples.exolist.json

デモアプリがインストールされているデバイスで *.exolist.json リンク(ブラウザやメール クライアントなど)をクリックすると、デモアプリでも開きます。*.exolist.json JSON ファイルをホストすると、他のユーザーがデモアプリで試せるようにコンテンツを配布できます。

3. インテントを起動する

インテントを使用すると、サンプルのリストをバイパスして、再生を直接開始できます。1 つのサンプルを再生するには、インテントのアクションを androidx.media3.demo.main.action.VIEW に設定し、そのデータ URI を再生するサンプルの URI に設定します。このようなインテントは、ターミナルから次のコマンドを使用して起動できます。

adb shell am start -a androidx.media3.demo.main.action.VIEW \
    -d https://yourdomain.com/sample.mp4

単一のサンプル インテントでサポートされているオプションの追加項目は次のとおりです。

  • 構成の追加例:
    • mime_type [文字列] サンプル MIME タイプのヒント。たとえば、DASH コンテンツの場合は application/dash+xml です。
    • clip_start_position_ms [Long] サンプルをクリップする開始点(ミリ秒単位)。
    • clip_end_position_ms [Long] サンプルをクリップするエンドポイント(ミリ秒単位)。
    • drm_scheme [文字列] DRM スキーム(保護されている場合)。有効な値は widevine、playready、clearkey です。DRM スキームの UUID も使用できます。
    • drm_license_uri [文字列] 保護されている場合は、ライセンス サーバーの URI。
    • drm_force_default_license_uri [ブール値] 独自のライセンス URI を含む鍵リクエストに drm_license_uri の使用を強制するかどうか。
    • drm_key_request_properties [文字列配列] 保護されている場合、name1、value1、name2、value2 などの形式でパックされたキー リクエスト ヘッダー。
    • drm_session_for_clear_content [ブール値] DRM セッションをクリアな動画トラックと音声トラックに接続するかどうか。
    • drm_multi_session [ブール値] 保護されている場合に鍵のローテーションを有効にします。
    • drm_force_software_crypto [ブール値] DRM スキームでサポートされている場合、デバイスのハードウェア暗号化機能に関係なく、DRM にソフトウェア暗号化ライブラリを強制的に使用させるかどうか。
    • subtitle_uri [文字列] 字幕サイドカー ファイルの URI。
    • subtitle_mime_type [文字列] subtitle_uri の MIME タイプ(subtitle_uri が設定されている場合は必須)。
    • subtitle_language [文字列] 字幕ファイルの BCP47 言語コード(subtitle_uri が設定されていない場合は無視されます)。
    • ad_tag_uri [文字列] IMA 拡張機能を使用して読み込む広告タグの URI。
    • prefer_extension_decoders [ブール値] プラットフォーム デコーダよりも拡張機能デコーダを優先するかどうか。

adb shell am start を使用してインテントを起動する場合、オプションの文字列エクストラを --es(例: --es extension mpd)で設定できます。オプションのブール値エクストラを --ez(例: --ez prefer_extension_decoders TRUE)で設定できます。オプションの long エクストラを --el(例: --el clip_start_position_ms 5000)で設定できます。オプションの文字列配列エクストラを --esa(例: --esa drm_key_request_properties name1,value1)で設定できます。

サンプルのプレイリストを再生するには、インテントのアクションを androidx.media3.demo.main.action.VIEW_LIST に設定します。サンプル構成のエクストラは、次の 2 つの違いを除いて androidx.media3.demo.main.action.VIEW と同じです。

  • エクストラのキーには、アンダースコアとサンプルの 0 ベースのインデックスを接尾辞として付ける必要があります。たとえば、extension_0 は最初のサンプルのサンプルタイプをヒントとして示します。drm_scheme_1 は、2 番目のサンプルの DRM スキームを設定します。
  • サンプルの URI は、キー uri_<sample-index> を持つエクストラとして渡されます。

サンプルに依存しないその他のエクストラは変更されません。たとえば、ターミナルで次のコマンドを実行すると、2 つのアイテムを含むプレイリストを再生し、2 番目のアイテムの拡張子をオーバーライドできます。

adb shell am start -a androidx.media3.demo.main.action.VIEW_LIST \
    --es uri_0 https://a.com/sample1.mp4 \
    --es uri_1 https://b.com/sample2.fake_mpd \
    --es extension_1 mpd