:::tip
本章使用 `@realsee/live@0.7.6` 提供的 `BrowserRTC` 实现浏览器语音。原生 App、WebView 和小程序应使用其他 RTC 实现，详见 [RTC 与运行环境](https://open.realsee.cn/docs/live/rtc-and-environment)。
:::

## 安装依赖

浏览器语音使用 Live 软件包的 TRTC 实现及其 peer dependency：

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

## 初始化 RTC

在创建 Live 实例的文件中初始化 `BrowserRTC`：

```tsx
import { BrowserRTC, createLiveReact } from '@realsee/live'

const getVoiceSign = async (params: {
  voiceId: string
  roomId: string
  userId: string
}) => {
  const response = await fetch('/api/live/voice-sign', {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify(params),
  })

  const result = await response.json()

  return {
    sdkAppId: Number(result.voice_app_id),
    userId: result.user_identifier,
    userSig: result.sign,
  }
}

const rtc = new BrowserRTC({ getVoiceSign })

const LiveReact = createLiveReact({
  __debug__: true,
  rtc,
})

export default LiveReact
```

`getVoiceSign` 的参数由 SDK 自动传入。业务后端负责请求或生成语音签名，浏览器中不得包含如视应用密钥或 RTC 服务商签名凭证。

原生 App 或 WebView 容器请使用 `VRWebViewRTC`；其他环境可以提供满足 `RTCProtocol` 的自定义对象。这两个类型都从软件包根路径导出。

## 监听语音事件

监听成功加入语音房间：

```tsx
import LiveReact from './LiveReact'

const { useRTCEventCallback } = LiveReact

useRTCEventCallback('joined', () => {
  console.log('已经加入语音房间')
})
```

监听语音错误：

```tsx
useRTCEventCallback('error', (error) => {
  console.error('RTC 错误：', error.message)
})
```

弱网环境可以监听 `weakNetwork`，并在不影响 WebSocket 同屏的前提下降级语音控制。

网页版本：https://open.realsee.cn/docs/live/function-expansion/rtc/browser
文档索引：https://open.realsee.cn/docs.md
