# การเล่นโฆษณา (Advertisement)

ByteArk Player SDK for Android รองรับการแสดงโฆษณาผ่าน Google IMA SDK ตามมาตรฐาน Video Ad Serving Template (VAST) (opens new window) และ VMAP รวมทั้งรองรับ Companion Ads

# การเปิดใช้งาน

val playerBuilder = ByteArkPlayerBuilder.Builder()
  .withContext(this)
  .withControl()
  .withAds()
  .withAdTagUrl("https://example.com/vast/tag.xml")
  .build()
1
2
3
4
5
6

# กำหนด Ad tag ต่อวิดีโอ

นอกจากกำหนดผ่าน ByteArkPlayerBuilder แล้ว ยังสามารถกำหนด Ad tag แยกต่อวิดีโอผ่าน ByteArkPlayerItem ได้ด้วย

val playerItem = ByteArkPlayerItemBuilder.Builder()
  .withMediaId("VIDEO_ID")
  .withTitle("Big Buck Bunny")
  .withUrl("https://example.com/playlist.m3u8")
  .withAdTagUrl("https://example.com/vast/tag.xml")
  .build()
1
2
3
4
5
6

# Companion Ads

สามารถกำหนด Container สำหรับ Companion Ads ที่จะแสดงข้าง Player

val companionAdSlot = ByteArkPlayerFragment.createCompanionAdSlot(
  mCompanionAdsSlotContainer,  // ViewGroup ที่จะใช้แสดง
  728,                          // ความกว้าง (pixel)
  90                            // ความสูง (pixel)
)
mPlayerFragment = ByteArkPlayerFragment.createInstance(
  playerBuilder,
  mFragmentListener,
  companionAdSlot,
  // ...
)
1
2
3
4
5
6
7
8
9
10
11
12

หรือใช้ Helper สำหรับ Default Companion Ad slot

val playerBuilder = ByteArkPlayerBuilder.Builder()
  // ...
  .withDefaultCompanionAdSlot()
  .withDefaultCompanionAdSize(728, 90)
  .build()
1
2
3
4
5

# รับ Event ของโฆษณา

override fun onAdsRequest() {
  // ส่งคำขอโฆษณา
}
override fun onAdsStart() {
  // เริ่มเล่นโฆษณา
}
override fun onAdsEnd() {
  // จบโฆษณาแต่ละชิ้น
}
override fun onAdsComplete() {
  // โฆษณาทั้งหมดเล่นเสร็จ
}
override fun onAdsError(errorMessage: String?) {
  // เกิด Error ในการเล่นโฆษณา
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19