ExoPlayer, कई कंटेनर फ़ॉर्मैट के साथ HLS को सपोर्ट करता है. इसमें शामिल ऑडियो और वीडियो के सैंपल फ़ॉर्मैट भी सपोर्ट किए जाने चाहिए. ज़्यादा जानकारी के लिए, सैंपल फ़ॉर्मैट वाला सेक्शन देखें. हम HLS कॉन्टेंट बनाने वाले लोगों को सलाह देते हैं कि वे अच्छी क्वालिटी वाली HLS स्ट्रीम जनरेट करें. इसके बारे में इस ब्लॉग पोस्ट में बताया गया है.
| सुविधा | समर्थित | टिप्पणियां |
|---|---|---|
| कंटेनर | ||
| 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 सर्वर-साइड और क्लाइंट-साइड विज्ञापन | हां | विज्ञापन शामिल करने के लिए गाइड |
| लाइव प्लेबैक | ||
| सामान्य लाइव प्लेबैक | हां | |
| लो-लेटेंसी HLS (Apple) | हां | |
| लो-लेटेंसी HLS (कम्यूनिटी) | नहीं | |
| कॉमन मीडिया क्लाइंट डेटा सीएमसीडी | हां | सीएमसीडी इंटिग्रेशन के लिए गाइड |
MediaItem का इस्तेमाल करना
HLS स्ट्रीम चलाने के लिए, आपको HLS मॉड्यूल पर निर्भर रहना होगा.
Kotlin
implementation("androidx.media3:media3-exoplayer-hls:1.10.0")
Groovy
implementation "androidx.media3:media3-exoplayer-hls:1.10.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 का इस्तेमाल करना
ज़्यादा विकल्पों के लिए, MediaItem के बजाय HlsMediaSource बनाया जा सकता है और उसे सीधे प्लेयर को पास किया जा सकता है.
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 में काटना चाहिए. मेनिफ़ेस्ट लोड होने पर भी, Player.Listener का onTimelineChanged कॉलबैक कॉल किया जाता है. ऑन-डिमांड कॉन्टेंट के लिए यह एक बार और लाइव कॉन्टेंट के लिए कई बार हो सकता है. नीचे दिया गया कोड स्निपेट दिखाता है कि मेनिफ़ेस्ट लोड होने पर, कोई ऐप्लिकेशन क्या कर सकता है.
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 पर कोई समस्या दर्ज करके हमें बताएं. साथ ही, हमें अपनी स्ट्रीम का यूआरआई भेजें, ताकि हम आपकी स्ट्रीम के लिए इंटरस्टीशियल को शामिल कर सकें.
प्लेलिस्ट एपीआई के साथ MediaItem का इस्तेमाल करना
इंटरस्टीशियल विज्ञापनों के साथ HLS स्ट्रीम चलाने का सबसे आसान तरीका है कि HlsInterstitialsAdsLoader.AdsMediaSourceFactory के साथ ExoPlayer इंस्टेंस बनाया जाए.
इससे, HLS इंटरस्टीशियल चलाने के लिए, Player
इंटरफ़ेस के MediaItem पर आधारित प्लेलिस्ट एपीआई का इस्तेमाल किया जा सकता है.
प्लेयर इंस्टेंस बनाते समय, ExoPlayer के MediaSource.Factory को बिल्डर में इंजेक्ट किया जा सकता है:
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();
मीडिया सोर्स पर आधारित एपीआई का इस्तेमाल करना
इसके अलावा, डिफ़ॉल्ट मीडिया सोर्स फ़ैक्ट्री को बदले बिना, ExoPlayer इंस्टेंस बनाया जा सकता है. इंटरस्टीशियल को शामिल करने के लिए, कोई ऐप्लिकेशन सीधे
HlsInterstitialsAdsLoader.AdsMediaSourceFactory का इस्तेमाल करके, MediaSource बना सकता है. इसके बाद, मीडिया सोर्स पर आधारित प्लेलिस्ट
एपीआई का इस्तेमाल करके, इसे ExoPlayer को दिया जा सकता है:
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();
विज्ञापन के इवेंट सुनना
HlsInterstitialsAdsLoader में Listener जोड़ा जा सकता है. इससे, HLS इंटरस्टीशियल के प्लेबैक से जुड़े स्टेटस में होने वाले बदलावों के बारे में जानकारी मॉनिटर की जा सकती है. इससे, कोई ऐप्लिकेशन या एसडीके, चलाए गए विज्ञापनों, लोड की जा रही ऐसेट की सूचियों, तैयार किए जा रहे विज्ञापन मीडिया सोर्स को ट्रैक कर सकता है. साथ ही, ऐसेट की सूची लोड होने और विज्ञापन तैयार करने में होने वाली गड़बड़ियों का पता लगा सकता है. इसके अलावा, विज्ञापन मीडिया सोर्स से जनरेट किए गए मेटाडेटा को, विज्ञापन के प्लेबैक की पुष्टि करने या विज्ञापन के प्लेबैक की प्रोग्रेस को ट्रैक करने के लिए इस्तेमाल किया जा सकता है.
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. } }
सभी उपलब्ध कॉलबैक की ज़्यादा जानकारी के लिए, का HlsInterstitialsAdsLoader.Listener JavaDoc देखें.
इसके बाद, लिसनर को विज्ञापन लोडर में जोड़ा जा सकता है:
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 के किसी इंस्टेंस को, कई
प्लेयर इंस्टेंस के लिए फिर से इस्तेमाल किया जा सकता है. ये प्लेयर इंस्टेंस, कई मीडिया सोर्स बनाते हैं जिनके लिए विज्ञापन लोड करने होते हैं.
उदाहरण के लिए, किसी Activity के onCreate तरीके में कोई इंस्टेंस बनाया जा सकता है. इसके बाद, इसे कई प्लेयर इंस्टेंस के लिए फिर से इस्तेमाल किया जा सकता है. यह तब तक काम करता है, जब तक इसका इस्तेमाल एक ही समय पर किसी एक प्लेयर इंस्टेंस से किया जा रहा हो. यह उस सामान्य इस्तेमाल के लिए काम का है, जब ऐप्लिकेशन को बैकग्राउंड में ले जाया जाता है, प्लेयर इंस्टेंस को बंद कर दिया जाता है, और फिर ऐप्लिकेशन को फ़ोरग्राउंड में लाने पर एक नया इंस्टेंस बनाया जाता है.
वीडियो फिर से शुरू करने पर विज्ञापन दिखेगा
उपयोगकर्ताओं को विज्ञापन फिर से न देखने पड़ें, इसके लिए विज्ञापन के प्लेबैक की स्थिति को सेव किया जा सकता है. साथ ही, प्लेयर को फिर से बनाने पर, इसे वापस लाया जा सकता है. इसके लिए, प्लेयर को बंद करने से पहले
getAdsResumptionStates() को कॉल किया जाता है और दिखाए गए
ऑब्जेक्ट को सेव किया जाता है.AdsResumptionState प्लेयर को फिर से बनाने पर, विज्ञापन लोडर इंस्टेंस पर addAdResumptionState() को कॉल करके, स्थिति को वापस लाया जा सकता है. AdsResumptionState को बंडल किया जा सकता है. इसलिए, इसे Activity के onSaveInstanceState बंडल में सेव किया जा सकता है. ध्यान दें कि विज्ञापन को फिर से शुरू करने की सुविधा, सिर्फ़ वीओडी स्ट्रीम के लिए उपलब्ध है
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"; @Nullable private HlsInterstitialsAdsLoader hlsInterstitialsAdsLoader; @Nullable private PlayerView playerView; @Nullable private 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 कॉन्टेंट को बेहतर बनाने के लिए कुछ दिशा-निर्देशों का पालन किया जा सकता है. ज़्यादा जानकारी के लिए, ExoPlayer में HLS प्लेबैक के बारे में हमारी Medium पोस्ट पढ़ें. यहां मुख्य बातें दी गई हैं:
- सेगमेंट की सटीक अवधि का इस्तेमाल करें.
- लगातार मीडिया स्ट्रीम का इस्तेमाल करें. सेगमेंट में मीडिया के स्ट्रक्चर में बदलाव करने से बचें.
#EXT-X-INDEPENDENT-SEGMENTSटैग का इस्तेमाल करें.- ऐसे फ़ाइलें इस्तेमाल करने के बजाय, डीमक्स की गई स्ट्रीम इस्तेमाल करें जिनमें वीडियो और ऑडियो, दोनों शामिल हों.
- मल्टीवैरिएंट प्लेलिस्ट में, ज़्यादा से ज़्यादा जानकारी शामिल करें.
लाइव स्ट्रीम के लिए, ये दिशा-निर्देश लागू होते हैं:
#EXT-X-PROGRAM-DATE-TIMEटैग का इस्तेमाल करें.#EXT-X-DISCONTINUITY-SEQUENCEटैग का इस्तेमाल करें.- लाइव विंडो की अवधि लंबी रखें. एक मिनट या उससे ज़्यादा की अवधि बेहतर है.