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 ที่มีเวลาในการตอบสนองต่ำ (Apple) | ใช่ | |
HLS ที่มีเวลาในการตอบสนองต่ำ (ชุมชน) | ไม่ | |
ข้อมูลไคลเอ็นต์สื่อทั่วไป CMCD | ใช่ | คู่มือการผสานรวม CMCD |
การใช้ MediaItem
หากต้องการเล่นสตรีม HLS คุณต้องใช้โมดูล HLS
Kotlin
implementation("androidx.media3:media3-exoplayer-hls:1.7.1")
Groovy
implementation "androidx.media3:media3-exoplayer-hls:1.7.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();
การเข้าถึงไฟล์ Manifest
คุณสามารถเรียกข้อมูลไฟล์ Manifest ปัจจุบันได้โดยเรียก Player.getCurrentManifest
สำหรับ HLS คุณควรแคสต์ออบเจ็กต์ที่ส่งคืนเป็น HlsManifest
ระบบจะเรียกใช้
onTimelineChanged
Callback ของ Player.Listener
ทุกครั้งที่โหลด
ไฟล์ Manifest การดำเนินการนี้จะเกิดขึ้น 1 ครั้งสำหรับเนื้อหาออนดีมานด์ และอาจเกิดขึ้นหลายครั้งสำหรับเนื้อหาแบบสด ข้อมูลโค้ดต่อไปนี้แสดงวิธีที่แอป
สามารถทำบางอย่างได้ทุกครั้งที่โหลดไฟล์ Manifest
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.
}
}
});
เล่นสตรีม HLS ด้วยโฆษณาคั่นระหว่างหน้า
ในการเปิดตัว Media3 ในอนาคตข้อกำหนด HLS กำหนดโฆษณาคั่นระหว่างหน้า HLS ซึ่งใช้เพื่อรวมข้อมูลโฆษณาคั่นระหว่างหน้าในเพลย์ลิสต์สื่อได้ โดยค่าเริ่มต้น ExoPlayer จะไม่สนใจโฆษณาคั่นกลางเหล่านี้ คุณเพิ่มการสนับสนุนได้โดยใช้ HlsInterstitialsAdsLoader
เรา
ไม่ได้รองรับฟีเจอร์ทั้งหมดของข้อกำหนดตั้งแต่เริ่มต้น หากคุณไม่ได้รับการสนับสนุนสำหรับสตรีม โปรดแจ้งให้เราทราบโดยแจ้งปัญหาใน GitHub และส่ง URI ของสตรีมมาให้เรา เพื่อให้เราเพิ่มการรองรับสตรีมของคุณได้
ใช้ MediaItem
กับ Playlist API
วิธีที่สะดวกที่สุดในการเล่นสตรีม HLS ที่มีโฆษณาคั่นกลางคือการสร้างอินสแตนซ์ ExoPlayer ด้วย HlsInterstitialsAdsLoader.AdsMediaSourceFactory
ซึ่งจะช่วยให้ใช้ MediaItem
API เพลย์ลิสต์ที่อิงตาม Player
อินเทอร์เฟซเพื่อเล่นโฆษณาคั่นระหว่างหน้า HLS ได้
คุณสามารถแทรก MediaSource.Factory
ของ ExoPlayer
ลงในเครื่องมือสร้างได้เมื่อ
สร้างอินสแตนซ์ของเพลเยอร์
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();
ใช้ API ที่อิงตามแหล่งที่มาของสื่อ
หรือจะสร้างอินสแตนซ์ ExoPlayer โดยไม่ต้องแทนที่
โรงงานแหล่งข้อมูลสื่อเริ่มต้นก็ได้ หากต้องการรองรับโฆษณาคั่นระหว่างหน้า แอปจะ
ใช้ HlsInterstitialsAdsLoader.AdsMediaSourceFactory
โดยตรงเพื่อสร้าง
MediaSource
และระบุให้กับ ExoPlayer โดยใช้เพลย์ลิสต์ตามแหล่งที่มาของสื่อ
API ได้
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();
ฟังเหตุการณ์โฆษณา
คุณสามารถเพิ่ม 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
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.
}
}
ดูJavaDoc ของ HlsInterstitialsAdsLoader.Listener
เพื่อดูเอกสารประกอบโดยละเอียด
ของการเรียกกลับทั้งหมดที่ใช้ได้
จากนั้นจึงเพิ่ม Listener ลงใน AdLoader ได้
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
ไปใช้ซ้ำกับอินสแตนซ์ของเพลเยอร์หลายรายการที่สร้างแหล่งที่มาของสื่อหลายรายการซึ่งต้องโหลดโฆษณาได้
เช่น คุณสร้างอินสแตนซ์ในเมธอด onCreate
ของ Activity
แล้วนำไปใช้ซ้ำสำหรับอินสแตนซ์ของเพลเยอร์หลายรายการได้ ซึ่งจะใช้งานได้ตราบใดที่อินสแตนซ์ของเพลเยอร์เดียวใช้งานอยู่พร้อมกัน ซึ่งมีประโยชน์สำหรับ
Use Case ทั่วไปเมื่อแอปเข้าสู่เบื้องหลัง ระบบจะทำลายอินสแตนซ์ของโปรแกรมเล่น
และสร้างอินสแตนซ์ใหม่เมื่อแอปกลับมาทำงานในเบื้องหน้า
อีกครั้ง
Kotlin
// Create the ads loader instance (for example onCreate).
hlsInterstitialsAdsLoader = HlsInterstitialsAdsLoader(context);
// Build a player and set it on the ads loader (for example onStart).
player = ExoPlayer.Builder(context).build();
hlsInterstitialsAdsLoader.setPlayer(player);
// Release the player and unset it on the ads loader (for example onStop).
player.release();
hlsInterstitialsAdsLoader.setPlayer(null);
// Build another player and set it on the ads loader (for example onStart).
player = ExoPlayer.Builder(context).build();
hlsInterstitialsAdsLoader.setPlayer(player);
// Release the player and unset it on the ads loader (for example onStop).
player.release();
hlsInterstitialsAdsLoader.setPlayer(null);
// Release the ads loader when not used anymore (for example onDestroy).
hlsInterstitialsAdsLoader.release();
Java
// Create the ads loader instance (for example onCreate).
hlsInterstitialsAdsLoader = new HlsInterstitialsAdsLoader(context);
// Build a player and set it on the ads loader (for example onStart).
player = new ExoPlayer.Builder(context).build();
hlsInterstitialsAdsLoader.setPlayer(player);
// Release the player and unset it on the ads loader (for example onStop).
player.release();
hlsInterstitialsAdsLoader.setPlayer(null);
// Build another player and set it on the ads loader (for example onStart).
player = new ExoPlayer.Builder(context).build();
hlsInterstitialsAdsLoader.setPlayer(player);
// Release the player and unset it on the ads loader (for example onStop).
player.release();
hlsInterstitialsAdsLoader.setPlayer(null);
// Release the ads loader when not used anymore (for example onDestroy).
hlsInterstitialsAdsLoader.release();
โดยทั่วไปแล้ว โปรดตรวจสอบว่าได้ปล่อยอินสแตนซ์เพลเยอร์เก่าก่อนที่จะตั้งค่าอินสแตนซ์เพลเยอร์ถัดไปใน AdLoader เมื่อเปิดตัวตัวโหลดโฆษณาแล้ว คุณจะใช้ตัวโหลดโฆษณาไม่ได้อีก
การปรับแต่งการเล่น
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
- เลือกสตรีมที่แยกมัลติเพล็กซ์แล้ว แทนที่จะเลือกไฟล์ที่มีทั้งวิดีโอและเสียง
- ระบุข้อมูลทั้งหมดที่คุณทำได้ในเพลย์ลิสต์เวอร์ชันหลายตัวแปร
หลักเกณฑ์ต่อไปนี้ใช้กับสตรีมแบบสดโดยเฉพาะ
- ใช้แท็ก
#EXT-X-PROGRAM-DATE-TIME
- ใช้แท็ก
#EXT-X-DISCONTINUITY-SEQUENCE
- ระบุระยะเวลาที่เปิดให้ใช้ข้อเสนอเป็นเวลานาน 1 นาทีขึ้นไปก็ยอดเยี่ยมแล้ว