HLS

ExoPlayer תומך ב-HLS עם כמה פורמטים של מאגרי תגים. גם הפורמטים של דגימות האודיו והווידאו שהקובץ מכיל חייבים להיות נתמכים (לפרטים, עיינו בקטע פורמטים של דגימות). אנחנו ממליצים מאוד ליוצרי תוכן ב-HLS ליצור שידורי HLS באיכות גבוהה, כמו שמתואר בפוסט הזה בבלוג.

תכונה נתמך תגובות
קונטיינרים
MPEG-TS כן
FMP4/CMAF כן
ADTS (AAC) כן
MP3 כן
כתוביות
CEA-608 כן
CEA-708 כן
WebVTT כן
מטא-נתונים
ID3 כן
SCTE-35 לא
הגנה על תוכן
AES-128 כן
‫AES-128 לדוגמה לא
Widevine כן ‫API 19+‎ (סכימת "cenc") ו-25+‎ (סכימת "cbcs")
PlayReady SL2000 כן Android TV בלבד
שליטה בשרת
עדכוני דלתא כן
חסימת הטעינה מחדש של הפלייליסט כן
חסימת הטעינה של רמזים לטעינה מראש כן חוץ מטווחים של בתים עם אורכים לא מוגדרים
הוספת מודעות
הכנסת מודעות בהנחיית השרת (מודעות מעברון) באופן חלקי רק VOD עם X-ASSET-URI. שידורים חיים וX-ASSET-LIST יתווספו בהמשך.
מודעות בצד השרת ובצד הלקוח ב-IMA כן מדריך להוספת מודעות
הפעלה בזמן אמת
הפעלה רגילה של שידור חי כן
‫HLS עם זמן אחזור נמוך (אפל) כן
‫HLS עם זמן אחזור קצר (קהילה) לא
Common Media Client Data CMCD כן מדריך לשילוב CMCD

שימוש ב-MediaItem

כדי להפעיל שידור בפרוטוקול HLS, צריך להסתמך על מודול HLS.

Kotlin

implementation("androidx.media3:media3-exoplayer-hls:1.11.1")

מגניב

implementation "androidx.media3:media3-exoplayer-hls:1.11.1"

לאחר מכן, אפשר ליצור MediaItem ל-URI של פלייליסט HLS ולהעביר אותו לנגן.

Kotlin

// Create a player instance.
val player = ExoPlayer.Builder(context).build()
// Set the media item to be played.
player.setMediaItem(MediaItem.fromUri(hlsUri))
// Prepare the player.
player.prepare()

Java

// Create a player instance.
ExoPlayer player = new ExoPlayer.Builder(context).build();
// Set the media item to be played.
player.setMediaItem(MediaItem.fromUri(hlsUri));
// Prepare the player.
player.prepare();

אם ה-URI לא מסתיים ב-.m3u8, אפשר להעביר את MimeTypes.APPLICATION_M3U8 אל setMimeType של MediaItem.Builder כדי לציין במפורש את סוג התוכן.

ה-URI של קובץ המדיה יכול להפנות לפלייליסט של מדיה או לפלייליסט עם כמה וריאציות. אם ה-URI מפנה לפלייליסט עם כמה וריאציות שמצהיר על כמה תגי #EXT-X-STREAM-INF, ExoPlayer יתאים באופן אוטומטי בין הווריאציות, תוך התחשבות ברוחב הפס הזמין וביכולות המכשיר.

שימוש ב-HlsMediaSource

כדי לקבל אפשרויות נוספות להתאמה אישית, אפשר ליצור HlsMediaSource ולהעביר אותו ישירות לנגן במקום MediaItem.

Kotlin

// Create a data source factory.
val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
// Create a HLS media source pointing to a playlist uri.
val hlsMediaSource =
  HlsMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(hlsUri))
// Create a player instance.
val player = ExoPlayer.Builder(context).build()
// Set the HLS media source as the playlist with a single media item.
player.setMediaSource(hlsMediaSource)
// Prepare the player.
player.prepare()

Java

// Create a data source factory.
DataSource.Factory dataSourceFactory = new DefaultHttpDataSource.Factory();
// Create a HLS media source pointing to a playlist uri.
HlsMediaSource hlsMediaSource =
    new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(hlsUri));
// Create a player instance.
ExoPlayer player = new ExoPlayer.Builder(context).build();
// Set the HLS media source as the playlist with a single media item.
player.setMediaSource(hlsMediaSource);
// Prepare the player.
player.prepare();

גישה למניפסט

כדי לאחזר את המניפסט הנוכחי, אפשר להפעיל את Player.getCurrentManifest. ב-HLS, צריך להמיר את האובייקט המוחזר ל-HlsManifest. בכל פעם שהמניפסט נטען, מתבצעת גם קריאה חוזרת (callback) של onTimelineChanged ב-Player.Listener. הפעולה הזו תתבצע פעם אחת בתוכן על פי דרישה, ויכול להיות שהיא תתבצע הרבה פעמים בתוכן בשידור חי. בקטע הקוד הבא אפשר לראות איך אפליקציה יכולה לבצע פעולה בכל פעם שהמניפסט נטען.

Kotlin

player.addListener(
  object : Player.Listener {
    override fun onTimelineChanged(
      timeline: Timeline,
      @Player.TimelineChangeReason reason: Int,
    ) {
      val manifest = player.currentManifest
      if (manifest is HlsManifest) {
        // Do something with the manifest.
      }
    }
  }
)

Java

player.addListener(
    new Player.Listener() {
      @Override
      public void onTimelineChanged(
          Timeline timeline, @Player.TimelineChangeReason int reason) {
        Object manifest = player.getCurrentManifest();
        if (manifest != null) {
          HlsManifest hlsManifest = (HlsManifest) manifest;
          // Do something with the manifest.
        }
      }
    });

הפעלת שידורי HLS עם מודעות מעברון

מפרט ה-HLS מגדיר מודעות מעברון ב-HLS, שבהן אפשר להשתמש כדי לכלול מידע על מודעות מעברון בפלייליסט של מדיה. כברירת מחדל, ExoPlayer מתעלם מהמודעות האלה. אפשר להוסיף תמיכה באמצעות HlsInterstitialsAdsLoader. אנחנו לא תומכים בכל התכונות של המפרט מההתחלה. אם אתם לא מוצאים תמיכה לסטרימינג שלכם, אתם יכולים לדווח על בעיה ב-GitHub ולשלוח לנו את ה-URI של הסטרימינג כדי שנוסיף תמיכה.

שימוש ב-MediaItem עם ה-API של הפלייליסט

הדרך הנוחה ביותר להפעיל שידורי HLS עם מודעות מעברון היא ליצור מכונה של ExoPlayer עם HlsInterstitialsAdsLoader.AdsMediaSourceFactory. כך אפשר להשתמש ב-API של הפלייליסט שמבוסס על MediaItem בממשק Player כדי להפעיל מודעות מעברון ב-HLS.

אפשר להוסיף את ה-MediaSource.Factory של ExoPlayer ל-builder כשיוצרים את המופע של הנגן:

Kotlin

val hlsInterstitialsAdsLoader = HlsInterstitialsAdsLoader(context)
// Create a MediaSource.Factory for HLS streams with interstitials.
val hlsMediaSourceFactory =
  HlsInterstitialsAdsLoader.AdsMediaSourceFactory(
    hlsInterstitialsAdsLoader,
    playerView,
    DefaultMediaSourceFactory(context),
  )

// Build player with interstitials media source factory
val player = ExoPlayer.Builder(context).setMediaSourceFactory(hlsMediaSourceFactory).build()

// Set the player on the ads loader.
hlsInterstitialsAdsLoader.setPlayer(player)
playerView.setPlayer(player)

Java

HlsInterstitialsAdsLoader hlsInterstitialsAdsLoader = new HlsInterstitialsAdsLoader(context);
// Create a MediaSource.Factory for HLS streams with interstitials.
MediaSource.Factory hlsMediaSourceFactory =
    new HlsInterstitialsAdsLoader.AdsMediaSourceFactory(
        hlsInterstitialsAdsLoader, playerView, new DefaultMediaSourceFactory(context));

// Build player with interstitials media source factory
ExoPlayer player =
    new ExoPlayer.Builder(context).setMediaSourceFactory(hlsMediaSourceFactory).build();

// Set the player on the ads loader.
hlsInterstitialsAdsLoader.setPlayer(player);
playerView.setPlayer(player);

כשהנגן מוגדר כך, כדי להפעיל מודעות מעברון ב-HLS צריך רק להגדיר קובץ מדיה עם AdsConfiguration בנגן:

Kotlin

// Build an HLS media item with ads configuration to be played.
player.setMediaItem(
  MediaItem.Builder()
    .setUri("https://www.example.com/media.m3u8")
    .setAdsConfiguration(
      MediaItem.AdsConfiguration.Builder("hls://interstitials".toUri())
        .setAdsId("ad-tag-0") // must be unique within playlist
        .build()
    )
    .build()
)

player.prepare()
player.play()

Java

// Build an HLS media item with ads configuration to be played.
player.setMediaItem(
    new MediaItem.Builder()
        .setUri("https://www.example.com/media.m3u8")
        .setAdsConfiguration(
            new AdsConfiguration.Builder(Uri.parse("hls://interstitials"))
                .setAdsId("ad-tag-0") // must be unique within playlist
                .build())
        .build());
player.prepare();
player.play();

שימוש ב-API שמבוסס על מקור המדיה

לחלופין, אפשר ליצור את המכונה של ExoPlayer בלי לשנות את ברירת המחדל של המפעל של מקור המדיה. כדי לתמוך במודעות מעברון, האפליקציה יכולה להשתמש ישירות ב-HlsInterstitialsAdsLoader.AdsMediaSourceFactory כדי ליצור MediaSource ולספק אותו ל-ExoPlayer באמצעות ה-API של הפלייליסט שמבוסס על מקור המדיה:

Kotlin

val hlsInterstitialsAdsLoader = HlsInterstitialsAdsLoader(context)
// Create a MediaSource.Factory for HLS streams with interstitials.
val hlsMediaSourceFactory =
  HlsInterstitialsAdsLoader.AdsMediaSourceFactory(
    hlsInterstitialsAdsLoader,
    playerView,
    context,
  )

// Build player with default media source factory.
val player = ExoPlayer.Builder(context).build()

// Create an media source from an HLS media item with ads configuration.
val mediaSource =
  hlsMediaSourceFactory.createMediaSource(
    MediaItem.Builder()
      .setUri("https://www.example.com/media.m3u8")
      .setAdsConfiguration(
        MediaItem.AdsConfiguration.Builder("hls://interstitials".toUri())
          .setAdsId("ad-tag-0")
          .build()
      )
      .build()
  )

// Set the media source on the player.
player.setMediaSource(mediaSource)
player.prepare()
player.play()

Java

HlsInterstitialsAdsLoader hlsInterstitialsAdsLoader = new HlsInterstitialsAdsLoader(context);
// Create a MediaSource.Factory for HLS streams with interstitials.
MediaSource.Factory hlsMediaSourceFactory =
    new HlsInterstitialsAdsLoader.AdsMediaSourceFactory(
        hlsInterstitialsAdsLoader, playerView, context);

// Build player with default media source factory.
ExoPlayer player = new ExoPlayer.Builder(context).build();

// Create a media source from an HLS media item with ads configuration.
MediaSource mediaSource =
    hlsMediaSourceFactory.createMediaSource(
        new MediaItem.Builder()
            .setUri("https://www.example.com/media.m3u8")
            .setAdsConfiguration(
                new MediaItem.AdsConfiguration.Builder(Uri.parse("hls://interstitials"))
                    .setAdsId("ad-tag-0")
                    .build())
            .build());

// Set the media source on the player.
player.setMediaSource(mediaSource);
player.prepare();
player.play();

האזנה לאירועי מודעות

אפשר להוסיף Listener אל HlsInterstitialsAdsLoader כדי לעקוב אחרי אירועים שקשורים לשינויים בסטטוס של הפעלת מודעות מעברון ב-HLS. הוא מאפשר לאפליקציה או ל-SDK לעקוב אחרי המודעות שמופעלות, רשימות הנכסים שנטענות ומקורות המדיה של המודעות שמוכנים, או לזהות שגיאות בטעינה של רשימת הנכסים ובהכנה של המודעות. בנוסף, אפשר לקבל מטא-נתונים שנשלחים ממקורות מדיה של מודעות כדי לאמת את ההפעלה של המודעות ברמה מפורטת, או כדי לעקוב אחרי התקדמות ההפעלה של המודעות.

Kotlin

class AdsLoaderListener : HlsInterstitialsAdsLoader.Listener {

  override fun onStart(mediaItem: MediaItem, adsId: Any, adViewProvider: AdViewProvider) {
    // Do something when HLS media item with interstitials is started.
  }

  override fun onMetadata(
    mediaItem: MediaItem,
    adsId: Any,
    adGroupIndex: Int,
    adIndexInAdGroup: Int,
    metadata: Metadata,
  ) {
    // Do something with metadata that is emitted by the ad media source of the given ad.
  }

  override fun onAdCompleted(
    mediaItem: MediaItem,
    adsId: Any,
    adGroupIndex: Int,
    adIndexInAdGroup: Int,
  ) {
    // Do something when ad completed playback.
  }

  // ... See JavaDoc for further callbacks of HlsInterstitialsAdsLoader.Listener.

  override fun onStop(mediaItem: MediaItem, adsId: Any, adPlaybackState: AdPlaybackState) {
    // Do something with the resulting ad playback state when stopped.
  }
}

Java

@OptIn(markerClass = UnstableApi.class)
private static class AdsLoaderListener implements HlsInterstitialsAdsLoader.Listener {

  // implement HlsInterstitialsAdsLoader.Listener
  @Override
  public void onStart(MediaItem mediaItem, Object adsId, AdViewProvider adViewProvider) {
    // Do something when HLS media item with interstitials is started.
  }

  @Override
  public void onMetadata(
      MediaItem mediaItem,
      Object adsId,
      int adGroupIndex,
      int adIndexInAdGroup,
      Metadata metadata) {
    // Do something with metadata that is emitted by the ad media source of the given ad.
  }

  @Override
  public void onAdCompleted(
      MediaItem mediaItem, Object adsId, int adGroupIndex, int adIndexInAdGroup) {
    // Do something when ad completed playback.
  }

  // ... See JavaDoc for further callbacks

  @Override
  public void onStop(MediaItem mediaItem, Object adsId, AdPlaybackState adPlaybackState) {
    // Do something with the resulting ad playback state when stopped.
  }
}

ב-JavaDoc של HlsInterstitialsAdsLoader.Listener מופיע תיעוד מפורט של כל הקריאות החוזרות הזמינות.

לאחר מכן אפשר להוסיף את המאזין לטוען המודעות:

Kotlin

val listener = AdsLoaderListener()
// Add the listener to the ads loader to receive ad loader events.
hlsInterstitialsAdsLoader.addListener(listener)

Java

AdsLoaderListener listener = new AdsLoaderListener();
// Add the listener to the ads loader to receive ad loader events.
hlsInterstitialsAdsLoader.addListener(listener);

מחזור החיים של HlsInterstitialsAdsLoader

אפשר להשתמש שוב במופע של HlsInterstitialsAdsLoader או HlsInterstitialsAdsLoader.AdsMediaSourceFactory בכמה מופעים של נגנים שיוצרים כמה מקורות מדיה שצריך לטעון להם מודעות.

אפשר ליצור מופע, למשל, ב-method‏ onCreate של Activity, ואז להשתמש בו שוב בכמה מופעים של נגן. זה אפשרי כל עוד רק מופע אחד של הנגן משתמש בו באותו זמן. זה שימושי במקרה הנפוץ שבו האפליקציה עוברת לרקע, מופע הנגן נהרס ואז נוצר מופע חדש כשהאפליקציה חוזרת לחזית.

המשך במצב הפעלה של מודעה

כדי שהמשתמשים לא יצטרכו לצפות שוב במודעות, אפשר לשמור את מצב ההפעלה של המודעה ולשחזר אותו כשיוצרים מחדש את הנגן. כדי לעשות את זה, צריך להפעיל את getAdsResumptionStates כשהנגן עומד להשתחרר, ולאחסן את האובייקטים של AdsResumptionState שמוחזרים. כשיוצרים מחדש את הנגן, אפשר לשחזר את המצב באמצעות קריאה ל-addAdResumptionState במופע של טוען המודעות. אפשר לארוז את AdsResumptionState, כך שניתן לאחסן אותו בחבילה של onSaveInstanceState ב-Activity. חשוב לשים לב שהמשך הצגת המודעות נתמך רק בשידורי VOD.

Kotlin

class HlsInterstitialsActivity : Activity() {

  companion object {
    const val ADS_RESUMPTION_STATE_KEY = "ads_resumption_state"
  }

  private var hlsInterstitialsAdsLoader: HlsInterstitialsAdsLoader? = null
  private var playerView: PlayerView? = null
  private var player: ExoPlayer? = null
  private var adsResumptionStates: List<HlsInterstitialsAdsLoader.AdsResumptionState>? = null

  @Suppress("DEPRECATION") // getParcelableArrayList without class type is deprecated in API 33+
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Create the ads loader instance.
    hlsInterstitialsAdsLoader = HlsInterstitialsAdsLoader(this)
    // Restore ad resumption states.
    val bundles =
      if (Build.VERSION.SDK_INT >= 33) {
        savedInstanceState?.getParcelableArrayList(ADS_RESUMPTION_STATE_KEY, Bundle::class.java)
      } else {
        savedInstanceState?.getParcelableArrayList(ADS_RESUMPTION_STATE_KEY)
      }
    adsResumptionStates = bundles?.map(HlsInterstitialsAdsLoader.AdsResumptionState::fromBundle)
  }

  override fun onStart() {
    super.onStart()
    // Build a player and set it on the ads loader.
    initializePlayer()
    hlsInterstitialsAdsLoader?.setPlayer(player)
    // Add any stored ad resumption states to the ads loader.
    adsResumptionStates?.forEach { hlsInterstitialsAdsLoader?.addAdResumptionState(it) }
    adsResumptionStates = null // Consume the states
  }

  override fun onStop() {
    super.onStop()
    // Get ad resumption states.
    adsResumptionStates = hlsInterstitialsAdsLoader?.adsResumptionStates
    releasePlayer()
  }

  override fun onDestroy() {
    // Release the ads loader when not used anymore.
    hlsInterstitialsAdsLoader?.release()
    hlsInterstitialsAdsLoader = null
    super.onDestroy()
  }

  override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    // Store the ad resumption states.
    adsResumptionStates?.let {
      outState.putParcelableArrayList(
        ADS_RESUMPTION_STATE_KEY,
        ArrayList(it.map(HlsInterstitialsAdsLoader.AdsResumptionState::toBundle)),
      )
    }
  }

  fun initializePlayer() {
    if (player == null) {
      // Create a media source factory for HLS streams.
      val hlsMediaSourceFactory =
        HlsInterstitialsAdsLoader.AdsMediaSourceFactory(
          checkNotNull(hlsInterstitialsAdsLoader),
          playerView!!,
          /* context= */ this,
        )
      // Build player with interstitials media source
      player =
        ExoPlayer.Builder(/* context= */ this)
          .setMediaSourceFactory(hlsMediaSourceFactory)
          .build()
      // Set the player on the ads loader.
      hlsInterstitialsAdsLoader?.setPlayer(player)
      playerView?.player = player
    }

    // Use a media item with an HLS stream URI, an ad tag URI and ads ID.
    player?.setMediaItem(
      MediaItem.Builder()
        .setUri("https://www.example.com/media.m3u8")
        .setMimeType(MimeTypes.APPLICATION_M3U8)
        .setAdsConfiguration(
          MediaItem.AdsConfiguration.Builder("hls://interstitials".toUri())
            .setAdsId("ad-tag-0") // must be unique within ExoPlayer playlist
            .build()
        )
        .build()
    )
    player?.prepare()
    player?.play()
  }

  fun releasePlayer() {
    player?.release()
    player = null
    hlsInterstitialsAdsLoader?.setPlayer(null)
    playerView?.player = null
  }
}

Java

@OptIn(markerClass = UnstableApi.class)
public static class HlsInterstitialsActivity extends Activity {

  public static final String ADS_RESUMPTION_STATE_KEY = "ads_resumption_state";

  private @Nullable HlsInterstitialsAdsLoader hlsInterstitialsAdsLoader;
  private @Nullable PlayerView playerView;
  private @Nullable ExoPlayer player;

  private List<HlsInterstitialsAdsLoader.AdsResumptionState> adsResumptionStates;

  @SuppressWarnings(
      "deprecation") // getParcelableArrayList without class type is deprecated in API 33+
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Create the ads loader instance.
    hlsInterstitialsAdsLoader = new HlsInterstitialsAdsLoader(this);
    // Restore ad resumption states.
    if (savedInstanceState != null) {
      ArrayList<Bundle> bundles;
      if (Build.VERSION.SDK_INT >= 33) {
        bundles =
            savedInstanceState.getParcelableArrayList(ADS_RESUMPTION_STATE_KEY, Bundle.class);
      } else {
        bundles = savedInstanceState.getParcelableArrayList(ADS_RESUMPTION_STATE_KEY);
      }
      if (bundles != null) {
        adsResumptionStates = new ArrayList<>();
        for (Bundle bundle : bundles) {
          adsResumptionStates.add(
              HlsInterstitialsAdsLoader.AdsResumptionState.fromBundle(bundle));
        }
      }
    }
  }

  @Override
  protected void onStart() {
    super.onStart();
    // Build a player and set it on the ads loader.
    initializePlayer();
    // Add any stored ad resumption states to the ads loader.
    if (adsResumptionStates != null) {
      for (HlsInterstitialsAdsLoader.AdsResumptionState state : adsResumptionStates) {
        hlsInterstitialsAdsLoader.addAdResumptionState(state);
      }
      adsResumptionStates = null; // Consume the states
    }
  }

  @Override
  protected void onStop() {
    super.onStop();
    // Get ad resumption states before releasing the player.
    if (hlsInterstitialsAdsLoader != null) {
      adsResumptionStates = hlsInterstitialsAdsLoader.getAdsResumptionStates();
    }
    releasePlayer();
  }

  @Override
  protected void onDestroy() {
    // Release the ads loader when not used anymore.
    if (hlsInterstitialsAdsLoader != null) {
      hlsInterstitialsAdsLoader.release();
      hlsInterstitialsAdsLoader = null;
    }
    super.onDestroy();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // Store the ad resumption states.
    if (adsResumptionStates != null) {
      ArrayList<Bundle> bundles = new ArrayList<>();
      for (HlsInterstitialsAdsLoader.AdsResumptionState state : adsResumptionStates) {
        bundles.add(state.toBundle());
      }
      outState.putParcelableArrayList(ADS_RESUMPTION_STATE_KEY, bundles);
    }
  }

  private void initializePlayer() {
    if (player == null) {
      // Create a media source factory for HLS streams.
      MediaSource.Factory hlsMediaSourceFactory =
          new HlsInterstitialsAdsLoader.AdsMediaSourceFactory(
              checkNotNull(hlsInterstitialsAdsLoader), playerView, /* context= */ this);
      // Build player with interstitials media source
      player =
          new ExoPlayer.Builder(/* context= */ this)
              .setMediaSourceFactory(hlsMediaSourceFactory)
              .build();
      // Set the player on the ads loader.
      hlsInterstitialsAdsLoader.setPlayer(player);
      playerView.setPlayer(player);
    }

    // Use a media item with an HLS stream URI, an ad tag URI and ads ID.
    player.setMediaItem(
        new MediaItem.Builder()
            .setUri("https://www.example.com/media.m3u8")
            .setMimeType(MimeTypes.APPLICATION_M3U8)
            .setAdsConfiguration(
                new MediaItem.AdsConfiguration.Builder(Uri.parse("hls://interstitials"))
                    .setAdsId("ad-tag-0") // must be unique within ExoPlayer playlist
                    .build())
            .build());
    player.prepare();
    player.play();
  }

  private void releasePlayer() {
    if (player != null) {
      player.release();
      player = null;
    }
    if (hlsInterstitialsAdsLoader != null) {
      hlsInterstitialsAdsLoader.setPlayer(null);
    }
    if (playerView != null) {
      playerView.setPlayer(null);
    }
  }
}

התאמה אישית של ההפעלה

ExoPlayer מספק כמה דרכים להתאים את חוויית ההפעלה לצרכים של האפליקציה. דוגמאות מופיעות בדף ההתאמה האישית.

השבתת ההכנה ללא מקטעים

כברירת מחדל, ExoPlayer ישתמש בהכנה ללא מקטעים. כלומר, ExoPlayer ישתמש רק במידע שבפלייליסט עם כמה וריאציות כדי להכין את הסטרימינג, וזה יעבוד אם התגים #EXT-X-STREAM-INF יכילו את המאפיין CODECS.

יכול להיות שתצטרכו להשבית את התכונה הזו אם קטעי המדיה שלכם מכילים רצועות של כתוביות סמויות מרובות שלא מוצהרות בפלייליסט עם כמה וריאציות באמצעות תג #EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS. אחרת, המערכת לא תזהה את רצועות הכתוביות האלה ולא תפעיל אותן. אפשר להשבית את ההכנה ללא חלוקה לחלקים ב-HlsMediaSource.Factory, כמו שמוצג בקטע הקוד הבא. חשוב לזכור שהפעולה הזו תאריך את זמן ההפעלה, כי ExoPlayer צריך להוריד קטע מדיה כדי לגלות את הטראקים הנוספים האלה. לכן, עדיף להצהיר על הטראקים של הכתוביות הסמויות בפלייליסט עם כמה וריאנטים.

Kotlin

val hlsMediaSource =
  HlsMediaSource.Factory(dataSourceFactory)
    .setAllowChunklessPreparation(false)
    .createMediaSource(MediaItem.fromUri(hlsUri))

Java

HlsMediaSource hlsMediaSource =
    new HlsMediaSource.Factory(dataSourceFactory)
        .setAllowChunklessPreparation(false)
        .createMediaSource(MediaItem.fromUri(hlsUri));

יצירת תוכן HLS באיכות גבוהה

כדי להפיק את המקסימום מ-ExoPlayer, יש כמה הנחיות שכדאי לפעול לפיהן כדי לשפר את תוכן ה-HLS. הסבר מלא זמין בפוסט שלנו ב-Medium בנושא הפעלת HLS ב-ExoPlayer. הנקודות העיקריות הן:

  • השתמשו במשכי זמן מדויקים של מקטעים.
  • השתמשו בסטרימינג רציף של מדיה. הימנעו משינויים במבנה המדיה בין הקטעים.
  • משתמשים בתג #EXT-X-INDEPENDENT-SEGMENTS.
  • עדיף להשתמש בסטרימינג מופרד (demuxed) ולא בקבצים שכוללים גם וידאו וגם אודיו.
  • הוסיפו כמה שיותר מידע לפלייליסט עם כמה וריאציות.

ההנחיות הבאות רלוונטיות במיוחד לשידורים חיים:

  • משתמשים בתג #EXT-X-PROGRAM-DATE-TIME.
  • משתמשים בתג #EXT-X-DISCONTINUITY-SEQUENCE.
  • חלון הפעילות צריך להיות ארוך. דקה או יותר זה מצוין.