ภาพรวม
Web Receiver SDK รองรับการจัดคิวด้วย
คิวเริ่มต้นที่ระบุโดย
SDK ที่ใช้
QueueData
และ
QueueManager
หรือใช้คิวที่กำหนดเองโดย
การใช้
cast.framework.QueueBase
และการใช้
QueueManager
Queueing API ช่วยให้แอปพลิเคชันผสานรวมกับ Cast ได้ดียิ่งขึ้นด้วยการให้ ฟีเจอร์ต่อไปนี้
- การสนับสนุนการใช้งานคิวในระบบคลาวด์ของ Google และพาร์ทเนอร์ไปยังภายนอก โหลดคิวที่จัดเก็บไว้และที่สร้างลงในอุปกรณ์แคสต์ได้โดยตรง
- กลไกที่อนุญาตให้ใส่เลขหน้าของรายการในคิวแทนการโหลด ทำทุกอย่างได้พร้อมกัน
- รองรับการรับส่งข้อความแบบใหม่ เช่น ไปที่รายการถัดไป รายการก่อนหน้า ดึงข้อมูลหน้าต่างของรายการ ตลอดจนการรับข้อมูลสื่อที่เกี่ยวข้องกับ ชุดของรายการคิว
- 
QueueManagerเพื่อจัดการการแทรก การนำออก และการอัปเดตรายการในคิว
คิวเริ่มต้น
Web Receiver SDK ให้การสนับสนุนคิวแบบจำกัดที่พร้อมใช้งานในแบบฟอร์ม ของคิวเริ่มต้น
หากต้องการใช้คิวเริ่มต้น ให้ระบุ
queueData
ใน LoadRequestData ของการโหลดจากฝั่งผู้ส่ง หรือส่งคำขอโหลดในเครื่อง
โดยใช้
PlayerManager#load
โปรดดูเพิ่มเติมที่การโหลดสื่อ
ที่ฝั่งผู้รับ คุณสามารถแก้ไขคิวได้โดยใช้
QueueManager
เมื่อสื่อเริ่มต้นถูกโหลดแล้ว
คิวที่กำหนดเอง
หากคิวเริ่มต้นไม่มีฟังก์ชันการจัดคิวที่จำเป็นสำหรับ คุณสามารถสร้างคิวที่กำหนดเองได้ ซึ่งช่วยให้ ความสามารถและความยืดหยุ่น
นักพัฒนาแอปพลิเคชันสามารถสร้างคิวฝั่งตัวรับเว็บได้โดยใช้
cast.framework.QueueBase
ต่อไปนี้เป็นตัวอย่างพื้นฐานของคิวง่ายๆ ซึ่ง
initialize
ลบล้างการเรียกแล้ว จากนั้นรายการในคิวพร้อมด้วยคำอธิบายคิว
สำหรับอุปกรณ์แคสต์
โปรดดูเพิ่มเติมที่การโหลดสื่อ
// Creates a simple queue with a combination of contents.
const DemoQueue = class extends cast.framework.QueueBase {
 constructor() {
   super();
   /**
    * List of media urls.
    * @private @const {!Array<string>}
    */
   this.myMediaUrls_ = [...];
 }
 /**
  * Provide a list of items.
  * @param {!cast.framework.messages.LoadRequestData} loadRequestData
  * @return {!cast.framework.messages.QueueData}
  */
 initialize(loadRequestData) {
   const items = [];
   for (const mediaUrl of this.myMediaUrls_) {
     const item = new cast.framework.messages.QueueItem();
     item.media = new cast.framework.messages.MediaInformation();
     item.media.contentId = mediaUrl;
     items.push(item);
   }
   let queueData = loadRequestData.queueData;
   // Create a new queue with media from the load request if one doesn't exist.
   if (!queueData) {
     queueData = new cast.framework.messages.QueueData();
     queueData.name = 'Your Queue Name';
     queueData.description = 'Your Queue Description';
     queueData.items = items;
     // Start with the first item in the playlist.
     queueData.startIndex = 0;
     // Start from 10 seconds into the first item.
     queueData.currentTime = 10;
   }
   return queueData;
 }
};
ในตัวอย่างนี้ รายการของรายการต่างๆ ใน
initialize
มีให้บริการในผู้ให้บริการ
QueueBase
การเรียกเครื่องมือสร้าง อย่างไรก็ตาม สำหรับการใช้คิวในระบบคลาวด์
ตรรกะของผู้รับสามารถเรียกข้อมูลรายการจากภายนอก จากนั้นจึงส่งคืนให้เป็นส่วนหนึ่งของ
เริ่มต้นการโทร
การสาธิตการใช้งาน API การจัดคิวที่ครอบคลุมยิ่งขึ้นมีดังนี้
คิวใช้งานส่วนใหญ่
QueueBase
const DemoQueue = class extends cast.framework.QueueBase {
 constructor() {
   /** @private {} */
   super();
   YourServer.onSomeEvent = this.updateEntireQueue_;
 }
 /**
  * Initializes the queue.
  * @param {!cast.framework.messages.LoadRequestData} loadRequestData
  * @return {!cast.framework.messages.QueueData}
  */
 initialize(loadRequestData) {
   let queueData = loadRequestData.queueData;
   // Create a new queue with media from the load request if one doesn't exist.
   if (!queueData) {
     queueData = new cast.framework.messages.QueueData();
     queueData.name = 'Your Queue Name';
     queueData.description = 'Your Queue Description';
     // Put the first set of items into the queue
     const items = this.nextItems();
     queueData.items = items;
     // Start with the first item in the playlist.
     queueData.startIndex = 0;
     // Start from 10 seconds into the first item.
     queueData.currentTime = 10;
   }
   return queueData;
 }
 /**
  * Picks a set of items from remote server after the reference item id and
  * return as the next items to be inserted into the queue. When
  * referenceItemId is omitted, items are simply appended to the end of the
  * queue.
  * @param {number} referenceItemId
  * @return {!Array<cast.framework.QueueItem>}
  */
 nextItems(referenceItemId) {
   // Assume your media has a itemId and the media url
   return this.constructQueueList_(YourServer.getNextMedias(referenceItemId));
 }
 /**
  * Picks a set of items from remote server before the reference item id and
  * return as the items to be inserted into the queue. When
  * referenceItemId is omitted, items are simply appended to beginning of the
  * queue.
  * @param {number} referenceItemId
  * @return {!Array<cast.framework.QueueItem>}
  */
 prevItems(referenceItemId) {
   return this.constructQueueList_(YourServer.getPrevMedias(referenceItemId));
 }
 /**
  * Constructs a list of QueueItems based on the media information containing
  * the item id and the media url.
  * @param {number} referenceItemId
  * @return {!Array<cast.framework.QueueItem>}
  */
 constructQueueList_(medias) {
   const items = [];
   for (media of medias) {
     const item = new cast.framework.messages.QueueItem(media.itemId);
     item.media = new cast.framework.messages.MediaInformation();
     item.media.contentId = media.url;
     items.push(item);
   }
   return items;
 }
 /**
  * Logs the currently playing item.
  * @param {number} itemId The unique id for the item.
  * @export
  */
 onCurrentItemIdChanged(itemId) {
   console.log('We are now playing video ' + itemId);
   YourServer.trackUsage(itemId);
 }
};
ในตัวอย่างด้านบน YourServer คือเซิร์ฟเวอร์คิวในระบบคลาวด์และมีตรรกะ
เกี่ยวกับวิธีดึงข้อมูลสื่อบางรายการ
หากต้องการใช้งาน QueueBase
-ใช้คิวแล้ว รายการหนึ่งจะตั้งค่าตัวเลือกคิวใน
CastReceiverContext:
const context = cast.framework.CastReceiverContext.getInstance();
context.start({queue: new DemoQueue()});
การจัดการคิว
QueueManager
ช่วยให้นักพัฒนาซอฟต์แวร์มีความยืดหยุ่นในการพัฒนาโซลูชันการจัดคิวโดย
เพื่อเข้าถึงรายการคิวที่จัดเก็บอยู่ในปัจจุบัน รวมถึง
รายการที่กำลังเล่น และยังให้การดำเนินการต่างๆ เช่น การแทรก การนำออก
และการอัปเดตรายการในคิว ข้อมูลโค้ดต่อไปนี้แสดงวิธีเข้าถึง
อินสแตนซ์ของ
QueueManager:
const context = cast.framework.CastReceiverContext.getInstance();
const queueManager = context.getPlayerManager().getQueueManager();
การจัดการคิวตามค่าเริ่มต้น
เมื่อคิวเริ่มต้นโหลดขึ้นมาแล้ว
QueueManager
สามารถใช้เพื่อดำเนินการต่างๆ เช่น เรียกดูรายการปัจจุบัน ดึงข้อมูล
รายการทั้งหมดในคิว และอัปเดตรายการในคิวโดยใช้
insertItems,
removeItems,
และ
updateItems
การจัดการคิวที่กำหนดเอง
ต่อไปนี้เป็นตัวอย่างการติดตั้งใช้งานคิวที่กำหนดเองซึ่งใช้การแทรกและ
วิธีการนําออกซึ่งอิงจากเหตุการณ์บางอย่าง ตัวอย่างนี้ยังแสดงให้เห็นการใช้งาน
updateItems
ซึ่งนักพัฒนาซอฟต์แวร์สามารถแก้ไขรายการในคิวในคิวที่มีอยู่ได้ เช่น
การนำช่วงพักโฆษณาออก
const DemoQueue = class extends cast.framework.QueueBase {
  constructor() {
    super();
    /** @private @const {!cast.framework.QueueManager} */
    this.queueManager_ = context.getPlayerManager().getQueueManager();
  }
  /**
   * Provide a list of items.
   * @param {!cast.framework.messages.LoadRequestData} loadRequestData
   * @return {!cast.framework.messages.QueueData}
   */
  initialize(loadRequestData) {
    // Your normal initialization; see examples above.
    return queueData;
  }
  /** Inserts items to the queue. */
  onSomeEventTriggeringInsertionToQueue() {
    const twoMoreUrls = ['http://url1', 'http://url2'];
    const items = [];
    for (const mediaUrl of twoMoreUrls) {
      const item = new cast.framework.QueueItem();
      item.media = new cast.framework.messages.MediaInformation();
      item.media.contentId = mediaUrl;
      items.push(item);
    }
    // Insert two more items after the current playing item.
    const allItems = this.queueManager_.getItems();
    const currentItemIndex = this.queueManager_.getCurrentItemIndex();
    const nextItemIndex = currentItemIndex + 1;
    let insertBefore = undefined;
    if (currentItemIndex >= 0 &&
        currentItemIndex < allItems.length - 1) {
      insertBefore = allItems[nextItemIndex].itemId;
    }
    this.queueManager_.insertItems(items, insertBefore);
  }
  /** Removes a particular item from the queue. */
  onSomeEventTriggeringRemovalFromQueue() {
    this.queueManager_.removeItems([2]);
  }
  /** Removes all the ads from all the items across the entire queue. */
  onUserBoughtAdFreeVersion() {
    const items = this.queueManager_.getItems();
    this.queueManager_.updateItems(items.map(item => {
      item.media.breaks = undefined;
      return item;
    }));
  }
};
ข้อความขาเข้าและขาออก
เพื่อรองรับการดึงข้อมูลคิวฝั่งผู้รับเป็นแหล่งที่มาของความจริงโดยสมบูรณ์ CAF ได้แนะนำและจัดการข้อมูลในคิวเพิ่มเติมต่อไปนี้ SDK ตัวรับ:
| ข้อความขาเข้า | พารามิเตอร์ | ข้อความตอบกลับขาออก | ส่งคืน | 
| ถัดไป | ไม่ต้องมีพารามิเตอร์ | MEDIA_STATUS | ตัวรับจะ (ดึงข้อมูลผ่าน nextItems() หากจำเป็น) และเริ่มเล่น รายการถัดไป | 
| ก่อนหน้า | ไม่ต้องมีพารามิเตอร์ | MEDIA_STATUS | ตัวรับเว็บจะ (ดึงข้อมูลผ่าน prevItems() หากจำเป็น) และเริ่มต้น กำลังเล่นรายการก่อนหน้า | 
| FETCH_ITEMS | FetchItemsRequestData | QUEUE_CHANGE | Cast.framework.messages.QueueChange ตัวอย่างเช่น สำหรับเคสแบบแทรก ฟิลด์รายการใน JSON จะมีรายการของรายการที่ดึงข้อมูลใหม่ | 
| GET_ITEMS_INFO | GetItemsInfoRequestData ที่มี itemIds: อาร์เรย์<จำนวน> | ITEMS_INFO | Cast.framework.messages.ItemsInfo พร้อมกับข้อมูลรายการในคิว | 
| GET_QUEUE_IDS | ไม่ต้องมีพารามิเตอร์ | QUEUE_IDS | cast.framework.messages.QueueIds. | 
สำหรับ NEXT/PREVIOUS หากการแสดงคิวที่มีอยู่ในเว็บรีซีฟเวอร์
ไม่มีรายการเพิ่มเติม
QueueBase.nextItems()
หรือ
QueueBase.prevItems()
ถูกเรียกโดยอัตโนมัติให้รับรายการเพิ่มเติม
สำหรับ FETCH_ITEM ฟังก์ชันที่เกี่ยวข้อง
fetchItems
ในการใช้งาน QueueBase จะถูกเรียกคิวของระบบคลาวด์ ซึ่งเรียกข้อมูล
ข้อมูลที่เกี่ยวข้องที่จะส่งกลับไปยัง Web Receiver เพื่อจัดเก็บ
เมื่อใดก็ตามที่มีการดึงข้อมูลรายการมากขึ้น ระบบจะทริกเกอร์ข้อความประเภท QUEUE_CHANGE ใหม่
และส่งกลับไปยังผู้ส่ง ดูประเภทต่างๆ ของ
การเปลี่ยนแปลงคิว
สำหรับ GET_ITEMS_INFO
ของ QueueBase
จะไม่มีการทริกเกอร์และตัวรับเว็บจะส่งคืนข้อมูลสื่อ
อยู่ในรายการรหัสอยู่แล้ว
การสุ่มคิว
หากต้องการตั้งค่าให้สับเปลี่ยนรายการในคิว ให้ตั้งค่า
shuffle
ธงของ
QueueData
เป็น true เมื่อโหลดรายการลงในคิว
หากคุณกำลังใช้การติดตั้งใช้งาน
QueueBase ใช้
เวลา
shuffle
ในการแสดงรายการที่สับเปลี่ยนกัน
หากต้องการสับเปลี่ยนคิวที่มีอยู่ ให้ใช้
shuffle
ธงของQUEUE_UPDATE
MessageType,
แทนที่จะเป็นคำสั่ง QUEUE_SHUFFLE โปรดดู
QueueUpdateRequestData
เพื่อดูข้อมูลเพิ่มเติม
โหมดเล่นซ้ำ
หากต้องการกำหนดให้รายการในคิวซ้ำกัน ให้ตั้งค่า
repeatMode
ทรัพย์สินของ
QueueData
เป็นที่ต้องการ
RepeatMode
เมื่อโหลดรายการลงในคิว
หากต้องการเปลี่ยน RepeatMode ของคิวที่มีอยู่ ให้ใช้เมธอด
repeatMode
ของ
QueueUpdateRequestData,
ซึ่งใช้ QUEUE_UPDATE
MessageType