# 实现纯语音互动 (/zh-CN/realtime-media/rtc/build/audio/audio-quick-start/web) > For AI agents: see the complete documentation index at [llms.txt](/llms.txt). 本文介绍如何集成声网实时互动 SDK,通过少量代码从 0 开始实现一个简单的纯语音互动 App,适用于语音通话场景。 ## 前提条件 [#前提条件-3] - Windows 或 macOS 计算机,需满足以下要求: - 下载满足声网 [Web SDK 兼容性的浏览器](/zh-CN/realtime-media/rtc/reference/browser-compatibility)。声网强烈推荐使用[最新稳定版 Google Chrome 浏览器](https://www.google.com/chrome/)。 - 具备物理音视频采集设备。 - 可连接到互联网。如果你的网络环境部署了防火墙,请参考[应对防火墙限制](/zh-CN/realtime-media/rtc/build/setup-and-access/firewall)以正常使用声网服务。 - 搭载 2.2 GHz Intel 第二代 i3/i5/i7 处理器或同等性能的其他处理器。 - 有效的声网账户和声网项目,请参考[开通 RTC 服务](/zh-CN/realtime-media/rtc/build/setup-and-access/enable-service),在声网控制台注册、登录并创建声网项目,然后获取项目的 App ID 和 临时 Token。 - 安装 [Node.js](https://nodejs.org/en/download/) 18 或更高版本。 :::info[信息] 有些框架模板可能需要更高版本的 Node.js 才能工作,如果你的包管理器发出警告,请升级你的 Node.js 版本。 ::: ## 创建 Web 项目 [#创建-web-项目] 创建一个名为 `agora_web_quickstart` 的文件夹。一个 Web 客户端项目至少需包含以下文件: - `index.html`: 用于设计 Web 应用的用户界面。 - `basicVoiceCall.js`: 通过 `AgoraRTCClient` 实现具体应用逻辑的代码。 - `package.json`: 安装并管理项目依赖。你可以通过命令行进入 `agora_web_quickstart` 目录并运行 `npm init` 命令来创建 `package.json` 文件。 ## 实现步骤 [#实现步骤-3] 下文介绍通过声网 RTC SDK 在你的 Web 应用中实现语音通话的详细步骤。 ### 1. 集成 SDK [#1-集成-sdk] 参考以下步骤通过 npm 将声网 Web SDK 集成到你的项目中: 1. 在 `package.json` 文件的 `dependencies` 字段中添加 `agora-rtc-sdk-ng` 及版本号: ```json { "name": "agora_web_quickstart", "version": "1.0.0", "description": "", "main": "basicVoiceCall.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "dependencies": { // highlight-start "agora-rtc-sdk-ng": "latest" // highlight-end }, "author": "", "license": "ISC" } ``` 2. 将以下代码复制到 `basicVoiceCall.js` 文件中,在你的项目中导入 `AgoraRTC` 模块。 ```javascript import AgoraRTC from "agora-rtc-sdk-ng" ``` ### 2. 实现用户界面 [#2-实现用户界面] 将以下代码复制到 `index.html` 实现客户端用户界面: ```html Web SDK Voice Quickstart

Web SDK Voice Quickstart

``` ### 3. 实现纯语音互动逻辑 [#3-实现纯语音互动逻辑] 参考以下步骤实现纯语音互动的逻辑: 1. 调用 [`createClient`](/zh-CN/api-reference/rtc/web/interfaces/iagorartc#createclient) 方法创建 `AgoraRTCClient` 对象。 2. 调用 [`join`](/zh-CN/api-reference/rtc/web/interfaces/iagorartcclient#join) 方法加入一个 RTC 频道,你需要在该方法中传入 App ID、用户 ID、Token 和频道名。 3. 先调用 [`createMicrophoneAudioTrack`](/zh-CN/api-reference/rtc/web/interfaces/iagorartc#createmicrophoneaudiotrack) 通过麦克风采集的音频创建本地音频轨道对象,然后调用 [`publish`](/zh-CN/api-reference/rtc/web/interfaces/iagorartcclient#publish) 方法,将这些本地音频轨道对象当作参数即可将音频发布到频道中。 4. 当一个远端用户加入频道并发布音频轨道时: 1. 监听 [client.on("user-published")](/zh-CN/api-reference/rtc/web/interfaces/iagorartcclient#event_user_published) 事件。当 SDK 触发该事件时,在这个事件回调函数的参数中你可以获取远端用户 `AgoraRTCRemoteUser` 对象 。 2. 调用 [`subscribe`](/zh-CN/api-reference/rtc/web/interfaces/iagorartcclient#subscribe) 方法订阅远端用户 `AgoraRTCRemoteUser` 对象,获取远端用户的远端音频轨道 `RemoteAudioTrack` 对象。 3. 调用 `play` 方法播放远端音频轨道。 下图展示了实现音频互动的 API 调用流程。注意图中的方法是对不同的对象调用的。 ![实现流程](https://web-cdn.agora.io/docs-portal/img/rtc/js-audio-quick-start.svg) 将以下代码复制到 `basicVoiceCall.js` 文件中,注意将 `appId` 和 `token` 参数替换为[你自己的 App ID 和临时 Token](#前提条件-3)。 ```javascript import AgoraRTC from "agora-rtc-sdk-ng"; let rtc = { localAudioTrack: null, client: null }; let options = { // Pass your App ID here. appId: "Your App ID", // Set the channel name. channel: "test", // Pass your temp token here. token: "Your temp token", // Set the user ID. uid: 123456, }; async function startBasicCall() { // Create an AgoraRTCClient object. rtc.client = AgoraRTC.createClient({mode: "rtc", codec: "vp8"}); // Listen for the "user-published" event, from which you can get an AgoraRTCRemoteUser object. rtc.client.on("user-published", async (user, mediaType) => { // Subscribe to the remote user when the SDK triggers the "user-published" event await rtc.client.subscribe(user, mediaType); console.log("subscribe success"); // If the remote user publishes an audio track. if (mediaType === "audio") { // Get the RemoteAudioTrack object in the AgoraRTCRemoteUser object. const remoteAudioTrack = user.audioTrack; // Play the remote audio track. remoteAudioTrack.play(); } }); // Listen for the "user-unpublished" event rtc.client.on("user-unpublished", async (user) => { // Unsubscribe from the tracks of the remote user. await rtc.client.unsubscribe(user); }); window.onload = function () { document.getElementById("join").onclick = async function () { // Join an RTC channel. await rtc.client.join(options.appId, options.channel, options.token, options.uid); // Create a local audio track from the audio sampled by a microphone. rtc.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack(); // Publish the local audio tracks to the RTC channel. await rtc.client.publish([rtc.localAudioTrack]); console.log("publish success!"); } document.getElementById("leave").onclick = async function () { // Destroy the local audio track. rtc.localAudioTrack.close(); // Leave the channel. await rtc.client.leave(); } } } startBasicCall(); ``` ## 调试 App [#调试-app-3] 本文使用 [webpack](https://webpack.js.org/) 打包项目,使用 `webpack-dev-server` 运行项目。参考以下步骤来测试你的 App: 1. 在 `package.json` 的 `dependencies` 中添加 `webpack-cli` 和 `webpack-dev-server` 字段中,在 `scripts` 字段中添加 `build` 和 `start:dev` 字段。 ```json { "name": "agora_web_quickstart", "version": "1.0.0", "description": "", "main": "basicVoiceCall.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", // highlight-start "build": "webpack --config webpack.config.js", "start:dev": "webpack serve --open --config webpack.config.js" // highlight-end }, "dependencies": { "agora-rtc-sdk-ng": "latest", // highlight-start "webpack": "5.28.0", "webpack-dev-server": "3.11.2", "webpack-cli": "4.10.0" // highlight-end }, "author": "", "license": "ISC" } ``` 2. 在项目目录中创建一个名为 `webpack.config.js` 的文件,将以下代码复制到 `webpack.config.js` 配置 webpack: ```javascript const path = require("path"); module.exports = { entry: "./basicVoiceCall.js", output: { filename: "bundle.js", path: path.resolve(__dirname, "./dist"), }, devServer: { compress: true, port: 9000, }, }; ``` 3. 运行下列命令安装依赖: ```shell npm install ``` 4. 运行下列命令通过 webpack 编译项目: ```shell # Use webpack to package the project npm run build ``` 5. 通过 webpack-dev-server 运行项目: ```shell npm run start:dev ``` 你的浏览器会自动打开以下页面: ![](https://web-cdn.agora.io/docs-files/1629367212930) 点击 **JOIN** 加入频道。你还可以邀请朋友克隆 [Gitee 示例项目](https://gitee.com/agoraio-community/API-Examples-Web) 或 [Github 示例项目](https://github.com/Shengwang-Lab/API-Examples-Web) 到本地。在浏览器中打开 `src/index.html` 文件,并输入相同的 App ID、频道名称和临时 Token。你的朋友加入频道后,你们可以听到彼此的声音。 :::info[信息] - 在本地服务器(localhost)运行 Web 应用仅作为测试用途。部署生产环境时,请确保使用 HTTPS 协议。 - 由于浏览器的安全策略对除 127.0.0.1 以外的 HTTP 地址作了限制,声网 Web SDK 仅支持 HTTPS 协议、 `http://localhost`(`http://127.0.0.1`) 。请勿使用 HTTP 协议在 `http://localhost`(`http://127.0.0.1`) 之外访问你的项目。 ::: ## 后续步骤 [#后续步骤-3] 为保障通信安全,在正式生产环境中,你需要在自己的 App 服务端生成 Token。详见[使用 Token 鉴权](/zh-CN/realtime-media/rtc/build/setup-and-access/token-authentication)。 ## 参考信息 [#参考信息-3] ### 示例项目 [#示例项目-3] 声网提供了开源的示例项目供你参考,你可以前往下载或查看其中的源代码。 示例项目} href="https://gitee.com/agoraio-community/API-Examples-Web/tree/main/src/example" /> 示例项目} href="https://github.com/Shengwang-Lab/API-Examples-Web/tree/main/src/example" /> ### 其它集成方式 [#其它集成方式] 使用 npm 集成 Web SDK 还支持开启 Tree shaking 来减小集成 SDK 后的 App 体积,详见[使用 Tree shaking](/zh-CN/realtime-media/rtc/build/optimize-and-operate/tree-shaking)。 除使用 npm 获取 Web SDK 之外,你还可以使用以下方法获取 SDK: - 在项目 HTML 文件中,添加如下代码,使用 CDN 方法获取 SDK: ```html ``` - 下载[声网 Web SDK 4.x 版本 SDK 包](/zh-CN/realtime-media/rtc/reference/downloads)至本地,将 SDK 包中的 `.js` 文件保存到项目文件所在的目录下,然后在项目 HTML 文件中添加如下代码: ```html ``` :::info[信息] 在以上方法中,SDK 都会在全局导出一个 `AgoraRTC` 对象,直接访问这个对象即可操作 SDK。 ::: ### 常见问题 [#常见问题-3] **为什么在本地运行快速开始项目时会报错 `digital envelope routines::unsupported`?** 本文中的快速开始项目通过 webpack 打包并在本地运行。由于 Node.js 16 及以上版本更改了对 OpenSSL 的依赖(详见 [node issue](https://github.com/nodejs/node/issues/29817)),影响了项目中本地开发环境的依赖(详见 [webpack issue](https://github.com/webpack/webpack/issues/14532)),运行项目会发生错误。解决方案如下: - (推荐)运行如下命令,设置临时的环境变量: ```shell export NODE_OPTIONS=--openssl-legacy-provider ``` - 暂时换用低版本的 Node.js。 然后再次尝试运行项目。