เปิดใช้เว็บแคสต์

จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ

1. ภาพรวม

โลโก้ Google Cast

Codelab นี้จะสอนวิธีแก้ไขแอปวิดีโอบนเว็บที่มีอยู่เพื่อแคสต์เนื้อหาในอุปกรณ์ที่พร้อมใช้งาน Google Cast

Google Cast คืออะไร

Google Cast ช่วยให้ผู้ใช้แคสต์เนื้อหาจากอุปกรณ์เคลื่อนที่ไปยังทีวีได้ ผู้ใช้สามารถใช้อุปกรณ์เคลื่อนที่เป็นรีโมตคอนโทรลสําหรับการเล่นสื่อบนทีวีได้

Google Cast SDK ช่วยให้คุณขยายแอปเพื่อควบคุมทีวีหรือระบบเสียง Cast SDK ช่วยให้คุณเพิ่มคอมโพเนนต์ UI ที่จําเป็นได้ตามรายการตรวจสอบการออกแบบ Google Cast

รายการตรวจสอบการออกแบบ Google Cast มีไว้เพื่อช่วยให้ผู้ใช้ได้รับประสบการณ์การแคสต์ที่ใช้งานง่ายและคาดการณ์ได้ในทุกแพลตฟอร์มที่รองรับ

สิ่งที่เรากําลังสร้าง

เมื่อใช้ Codelab เสร็จสิ้น คุณจะมีแอปวิดีโอบนเว็บ Chrome ที่แคสต์วิดีโอไปยังอุปกรณ์ Google Cast ได้

สิ่งที่คุณจะได้เรียนรู้

  • วิธีเพิ่ม Google Cast SDK ลงในแอปวิดีโอตัวอย่าง
  • วิธีเพิ่มปุ่ม "แคสต์" เพื่อเลือกอุปกรณ์ Google Cast
  • วิธีเชื่อมต่อกับอุปกรณ์แคสต์และเปิดตัวรับสื่อ
  • วิธีแคสต์วิดีโอ
  • วิธีผสานรวม Cast Connect

สิ่งที่ต้องมี

  • เบราว์เซอร์ล่าสุดใน Google Chrome
  • บริการโฮสติ้ง HTTPS เช่น Firebase Hosting หรือ ngrok
  • อุปกรณ์ Google Cast เช่น Chromecast หรือ Android TV ที่กําหนดค่าด้วยการเข้าถึงอินเทอร์เน็ต
  • ทีวีหรือจอภาพที่มีอินพุต HDMI
  • ต้องมี Chromecast ที่มี Google TV เพื่อทดสอบการผสานรวม Cast Connect แต่ไม่บังคับสําหรับ Codelab ที่เหลือ หากไม่มี โปรดข้ามขั้นตอนเพิ่มการรองรับ Cast Connect ไปที่ด้านล่างของบทแนะนํานี้

ประสบการณ์การใช้งาน

  • คุณจะต้องมีความรู้เกี่ยวกับการพัฒนาเว็บมาก่อน
  • และคุณก็ต้องมีความรู้เกี่ยวกับการดูทีวีด้วยนะ :)

คุณจะใช้บทแนะนํานี้อย่างไร

อ่านแบบฝึกหัดเท่านั้น อ่านและทําตามแบบฝึกหัด

คุณจะให้คะแนนประสบการณ์ในการสร้างเว็บแอปอย่างไร

ผู้ฝึกหัด ระดับกลาง ผู้ชํานาญ

คุณจะให้คะแนนประสบการณ์ในการรับชมทีวีอย่างไร

ผู้ฝึกหัด ระดับกลาง ผู้ชํานาญ

2. รับโค้ดตัวอย่าง

คุณสามารถดาวน์โหลดโค้ดตัวอย่างทั้งหมดลงในคอมพิวเตอร์...

และแตกไฟล์ ZIP ที่ดาวน์โหลด

3. เรียกใช้แอปตัวอย่าง

โลโก้ Google Chrome

ก่อนอื่น เรามาดูว่าแอปตัวอย่างที่สมบูรณ์มีลักษณะอย่างไร แอปเป็นโปรแกรมเล่นวิดีโอพื้นฐาน ผู้ใช้สามารถเลือกวิดีโอจากรายการและเล่นวิดีโอในเครื่องหรือแคสต์ไปยังอุปกรณ์ Google Cast ได้

หากต้องการใช้โฮสต์ที่เสร็จสมบูรณ์ โฮสต์นั้นต้องโฮสต์ไว้

หากไม่มีเซิร์ฟเวอร์ที่ใช้งานได้ ให้ใช้ Firebase Hosting หรือ ngrok

เรียกใช้เซิร์ฟเวอร์

เมื่อคุณตั้งค่าบริการที่ต้องการแล้ว ให้ไปที่ app-done และเริ่มเซิร์ฟเวอร์

ในเบราว์เซอร์ ให้ไปที่ URL ของ https เพื่อดูตัวอย่างที่โฮสต์ไว้

  1. คุณจะเห็นแอปวิดีโอปรากฏขึ้น
  2. คลิกปุ่ม "แคสต์" และเลือกอุปกรณ์ Google Cast
  3. เลือกวิดีโอ แล้วคลิกปุ่มเล่น
  4. วิดีโอจะเริ่มเล่นในอุปกรณ์ Google Cast

รูปภาพวิดีโอกําลังเล่นในอุปกรณ์แคสต์

คลิกปุ่มหยุดชั่วคราวในองค์ประกอบวิดีโอเพื่อหยุดวิดีโอชั่วคราวในอุปกรณ์รับ คลิกปุ่มเล่นในองค์ประกอบวิดีโอเพื่อเล่นวิดีโอต่อ

คลิกปุ่ม "แคสต์" เพื่อหยุดแคสต์ไปยังอุปกรณ์ Google Cast

ก่อนจะดําเนินการต่อ โปรดหยุดเซิร์ฟเวอร์

4. เตรียมโปรเจ็กต์เริ่มต้น

รูปภาพวิดีโอกําลังเล่นในอุปกรณ์แคสต์

เราต้องเพิ่มการรองรับ Google Cast ในแอปเริ่มต้นที่คุณดาวน์โหลด คําศัพท์ของ Google Cast ต่อไปนี้เราจะใช้ใน Codelab นี้

  • แอปผู้ส่งทํางานในอุปกรณ์เคลื่อนที่หรือแล็ปท็อป
  • แอปตัวรับทํางานในอุปกรณ์ Google Cast

ตอนนี้คุณพร้อมที่จะสร้างโปรเจ็กต์เริ่มต้นโดยใช้เครื่องมือแก้ไขข้อความที่ชื่นชอบแล้ว โดยทําดังนี้

  1. เลือกไดเรกทอรี ไอคอนโฟลเดอร์app-start จากการดาวน์โหลดโค้ดตัวอย่าง
  2. เรียกใช้แอปโดยใช้เซิร์ฟเวอร์ของคุณและสํารวจ UI

โปรดทราบว่าขณะทํางานกับ Codelab นี้ คุณจะต้องโฮสต์ตัวอย่างบนเซิร์ฟเวอร์อีกครั้งโดยขึ้นอยู่กับบริการ

การออกแบบแอป

แอปดึงข้อมูลรายการวิดีโอจากเว็บเซิร์ฟเวอร์ระยะไกลและให้รายการสําหรับผู้ใช้เรียกดู ผู้ใช้สามารถเลือกวิดีโอเพื่อดูรายละเอียดหรือเล่นวิดีโอในเครื่องได้

แอปประกอบด้วยมุมมองหลัก 1 รายการ ที่กําหนดไว้ใน index.html และตัวควบคุมหลัก CastVideos.js.

index.html

ไฟล์ HTML นี้ประกาศ UI เกือบทั้งหมดของเว็บแอป

เรามี div#main_video ยอดดูซึ่งมีองค์ประกอบต่างๆ ในวิดีโอ ซึ่งก็คือยอดดูบางส่วน ที่เกี่ยวข้องกับ div ของวิดีโอ เรามี div#media_control ซึ่งกําหนดการควบคุมทั้งหมดสําหรับองค์ประกอบวิดีโอ ด้านล่างคือ media_info ซึ่งแสดงรายละเอียดของวิดีโอในมุมมองของคุณ สุดท้าย div ของ carousel จะแสดงรายการวิดีโอใน div

นอกจากนี้ ไฟล์ index.html ยังบูต Cast SDK และแจ้งให้โหลดฟังก์ชัน CastVideos ด้วย

เนื้อหาส่วนใหญ่ที่จะป้อนองค์ประกอบเหล่านี้ได้รับการกําหนด แทรก และควบคุมใน CastVideos.js เรามาดูรายละเอียดกัน

CastVideos.js

สคริปต์นี้จะจัดการตรรกะทั้งหมดสําหรับเว็บแอปสําหรับวิดีโอแคสต์ รายการวิดีโอและข้อมูลเมตาที่เกี่ยวข้องซึ่งกําหนดไว้ใน CastVideos.js มีอยู่ในออบเจ็กต์ชื่อ mediaJSON

มีส่วนหลัก 2-3 ส่วนด้วยกันที่รับผิดชอบในการจัดการและเล่นวิดีโอทั้งในพื้นที่และจากระยะไกล โดยรวมแล้ว นี่จะเป็นเว็บแอปพลิเคชันที่ค่อนข้างตรงไปตรงมา

CastPlayer เป็นคลาสหลักที่จัดการแอปทั้งหมด ตั้งค่าโปรแกรมเล่น เลือกสื่อ และเชื่อมโยงเหตุการณ์กับ PlayerHandler สําหรับการเล่นสื่อ CastPlayer.prototype.initializeCastPlayer คือวิธีตั้งค่าฟังก์ชันแคสต์ทั้งหมด CastPlayer.prototype.switchPlayer จะเปลี่ยนสถานะระหว่างโปรแกรมเล่นในเครื่องและโปรแกรมเล่นระยะไกล CastPlayer.prototype.setupLocalPlayer และ CastPlayer.prototype.setupRemotePlayer จะเริ่มต้นโปรแกรมเล่นในเครื่องและระยะไกล

PlayerHandler เป็นชั้นเรียนที่รับผิดชอบการจัดการการเล่นสื่อ วิธีอื่นๆ ที่รับผิดชอบรายละเอียดของการจัดการสื่อและการเล่นมีดังนี้

คำถามที่พบบ่อย

5. การเพิ่มปุ่ม "แคสต์"

รูปภาพของแอปที่พร้อมใช้งาน Cast

แอปพลิเคชันที่พร้อมใช้งาน Cast จะแสดงปุ่ม "แคสต์" ในองค์ประกอบวิดีโอ การคลิกปุ่มแคสต์จะแสดงรายการอุปกรณ์แคสต์ที่ผู้ใช้สามารถเลือกได้ หากผู้ใช้เล่นเนื้อหาภายในอุปกรณ์ของผู้ส่ง การเลือกอุปกรณ์แคสต์จะเริ่มต้นหรือเล่นต่อในอุปกรณ์แคสต์นั้น ผู้ใช้สามารถคลิกปุ่ม "แคสต์" และหยุดแคสต์แอปพลิเคชันไปยังอุปกรณ์แคสต์ได้ทุกเมื่อระหว่างเซสชันการแคสต์ ผู้ใช้ต้องเชื่อมต่อหรือยกเลิกการเชื่อมต่อจากอุปกรณ์แคสต์ได้ขณะอยู่ในหน้าจอของแอปพลิเคชัน ตามที่อธิบายไว้ในรายการตรวจสอบการออกแบบ Google Cast

การกำหนดค่า

โปรเจ็กต์เริ่มต้นต้องอาศัยทรัพยากร Dependency และการตั้งค่าแบบเดียวกับที่ทําสําหรับแอปตัวอย่างที่สมบูรณ์ แต่ครั้งนี้โฮสต์เนื้อหาของ app-start

ในเบราว์เซอร์ ให้ไปที่ URL https ของตัวอย่างที่โฮสต์

โปรดทราบว่าเมื่อทําการเปลี่ยนแปลง คุณจะต้องโฮสต์ตัวอย่างบนเซิร์ฟเวอร์อีกครั้งโดยขึ้นอยู่กับบริการ

การเริ่มต้น

เฟรมเวิร์ก "แคสต์" มีออบเจ็กต์ Singleton ทั่วโลกที่ชื่อว่า CastContext ซึ่งจะเชื่อมโยงกิจกรรมทั้งหมดของเฟรมเวิร์ก ออบเจ็กต์นี้จะต้องเริ่มต้นก่อนในวงจรของแอปพลิเคชัน ซึ่งโดยทั่วไปแล้วเรียกจากโค้ดเรียกกลับที่กําหนดให้กับ window['__onGCastApiAvailable'] ซึ่งเรียกใช้หลังจากที่โหลด SDK ของ Cast แล้ว และใช้งานได้ ในกรณีนี้ ระบบจะเรียก CastContext ใน CastPlayer.prototype.initializeCastPlayer ซึ่งเป็นการเรียกจากโค้ดเรียกกลับที่กล่าวถึงข้างต้น

ต้องระบุออบเจ็กต์ JSON ของ options เมื่อเริ่มต้น CastContext คลาสนี้มีตัวเลือกที่มีผลต่อการทํางานของเฟรมเวิร์ก สิ่งสําคัญที่สุดคือรหัสแอปพลิเคชันตัวรับสัญญาณที่ใช้กรองรายการอุปกรณ์แคสต์ที่พร้อมใช้งานเพื่อแสดงเฉพาะอุปกรณ์ที่สามารถเรียกใช้แอปที่ระบุและเรียกใช้งานแอปพลิเคชันตัวรับเมื่อเซสชันการแคสต์เริ่มต้นเท่านั้น

เมื่อคุณพัฒนาแอปที่พร้อมใช้งาน Cast ของคุณเอง คุณจะต้องลงทะเบียนเป็นนักพัฒนาซอฟต์แวร์การแคสต์ แล้วรับรหัสแอปพลิเคชันสําหรับแอปของคุณ สําหรับ Codelab นี้ เราจะใช้รหัสแอปตัวอย่าง

เพิ่มโค้ดต่อไปนี้ไว้ที่ index.html ที่ท้ายส่วน body

<script type="text/javascript" src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1"></script>

เพิ่มโค้ดต่อไปนี้ลงใน index.html เพื่อเริ่มต้นใช้งานแอป CastVideos รวมถึงเริ่มต้น CastContext

<script src="CastVideos.js"></script>
<script type="text/javascript">
var castPlayer = new CastPlayer();
window['__onGCastApiAvailable'] = function(isAvailable) {
  if (isAvailable) {
    castPlayer.initializeCastPlayer();
  }
};
</script>

ตอนนี้เราต้องเพิ่มวิธีการใหม่ใน CastVideos.js ซึ่งสอดคล้องกับวิธีที่เพิ่งเรียกใช้ไปเมื่อ index.html มาเพิ่มวิธีการใหม่ที่เรียกว่า initializeCastPlayer ซึ่งจะตั้งค่าตัวเลือกใน CastContext แล้วเริ่ม RemotePlayer และ RemotePlayerControllers ใหม่

/**
 * This method sets up the CastContext, and a few other members
 * that are necessary to play and control videos on a Cast
 * device.
 */
CastPlayer.prototype.initializeCastPlayer = function() {

    var options = {};

    // Set the receiver application ID to your own (created in
    // the Google Cast Developer Console), or optionally
    // use the chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID
    options.receiverApplicationId = 'C0868879';

    // Auto join policy can be one of the following three:
    // ORIGIN_SCOPED - Auto connect from same appId and page origin
    // TAB_AND_ORIGIN_SCOPED - Auto connect from same appId, page origin, and tab
    // PAGE_SCOPED - No auto connect
    options.autoJoinPolicy = chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED;

    cast.framework.CastContext.getInstance().setOptions(options);

    this.remotePlayer = new cast.framework.RemotePlayer();
    this.remotePlayerController = new cast.framework.RemotePlayerController(this.remotePlayer);
    this.remotePlayerController.addEventListener(
        cast.framework.RemotePlayerEventType.IS_CONNECTED_CHANGED,
        this.switchPlayer.bind(this)
    );
};

สุดท้ายเราต้องสร้างตัวแปรสําหรับ RemotePlayer และ RemotePlayerController

var CastPlayer = function() {
  //...
  /* Cast player variables */
  /** @type {cast.framework.RemotePlayer} */
  this.remotePlayer = null;
  /** @type {cast.framework.RemotePlayerController} */
  this.remotePlayerController = null;
  //...
};

ปุ่ม "แคสต์"

เมื่อ CastContext เริ่มต้นแล้ว เราต้องเพิ่มปุ่ม "แคสต์" เพื่อให้ผู้ใช้เลือกอุปกรณ์แคสต์ได้ Cast SDK มีคอมโพเนนต์ปุ่ม "แคสต์" ที่ชื่อ google-cast-launcher ที่มีรหัส "castbutton"" สามารถเพิ่มลงในองค์ประกอบวิดีโอของแอปพลิเคชันได้โดยการเพิ่ม button ในส่วน media_control

องค์ประกอบของปุ่มจะมีลักษณะดังนี้

<google-cast-launcher id="castbutton"></google-cast-launcher>

เพิ่มโค้ดต่อไปนี้ลงใน index.html ในส่วน media_control

<div id="media_control">
  <div id="play"></div>
  <div id="pause"></div>
  <div id="progress_bg"></div>
  <div id="progress"></div>
  <div id="progress_indicator"></div>
  <div id="fullscreen_expand"></div>
  <div id="fullscreen_collapse"></div>
  <google-cast-launcher id="castbutton"></google-cast-launcher>
  <div id="audio_bg"></div>
  <div id="audio_bg_track"></div>
  <div id="audio_indicator"></div>
  <div id="audio_bg_level"></div>
  <div id="audio_on"></div>
  <div id="audio_off"></div>
  <div id="duration">00:00:00</div>
</div>

แล้วรีเฟรชหน้าเว็บในเบราว์เซอร์ Chrome คุณควรเห็นปุ่ม "แคสต์" ในองค์ประกอบวิดีโอ และเมื่อคุณคลิก จําเป็นต้องมีอุปกรณ์แคสต์ในเครือข่ายภายใน เบราว์เซอร์ Chrome จะจัดการการค้นพบอุปกรณ์โดยอัตโนมัติ เลือกอุปกรณ์แคสต์ แล้วแอปตัวรับตัวอย่างจะโหลดในอุปกรณ์แคสต์

เนื่องจากเรายังไม่ได้รองรับการรองรับการเล่นสื่อ คุณจึงยังเล่นวิดีโอในอุปกรณ์แคสต์ไม่ได้ คลิกปุ่ม "แคสต์" เพื่อหยุดแคสต์

6. การแคสต์เนื้อหาวิดีโอ

รูปภาพของแอปที่พร้อมใช้งาน Cast ที่มีเมนูการเลือกอุปกรณ์แคสต์

เราจะขยายตัวอย่างแอปให้เล่นวิดีโอจากระยะไกลในอุปกรณ์แคสต์ด้วย เราต้องฟังเหตุการณ์ต่างๆ ที่สร้างโดยเฟรมเวิร์กแคสต์

การแคสต์สื่อ

ในระดับสูง หากต้องการเล่นสื่อในอุปกรณ์แคสต์ คุณต้องดําเนินการต่อไปนี้

  1. สร้างออบเจ็กต์ JSON ของ MediaInfo จาก Cast SDK ซึ่งจําลองรายการสื่อ
  2. ผู้ใช้เชื่อมต่อกับอุปกรณ์แคสต์เพื่อเปิดแอปพลิเคชันตัวรับ
  3. โหลดออบเจ็กต์ MediaInfo ลงในตัวรับและเล่นเนื้อหา
  4. ติดตามสถานะสื่อ
  5. ส่งคําสั่งการเล่นไปยังผู้รับตามการโต้ตอบของผู้ใช้

ขั้นตอนที่ 1 ในการจับคู่ออบเจ็กต์หนึ่งกับออบเจ็กต์อื่น MediaInfo คือสิ่งที่ Cast SDK เข้าใจและ mediaJSON เป็นการรวมรายการแอปของแอปเรา เราสามารถแมป mediaJSON กับ MediaInfo ได้อย่างง่ายดาย เราได้ทําตามขั้นตอนที่ 2 ในส่วนก่อนหน้าแล้ว ขั้นตอนที่ 3 สามารถทําได้ง่ายๆ ด้วย Cast SDK

ตัวอย่างแอป CastPlayer แยกความแตกต่างระหว่างการเล่นในเครื่องกับการเล่นจากระยะไกลในเมธอด switchPlayer แล้ว

if (cast && cast.framework) {
  if (this.remotePlayer.isConnected) {
    //...

การทํา Codelab นี้ไม่ได้สําคัญสําหรับคุณในการทําความเข้าใจวิธีการทํางานของตรรกะของตัวอย่างโปรแกรมเล่นทั้งหมด อย่างไรก็ตาม คุณต้องเข้าใจว่าโปรแกรมเล่นสื่อของแอปจะต้องได้รับการแก้ไขให้รู้จักทั้งการเล่นในเครื่องและจากระยะไกล

ขณะนี้โปรแกรมเล่นในเครื่องจะอยู่ในสถานะการเล่นในเครื่องเสมอ เนื่องจากยังไม่รู้สถานะการแคสต์ เราจําเป็นต้องอัปเดต UI ตามการเปลี่ยนแปลงสถานะที่เกิดขึ้นในเฟรมเวิร์ก Cast เช่น หากเราเริ่มแคสต์ เราต้องหยุดการเล่นในเครื่องและปิดใช้การควบคุมบางอย่าง ในทํานองเดียวกัน หากเราหยุดแคสต์เมื่อเราอยู่ในตัวควบคุมการดูนี้ เราต้องเปลี่ยนไปใช้การเล่นในเครื่อง ในการจัดการว่าเราต้องฟังเหตุการณ์ต่างๆ ที่เฟรมเวิร์กแคสต์สร้างขึ้น

การจัดการเซสชันการแคสต์

สําหรับเฟรมเวิร์ก "แคสต์" เซสชันแคสต์จะรวมขั้นตอนการเชื่อมต่ออุปกรณ์ การเปิด (หรือเข้าร่วมเซสชันที่มีอยู่) การเชื่อมต่อกับแอปพลิเคชันตัวรับ และการเริ่มต้นแชแนลการควบคุมสื่อตามความเหมาะสม ช่องทางควบคุมสื่อคือวิธีที่เฟรมเวิร์กแคสต์ส่งและรับข้อความที่เกี่ยวข้องกับการเล่นสื่อจากตัวรับ

เซสชันการแคสต์จะเริ่มต้นโดยอัตโนมัติเมื่อผู้ใช้เลือกอุปกรณ์จากปุ่ม "แคสต์" และจะหยุดโดยอัตโนมัติเมื่อผู้ใช้ยกเลิกการเชื่อมต่อ เฟรมเวิร์ก "แคสต์" ยังจัดการการเชื่อมต่อเซสชันผู้รับอีกครั้งเนื่องจากปัญหาเครือข่ายโดยอัตโนมัติ

CastSession เป็นผู้จัดการเซสชันการแคสต์ ซึ่งเข้าถึงได้ผ่าน cast.framework.CastContext.getInstance().getCurrentSession() โค้ดเรียกกลับของ EventListener มีไว้เพื่อตรวจสอบเหตุการณ์ของเซสชัน เช่น การสร้าง การระงับ การเกิดซ้ํา และการสิ้นสุด

ในแอปพลิเคชันปัจจุบัน การจัดการเซสชันและสถานะทั้งหมดจะจัดการเราในเมธอด setupRemotePlayer มาเริ่มกําหนดค่านี้ในแอปโดยการเพิ่มโค้ดต่อไปนี้ลงใน CastVideos.js กัน

/**
 * Set the PlayerHandler target to use the remote player
 */
CastPlayer.prototype.setupRemotePlayer = function () {
    var castSession = cast.framework.CastContext.getInstance().getCurrentSession();

    this.playerHandler.setTarget(playerTarget);

    // Setup remote player volume right on setup
    // The remote player may have had a volume set from previous playback
    if (this.remotePlayer.isMuted) {
        this.playerHandler.mute();
    }
    var currentVolume = this.remotePlayer.volumeLevel * FULL_VOLUME_HEIGHT;
    var p = document.getElementById('audio_bg_level');
    p.style.height = currentVolume + 'px';
    p.style.marginTop = -currentVolume + 'px';

    this.hideFullscreenButton();

    this.playerHandler.play();
};

เรายังคงผูกเหตุการณ์ทั้งหมดจากการเรียกกลับ และจัดการกับเหตุการณ์ทั้งหมดที่เข้ามาใหม่ การดําเนินการนี้ไม่ซับซ้อนเลย ดังนั้นให้จัดการตอนนี้เลย

/**
 * Set the PlayerHandler target to use the remote player
 */
CastPlayer.prototype.setupRemotePlayer = function () {
    var castSession = cast.framework.CastContext.getInstance().getCurrentSession();

    // Add event listeners for player changes which may occur outside sender app
    this.remotePlayerController.addEventListener(
        cast.framework.RemotePlayerEventType.IS_PAUSED_CHANGED,
        function() {
            if (this.remotePlayer.isPaused) {
                this.playerHandler.pause();
            } else {
                this.playerHandler.play();
            }
        }.bind(this)
    );

    this.remotePlayerController.addEventListener(
        cast.framework.RemotePlayerEventType.IS_MUTED_CHANGED,
        function() {
            if (this.remotePlayer.isMuted) {
                this.playerHandler.mute();
            } else {
                this.playerHandler.unMute();
            }
        }.bind(this)
    );

    this.remotePlayerController.addEventListener(
        cast.framework.RemotePlayerEventType.VOLUME_LEVEL_CHANGED,
        function() {
            var newVolume = this.remotePlayer.volumeLevel * FULL_VOLUME_HEIGHT;
            var p = document.getElementById('audio_bg_level');
            p.style.height = newVolume + 'px';
            p.style.marginTop = -newVolume + 'px';
        }.bind(this)
    );

    // This object will implement PlayerHandler callbacks with
    // remotePlayerController, and makes necessary UI updates specific
    // to remote playback
    var playerTarget = {};

    playerTarget.play = function () {
        if (this.remotePlayer.isPaused) {
            this.remotePlayerController.playOrPause();
        }

        var vi = document.getElementById('video_image');
        vi.style.display = 'block';
        var localPlayer = document.getElementById('video_element');
        localPlayer.style.display = 'none';
    }.bind(this);

    playerTarget.pause = function () {
        if (!this.remotePlayer.isPaused) {
            this.remotePlayerController.playOrPause();
        }
    }.bind(this);

    playerTarget.stop = function () {
         this.remotePlayerController.stop();
    }.bind(this);

    playerTarget.getCurrentMediaTime = function() {
        return this.remotePlayer.currentTime;
    }.bind(this);

    playerTarget.getMediaDuration = function() {
        return this.remotePlayer.duration;
    }.bind(this);

    playerTarget.updateDisplayMessage = function () {
        document.getElementById('playerstate').style.display = 'block';
        document.getElementById('playerstatebg').style.display = 'block';
        document.getElementById('video_image_overlay').style.display = 'block';
        document.getElementById('playerstate').innerHTML =
            this.mediaContents[ this.currentMediaIndex]['title'] + ' ' +
            this.playerState + ' on ' + castSession.getCastDevice().friendlyName;
    }.bind(this);

    playerTarget.setVolume = function (volumeSliderPosition) {
        // Add resistance to avoid loud volume
        var currentVolume = this.remotePlayer.volumeLevel;
        var p = document.getElementById('audio_bg_level');
        if (volumeSliderPosition < FULL_VOLUME_HEIGHT) {
            var vScale =  this.currentVolume * FULL_VOLUME_HEIGHT;
            if (volumeSliderPosition > vScale) {
                volumeSliderPosition = vScale + (pos - vScale) / 2;
            }
            p.style.height = volumeSliderPosition + 'px';
            p.style.marginTop = -volumeSliderPosition + 'px';
            currentVolume = volumeSliderPosition / FULL_VOLUME_HEIGHT;
        } else {
            currentVolume = 1;
        }
        this.remotePlayer.volumeLevel = currentVolume;
        this.remotePlayerController.setVolumeLevel();
    }.bind(this);

    playerTarget.mute = function () {
        if (!this.remotePlayer.isMuted) {
            this.remotePlayerController.muteOrUnmute();
        }
    }.bind(this);

    playerTarget.unMute = function () {
        if (this.remotePlayer.isMuted) {
            this.remotePlayerController.muteOrUnmute();
        }
    }.bind(this);

    playerTarget.isMuted = function() {
        return this.remotePlayer.isMuted;
    }.bind(this);

    playerTarget.seekTo = function (time) {
        this.remotePlayer.currentTime = time;
        this.remotePlayerController.seek();
    }.bind(this);

    this.playerHandler.setTarget(playerTarget);

    // Setup remote player volume right on setup
    // The remote player may have had a volume set from previous playback
    if (this.remotePlayer.isMuted) {
        this.playerHandler.mute();
    }
    var currentVolume = this.remotePlayer.volumeLevel * FULL_VOLUME_HEIGHT;
    var p = document.getElementById('audio_bg_level');
    p.style.height = currentVolume + 'px';
    p.style.marginTop = -currentVolume + 'px';

    this.hideFullscreenButton();

    this.playerHandler.play();
};

กําลังโหลดสื่อ

ใน Cast SDK RemotePlayer และ RemotePlayerController มีชุด API ที่ใช้งานสะดวกสําหรับการจัดการการเล่นสื่อระยะไกลบนตัวรับ สําหรับ CastSession ที่รองรับการเล่นสื่อ ระบบจะสร้างอินสแตนซ์ของ RemotePlayer และ RemotePlayerController โดยอัตโนมัติโดย SDK คุณสามารถเข้าถึงได้โดยการสร้างอินสแตนซ์ของ cast.framework.RemotePlayer และ cast.framework.RemotePlayerController ตามลําดับดังที่แสดงใน Codelab ก่อนหน้านี้

ถัดไป เราต้องโหลดวิดีโอที่เลือกอยู่ในปัจจุบันในเครื่องรับโดยสร้างออบเจ็กต์ MediaInfo สําหรับ SDK เพื่อประมวลผลและส่งคําขอนั้น เพิ่มโค้ดต่อไปนี้ลงใน setupRemotePlayer เพื่อดําเนินการ

/**
 * Set the PlayerHandler target to use the remote player
 */
CastPlayer.prototype.setupRemotePlayer = function () {
    //...

    playerTarget.load = function (mediaIndex) {
        console.log('Loading...' + this.mediaContents[mediaIndex]['title']);
        var mediaInfo = new chrome.cast.media.MediaInfo(
            this.mediaContents[mediaIndex]['sources'][0], 'video/mp4');

        mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata();
        mediaInfo.metadata.metadataType = chrome.cast.media.MetadataType.GENERIC;
        mediaInfo.metadata.title = this.mediaContents[mediaIndex]['title'];
        mediaInfo.metadata.images = [
            {'url': MEDIA_SOURCE_ROOT + this.mediaContents[mediaIndex]['thumb']}];

        var request = new chrome.cast.media.LoadRequest(mediaInfo);
        castSession.loadMedia(request).then(
            this.playerHandler.loaded.bind(this.playerHandler),
            function (errorCode) {
                this.playerState = PLAYER_STATE.ERROR;
                console.log('Remote media load error: ' +
                    CastPlayer.getErrorMessage(errorCode));
            }.bind(this));
    }.bind(this);

    //...
};

เพิ่มวิธีการสลับระหว่างการเล่นในเครื่องและการเล่นจากระยะไกลได้แล้ว ดังนี้

/**
 * This is a method for switching between the local and remote
 * players. If the local player is selected, setupLocalPlayer()
 * is run. If there is a cast device connected we run
 * setupRemotePlayer().
 */
CastPlayer.prototype.switchPlayer = function() {
    this.stopProgressTimer();
    this.resetVolumeSlider();
    this.playerHandler.stop();
    this.playerState = PLAYER_STATE.IDLE;
    if (cast && cast.framework) {
        if (this.remotePlayer.isConnected) {
            this.setupRemotePlayer();
            return;
        }
    }
    this.setupLocalPlayer();
};

สุดท้าย ให้เพิ่มเมธอดเพื่อจัดการข้อความแสดงข้อผิดพลาดของ Cast ดังนี้

/**
 * Makes human-readable message from chrome.cast.Error
 * @param {chrome.cast.Error} error
 * @return {string} error message
 */
CastPlayer.getErrorMessage = function(error) {
  switch (error.code) {
    case chrome.cast.ErrorCode.API_NOT_INITIALIZED:
      return 'The API is not initialized.' +
        (error.description ? ' :' + error.description : '');
    case chrome.cast.ErrorCode.CANCEL:
      return 'The operation was canceled by the user' +
        (error.description ? ' :' + error.description : '');
    case chrome.cast.ErrorCode.CHANNEL_ERROR:
      return 'A channel to the receiver is not available.' +
        (error.description ? ' :' + error.description : '');
    case chrome.cast.ErrorCode.EXTENSION_MISSING:
      return 'The Cast extension is not available.' +
        (error.description ? ' :' + error.description : '');
    case chrome.cast.ErrorCode.INVALID_PARAMETER:
      return 'The parameters to the operation were not valid.' +
        (error.description ? ' :' + error.description : '');
    case chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE:
      return 'No receiver was compatible with the session request.' +
        (error.description ? ' :' + error.description : '');
    case chrome.cast.ErrorCode.SESSION_ERROR:
      return 'A session could not be created, or a session was invalid.' +
        (error.description ? ' :' + error.description : '');
    case chrome.cast.ErrorCode.TIMEOUT:
      return 'The operation timed out.' +
        (error.description ? ' :' + error.description : '');
  }
};

เรียกใช้แอป เชื่อมต่อกับอุปกรณ์แคสต์และเริ่มเล่นวิดีโอ คุณควรเห็นวิดีโอกําลังเล่นบนตัวรับ

7. เพิ่มการรองรับ Cast Connect

ไลบรารี Cast Connect ช่วยให้แอปพลิเคชันผู้ส่งที่มีอยู่สามารถสื่อสารกับแอปพลิเคชัน Android TV ผ่านโปรโตคอล Cast Cast Connect สร้างขึ้นบนโครงสร้างพื้นฐาน Cast โดยแอป Android TV จะทําหน้าที่เป็นตัวรับ

การอ้างอิง

  • เบราว์เซอร์ Chrome เวอร์ชัน M87 ขึ้นไป

ตั้งค่าตัวรับสัญญาณ Android ที่ใช้ร่วมกันได้

ในการเปิดใช้แอปพลิเคชัน Android TV หรือเรียกอีกอย่างว่าตัวรับ Android เราจําเป็นต้องตั้งค่าแฟล็ก androidReceiverCompatible เป็น "จริง" ในออบเจ็กต์ CastOptions

เพิ่มโค้ดต่อไปนี้ลงใน CastVideos.js ในฟังก์ชัน initializeCastPlayer

var options = {};
...
options.androidReceiverCompatible = true;

cast.framework.CastContext.getInstance().setOptions(options);

ตั้งค่าข้อมูลเข้าสู่ระบบที่ใช้

ทางฝั่งผู้ส่ง คุณสามารถระบุ CredentialsData เพื่อแสดงว่าใครเข้าร่วมเซสชันบ้าง credentials เป็นสตริงที่ผู้ใช้กําหนดได้ ตราบใดที่แอป ATV เข้าใจได้ CredentialsData จะส่งไปยังแอป Android TV ระหว่างเวลาเปิดตัวหรือเข้าร่วมเท่านั้น หากตั้งค่าไว้อีกครั้งขณะที่เชื่อมต่ออยู่ ระบบจะไม่ส่งรหัสดังกล่าวไปยังแอป Android TV

หากต้องการระบุข้อมูลเข้าสู่ระบบ CredentialsData ต้องกําหนดได้ทุกเมื่อหลังจากตั้งค่าตัวเลือกการเปิดตัวแล้ว

เพิ่มโค้ดต่อไปนี้ในชั้นเรียน CastVideos.js ของคุณภายใต้ฟังก์ชัน initializeCastPlayer

cast.framework.CastContext.getInstance().setOptions(options);
...
let credentialsData = new chrome.cast.CredentialsData("{\"userId\": \"abc\"}");
cast.framework.CastContext.getInstance().setLaunchCredentialsData(credentialsData);
...

ระบุข้อมูลเข้าสู่ระบบในคําขอโหลด

ในกรณีที่แอป Web Receiver และแอป Android TV จัดการ credentials แตกต่างกัน คุณอาจต้องกําหนดข้อมูลเข้าสู่ระบบแยกกันสําหรับแต่ละแอป หากต้องการดําเนินการดังกล่าว ให้เพิ่มโค้ดต่อไปนี้ใน CastVideos.js ในส่วน playerTarget.load ในฟังก์ชัน setupRemotePlayer

...
var request = new chrome.cast.media.LoadRequest(mediaInfo);
request.credentials = 'user-credentials';
request.atvCredentials = 'atv-user-credentials';
...

โดย SDK จะจัดการข้อมูลเข้าสู่ระบบที่จะใช้สําหรับเซสชันปัจจุบันโดยอัตโนมัติ ทั้งนี้ขึ้นอยู่กับแอปของผู้รับที่แคสต์ไป

กําลังทดสอบ Cast Connect

ขั้นตอนการติดตั้ง Android TV APK ใน Chromecast ที่มี Google TV

  1. ค้นหาที่อยู่ IP ของอุปกรณ์ Android TV โดยปกติแล้วจะพร้อมใช้งานในส่วนการตั้งค่า > เครือข่ายและอินเทอร์เน็ต > (ชื่อเครือข่ายที่อุปกรณ์เชื่อมต่อ) ซึ่งจะแสดงรายละเอียดและ IP ของอุปกรณ์ในเครือข่ายทางด้านขวา
  2. ใช้ที่อยู่ IP ของอุปกรณ์เพื่อเชื่อมต่อผ่าน ADB โดยใช้เครื่องชําระเงิน
$ adb connect <device_ip_address>:5555
  1. จากหน้าต่างเทอร์มินัล ให้ไปที่โฟลเดอร์ระดับบนสุดสําหรับตัวอย่าง Codelab ที่คุณดาวน์โหลดในตอนต้นของ Codelab เช่น
$ cd Desktop/chrome_codelab_src
  1. ติดตั้งไฟล์ .apk ในโฟลเดอร์นี้ไปยัง Android TV โดยเรียกใช้:
$ adb -s <device_ip_address>:5555 install android-tv-app.apk
  1. ตอนนี้คุณควรจะเห็นแอปตามชื่อแคสต์วิดีโอในเมนูแอปของคุณในอุปกรณ์ Android TV
  2. เรียกใช้รหัสผู้ส่งบนเว็บที่อัปเดตแล้วและสร้างเซสชันการแคสต์ด้วยอุปกรณ์ Android TV โดยใช้ไอคอนแคสต์ หรือเลือก Cast.. จากเมนูแบบเลื่อนลงในเบราว์เซอร์ Chrome ตอนนี้คุณควรเปิดแอป Android TV บน Android Receiver และให้คุณควบคุมการเล่นโดยใช้รีโมต Android TV ได้แล้ว

8. ขอแสดงความยินดี

ตอนนี้คุณดูวิธีแคสต์แอปวิดีโอโดยใช้วิดเจ็ต Cast SDK ในแอปเว็บ Chrome

โปรดดูรายละเอียดเพิ่มเติมในคู่มือนักพัฒนาซอฟต์แวร์ Web Sender