एचएलएस

ExoPlayer, कई कंटेनर फ़ॉर्मैट के साथ एचएलएस के साथ काम करता है. ऑडियो और वीडियो के सैंपल के फ़ॉर्मैट भी काम करने चाहिए. ज़्यादा जानकारी के लिए, सैंपल के फ़ॉर्मैट सेक्शन देखें. हम एचएलएस कॉन्टेंट बनाने वाले लोगों को, अच्छी क्वालिटी वाली एचएलएस स्ट्रीम जनरेट करने के लिए प्रोत्साहित करते हैं. इसके बारे में इस ब्लॉग पोस्ट में बताया गया है.

सुविधा इनकी अनुमति है टिप्पणियां
कंटेनर
MPEG-TS हां
FMP4/CMAF हां
ADTS (AAC) हां
MP3 हां
सबटाइटल / कैप्शन
CEA-608 हां
CEA-708 हां
WebVTT हां
मेटाडेटा
ID3 हां
SCTE-35 नहीं
कॉन्टेंट की सुरक्षा
AES-128 हां
AES-128 का सैंपल नहीं
वाइडवाइन हां एपीआई 19+ ("cenc" स्कीम) और 25+ ("cbcs" स्कीम)
PlayReady SL2000 हां सिर्फ़ Android TV के लिए
सर्वर कंट्रोल
डेल्टा अपडेट हां
प्लेलिस्ट को फिर से लोड करने की सुविधा को ब्लॉक करना हां
प्रीलोड करने के सुझावों को लोड होने से रोकना हां जिन बाइट रेंज की लंबाई तय नहीं की गई है उन्हें छोड़कर
विज्ञापन शामिल करना
सर्वर के ज़रिए विज्ञापन डालने की सुविधा (इंटरस्टीशियल विज्ञापन) कुछ हद तक सिर्फ़ X-ASSET-URI वाला वीओडी. लाइव स्ट्रीम और X-ASSET-LIST को बाद में जोड़ा जाएगा.
IMA सर्वर-साइड और क्लाइंट-साइड विज्ञापन हां विज्ञापन डालने के बारे में गाइड
लाइव स्ट्रीम को फिर से चलाना
लाइव स्ट्रीम को सामान्य तरीके से चलाना हां
लो-लेटेंसी एचएलएस (ऐपल) हां
लो-लेटेंसी एचएलएस (कम्यूनिटी) नहीं
कॉमन मीडिया क्लाइंट डेटा सीएमसीडी हां CMCD इंटिग्रेशन गाइड

MediaItem का इस्तेमाल करना

एचएलएस स्ट्रीम चलाने के लिए, आपको एचएलएस मॉड्यूल पर निर्भर रहना होगा.

Kotlin

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

ग्रूवी

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

इसके बाद, HLS प्लेलिस्ट यूआरआई के लिए MediaItem बनाया जा सकता है और उसे प्लेयर को पास किया जा सकता है.

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

अगर आपका यूआरआई .m3u8 से खत्म नहीं होता है, तो कॉन्टेंट का टाइप साफ़ तौर पर बताने के लिए, MediaItem.Builder के setMimeType को MimeTypes.APPLICATION_M3U8 पास किया जा सकता है.

मीडिया आइटम का यूआरआई, मीडिया प्लेलिस्ट या मल्टीवेरिएंट प्लेलिस्ट की ओर इशारा कर सकता है. अगर यूआरआई, कई वैरिएंट वाली ऐसी प्लेलिस्ट की ओर ले जाता है जिसमें कई #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 को कॉल करके, मौजूदा मेनिफ़ेस्ट को वापस पाया जा सकता है. एचएलएस के लिए, आपको दिखाए गए ऑब्जेक्ट को HlsManifest में काफ़ी करना चाहिए. मेनिफ़ेस्ट लोड होने पर, Player.Listener का onTimelineChanged कॉलबैक भी कॉल किया जाता है. मांग पर उपलब्ध कॉन्टेंट के लिए ऐसा एक बार होगा. वहीं, लाइव कॉन्टेंट के लिए ऐसा कई बार हो सकता है. नीचे दिए गए कोड स्निपेट में दिखाया गया है कि मेनिफ़ेस्ट लोड होने पर, कोई ऐप्लिकेशन क्या कर सकता है.

Kotlin

player.addListener(
  object : Player.Listener {
    override fun onTimelineChanged(timeline: Timeline, @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.
        }
      }
    });

इंटरस्टीशियल विज्ञापनों के साथ एचएलएस स्ट्रीम चलाना

एचएलएस स्पेसिफ़िकेशन में, एचएलएस इंटरस्टीशियल के बारे में बताया गया है. इनका इस्तेमाल, मीडिया प्लेलिस्ट में इंटरस्टीशियल की जानकारी शामिल करने के लिए किया जा सकता है. ExoPlayer डिफ़ॉल्ट रूप से इन इंटरस्टीशियल विज्ञापनों को अनदेखा करता है. HlsInterstitialsAdsLoader का इस्तेमाल करके सहायता जोड़ी जा सकती है. हम शुरू से ही, स्पेसिफ़िकेशन की सभी सुविधाओं के साथ काम नहीं करते हैं. अगर आपको अपनी स्ट्रीम के लिए सहायता नहीं मिल रही है, तो हमें बताएं. इसके लिए, GitHub पर गड़बड़ी की शिकायत करें. साथ ही, हमें अपनी स्ट्रीम का यूआरआई भेजें, ताकि हम आपकी स्ट्रीम के लिए सहायता जोड़ सकें.

प्लेलिस्ट एपीआई के साथ MediaItem का इस्तेमाल करना

इंटरस्टीशियल विज्ञापनों के साथ एचएलएस स्ट्रीम चलाने का सबसे आसान तरीका है कि HlsInterstitialsAdsLoader.AdsMediaSourceFactory की मदद से ExoPlayer इंस्टेंस बनाया जाए. इससे, HLS इंटरस्टीशियल चलाने के लिए, Player इंटरफ़ेस के MediaItem पर आधारित प्लेलिस्ट एपीआई का इस्तेमाल किया जा सकता है.

ExoPlayer के MediaSource.Factory को प्लेयर इंस्टेंस बनाते समय, बिल्डर में इंजेक्ट किया जा सकता है:

Kotlin

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

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

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

Java

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
player =
    new ExoPlayer.Builder(context)
        .setMediaSourceFactory(hlsMediaSourceFactory)
        .build();

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

इस तरह के प्लेयर सेटअप के साथ, HLS इंटरस्टीशियल चलाने के लिए, प्लेयर पर सिर्फ़ AdsConfiguration वाला मीडिया आइटम सेट करना होता है:

Kotlin

player.setMediaItem(
  MediaItem.Builder()
    .setUri("https://www.example.com/media.m3u8")
    .setAdsConfiguration(
      AdsConfiguration.Builder(Uri.parse("hls://interstitials"))
        .setAdsId("ad-tag-0") // must be unique within playlist
        .build())
    .build())

player.prepare();
player.play();

Java

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

मीडिया सोर्स के आधार पर एपीआई का इस्तेमाल करना

इसके अलावा, ExoPlayer इंस्टेंस को डिफ़ॉल्ट मीडिया सोर्स फ़ैक्ट्री को बदले बिना बनाया जा सकता है. इंटरस्टीशियल विज्ञापनों को दिखाने के लिए, ऐप्लिकेशन सीधे तौर पर HlsInterstitialsAdsLoader.AdsMediaSourceFactory का इस्तेमाल करके MediaSource बना सकता है. इसके बाद, मीडिया सोर्स पर आधारित प्लेलिस्ट एपीआई का इस्तेमाल करके, इसे ExoPlayer को उपलब्ध करा सकता है:

Kotlin

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

// Build player with default media source factory.
player = new 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(Uri.parse("hls://interstitials"))
          .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.
player = new ExoPlayer.Builder(context).build();

// Create an 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.
exoPlayer.setMediaSource(mediaSource);
exoPlayer.prepare();
exoPlayer.play();

विज्ञापन इवेंट सुनना

एचएलएस इंटरस्टीशियल के प्लेबैक से जुड़े स्टेटस में होने वाले बदलावों को मॉनिटर करने के लिए, HlsInterstitialsAdsLoader में Listener जोड़ा जा सकता है. इससे किसी ऐप्लिकेशन या एसडीके को, दिखाए गए विज्ञापनों, लोड की जा रही ऐसेट की सूचियों, तैयार किए जा रहे विज्ञापन मीडिया सोर्स को ट्रैक करने या ऐसेट की सूची लोड करने और विज्ञापन तैयार करने से जुड़ी गड़बड़ियों का पता लगाने की अनुमति मिलती है. इसके अलावा, विज्ञापन मीडिया सोर्स से मिले मेटाडेटा का इस्तेमाल, विज्ञापन चलाने की पुष्टि करने के लिए किया जा सकता है. इससे यह पता चलता है कि विज्ञापन सही तरीके से चल रहा है या नहीं. साथ ही, इससे विज्ञापन चलाने की प्रोग्रेस को ट्रैक किया जा सकता है.

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

private 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.
  }
}

सभी उपलब्ध कॉलबैक के बारे में ज़्यादा जानकारी के लिए, HlsInterstitialsAdsLoader.Listener का JavaDoc देखें.

इसके बाद, लिसनर को विज्ञापन लोडर में जोड़ा जा सकता है:

Kotlin

var 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 के किसी इंस्टेंस का फिर से इस्तेमाल किया जा सकता है. ऐसा कई प्लेयर इंस्टेंस के लिए किया जा सकता है. ये प्लेयर इंस्टेंस, कई मीडिया सोर्स बनाते हैं. इन मीडिया सोर्स के लिए विज्ञापन लोड करने होते हैं.

उदाहरण के लिए, किसी Activity के onCreate तरीके में एक इंस्टेंस बनाया जा सकता है. इसके बाद, इसका इस्तेमाल कई प्लेयर इंस्टेंस के लिए किया जा सकता है. यह तब तक काम करता है, जब तक इसका इस्तेमाल एक ही समय में एक प्लेयर इंस्टेंस कर रहा हो. यह उस सामान्य इस्तेमाल के उदाहरण के लिए फ़ायदेमंद है जब ऐप्लिकेशन को बैकग्राउंड में ले जाया जाता है, प्लेयर इंस्टेंस को बंद कर दिया जाता है, और फिर ऐप्लिकेशन को फ़ोरग्राउंड में लाने पर एक नया इंस्टेंस बनाया जाता है.

विज्ञापन दिखाने की स्थिति के साथ वीडियो को फिर से चलाना

उपयोगकर्ताओं को विज्ञापन फिर से न देखना पड़े, इसके लिए विज्ञापन चलाने की स्थिति को सेव किया जा सकता है. साथ ही, प्लेयर को फिर से बनाने पर इसे वापस लाया जा सकता है. ऐसा तब किया जाता है, जब खिलाड़ी को रिलीज़ किया जाता है. इसके लिए, getAdsResumptionStates() को कॉल किया जाता है. साथ ही, वापस लाए गए AdsResumptionState ऑब्जेक्ट को सेव किया जाता है. प्लेयर को फिर से बनाने पर, विज्ञापन लोडर इंस्टेंस पर addAdResumptionState() को कॉल करके स्थिति को वापस लाया जा सकता है. AdsResumptionState को बंडल किया जा सकता है. इसलिए, इसे Activity के onSaveInstanceState बंडल में सेव किया जा सकता है. ध्यान दें कि विज्ञापन फिर से शुरू करने की सुविधा सिर्फ़ वीओडी स्ट्रीम के लिए उपलब्ध है

Kotlin

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

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  // Create the ads loader instance.
  hlsInterstitialsAdsLoader = HlsInterstitialsAdsLoader(this)
  // Restore ad resumption states.
  savedInstanceState?.getParcelableArrayList<Bundle>(ADS_RESUMPTION_STATE_KEY)?.let { bundles ->
    adsResumptionStates =
      bundles.map { HlsInterstitialsAdsLoader.AdsResumptionState.fromBundle(it) }
  }
}

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(Uri.parse("hls://interstitials"))
          .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?.setPlayer(null)
}

Java

public static final String ADS_RESUMPTION_STATE_KEY = "ads_resumption_state";

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

private List<HlsInterstitialsAdsLoader.AdsResumptionState> adsResumptionStates;

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

removeAdResumptionState(Object adsId) की मदद से, किसी एक वीडियो को फिर से शुरू करने की स्थिति को हटाया जा सकता है. इसके अलावा, clearAllAdResumptionStates() की मदद से, सभी वीडियो को फिर से शुरू करने की स्थिति को हटाया जा सकता है.

आम तौर पर, विज्ञापन लोडर पर अगला प्लेयर इंस्टेंस सेट करने से पहले, पुराने प्लेयर इंस्टेंस को रिलीज़ करना न भूलें. विज्ञापन लोडर के रिलीज़ होने के बाद, इसका इस्तेमाल नहीं किया जा सकेगा.

वीडियो चलाने की सुविधा को पसंद के मुताबिक बनाना

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 कॉन्टेंट को बेहतर बनाया जा सकता है. पूरी जानकारी के लिए, ExoPlayer में HLS फ़ाइल चलाने के बारे में हमारी Medium पोस्ट पढ़ें. मुख्य बातें ये हैं:

  • सेगमेंट की अवधि की सटीक जानकारी का इस्तेमाल करें.
  • लगातार मीडिया स्ट्रीम का इस्तेमाल करें. साथ ही, अलग-अलग सेगमेंट में मीडिया स्ट्रक्चर में बदलाव न करें.
  • #EXT-X-INDEPENDENT-SEGMENTS टैग का इस्तेमाल करें.
  • वीडियो और ऑडियो, दोनों को शामिल करने वाली फ़ाइलों के बजाय, डीमक्स की गई स्ट्रीम को प्राथमिकता दें.
  • एक से ज़्यादा वैरिएंट वाली प्लेलिस्ट में ज़्यादा से ज़्यादा जानकारी शामिल करें.

ये दिशा-निर्देश, खास तौर पर लाइव स्ट्रीम पर लागू होते हैं:

  • #EXT-X-PROGRAM-DATE-TIME टैग का इस्तेमाल करें.
  • #EXT-X-DISCONTINUITY-SEQUENCE टैग का इस्तेमाल करें.
  • लाइव इवेंट के लिए लंबी समयसीमा तय करें. एक मिनट या इससे ज़्यादा समय अच्छा होता है.