本文へ移動
ドキュメント

ブラウザーの音声

組み込み BrowserRTC を使い、ブラウザー側で Live 音声を接続します。

Markdown を表示
このページの内容

ヒント

@realsee/live@0.7.6 の BrowserRTC で音声付き共同閲覧を有効にします。ネイティブアプリ・WebView・ミニプログラムは別の実装を使います。RTC と環境 を参照してください。

依存関係をインストールする

重要

ブラウザー音声は Live の TRTC 実装と宣言された peer dependency を使います。

npm install @realsee/live@0.7.6 @realsee/jsbridge-x@^0.2.6 trtc-js-sdk@^4.15.3

React のサンプル

RTC インスタンスを初期化する

共同閲覧インスタンスを作るファイルに RTC の初期化を追加します。

import { BrowserRTC, createLiveReact } from "@realsee/live";
import request from "../utils/request";

/*
 * Callback method for obtaining the voice signature: getVoiceSign
 * This method is invoked by the SDK internally with parameters; you only need to implement the method in this form.
 * [Special note]: The opts here is a formal parameter that the SDK injects automatically when it calls the voice signature method. Just write its reference as shown in the example; there is no need to inject the parameter yourself.
 * */
// highlight-start
const getVoiceSign = async (opts) => {
  // The opts here is a formal parameter that the SDK injects automatically when calling the voice signature method; the business side does not need to handle it.
  // highlight-end
  // Request your backend interface here. The request method below merely wraps fetch in a simple way, with no special handling.
  return await request("getRtcSign", {
    voice_id: opts.voiceId,
    room_id: opts.roomId,
    user_id: opts.userId,
  })
    .then((res) => {
      // Once you have the voice signature and other information returned by the backend, return it as required by the SDK.
      return {
        sdkAppId: Number(res.voice_app_id),
        userId: res.user_identifier,
        userSig: res.sign,
      };
    })
    .catch((error) => {
      throw Error(error.message);
    });
};

// Initialize the RTC instance
const rtcInstance = new BrowserRTC({ getVoiceSign });

const liveInstance = createLiveReact({
  __debug__: true,
  rtc: rtcInstance,
});

export default liveInstance;

ネイティブアプリや WebView は VRWebViewRTC、他環境は RTCProtocol を満たすオブジェクトを使います。いずれもパッケージルートから公開されています。

音声参加の成功を監視する

情報

共同閲覧インスタンスの useRTCEventCallback で音声参加成功を購読します。

import LiveReact from "./LiveReact";

const { useRTCEventCallback } = LiveReact;

useRTCEventCallback("joined", () => {
  console.log("rtc -- joined");
});

音声エラーを監視する

情報

共同閲覧インスタンスの useRTCEventCallback で音声エラーを購読します。

import LiveReact from "./LiveReact";

const { useRTCEventCallback } = LiveReact;

useRTCEventCallback("error", (error) => {
  console.log("rtc -- error: ", error.message);
});