实时语音通话能够拉近人与人之间的距离,为用户提供沉浸式的交流体验,帮助你的 app 提高用户黏性。
本文介绍如何通过少量代码集成 Agora 音频 SDK 并调用 API,在你的应用中实现高质量、低延迟的语音通话功能。
下图展示在 app 中实现声网语音通话的基本工作流程:
实现语音通话的步骤如下:
1. 设置角色
将用户角色均设置为主播。
2. 加入频道
调用 joinChannel
创建并加入频道。在 App ID 一致的前提下,传入相同频道名的用户会进入同一个频道。
3、4. 在频道内发布和订阅音频
加入频道后,两位主播可以发布音频并互相订阅。
如果你的目标平台为 iOS,你的开发环境需要满足以下需求:
如果你的目标平台为 Android,你的开发环境需要满足以下需求:
如果你的目标平台为 macOS,你的开发环境需要满足以下需求:
如果你的目标平台为 Windows,你的开发环境需要满足以下需求:
更多环境要求细节,详见 Install Flutter。
flutter doctor
命令检查开发环境和运行环境是否满足要求。一个有效的声网开发者账号。
参考以下步骤创建一个声网项目:
声网会给每个项目自动分配一个 App ID 作为项目唯一标识。
在声网控制台的项目管理页面,找到你的项目,点击 App ID 右侧的 图标,即可获取项目的 App ID。
为提高项目的安全性,声网推荐使用 Token 对即将加入频道的用户进行鉴权。
为了方便测试,声网控制台提供生成 RTC 临时 Token 的功能。参考以下步骤获取 RTC 临时 Token:
testroom
,然后点击生成。后续调用 API 加入频道时,请确保填入的频道名与这里的频道名一致。本文使用 Visual Studio Code 创建 Flutter 项目。你需要在 Visual Studio Code 中安装 Flutter plugin。关于详细设置可以参考 Set up an editor。
在 pubspec.yaml
文件中添加以下依赖项:
agora_rtc_engine
依赖项,集成声网 Flutter SDK。关于 agora_rtc_engine
的最新版本可以查询 https://pub.dev/packages/agora_rtc_engine。permission_handler
依赖项,安装权限处理插件。environment:
sdk: ">=2.12.0 <3.0.0"
# 依赖项
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3
# 声网 Flutter SDK 依赖项,请使用最新版本的 agora_rtc_engine
agora_rtc_engine: ^6.0.0
# 权限处理插件依赖项
permission_handler: ^8.3.0
打开 main.dart
,删除 void main() => runApp(MyApp());
语句下方的全部代码。并按照步骤增加以下代码:
void main() => runApp(MyApp());
,你需要将其替换为 void main() => runApp(MyApp());
并删除下方的全部代码。导入以下 package:
import 'dart:async';
import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
输入你获得的 App ID 和临时 Token。
// 定义 App ID、Token 和 Channel
const appId = "<-- Insert App Id -->";
const token = "<-- Insert Token -->";
const channel = "<-- Insert Channel Name -->";
定义 MyApp 应用类:
// 应用类
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
定义应用状态类,包含以下设置:
class _MyAppState extends State<MyApp> {
int? _remoteUid;
bool _localUserJoined = false;
late RtcEngine _engine;
@override
void initState() {
super.initState();
initAgora();
}
// 初始化应用
Future<void> initAgora() async {
// 获取权限
await [Permission.microphone].request();
// 创建 RtcEngine
_engine = await createAgoraRtcEngine();
// 初始化 RtcEngine,设置频道场景为直播场景
await _engine.initialize(const RtcEngineContext(
appId: appId,
channelProfile: ChannelProfileType.channelProfileLiveBroadcasting,
));
_engine.registerEventHandler(
RtcEngineEventHandler(
onJoinChannelSuccess: (RtcConnection connection, int elapsed) {
debugPrint("local user ${connection.localUid} joined");
setState(() {
_localUserJoined = true;
});
},
onUserJoined: (RtcConnection connection, int remoteUid, int elapsed) {
debugPrint("remote user $remoteUid joined");
setState(() {
_remoteUid = remoteUid;
});
},
onUserOffline: (RtcConnection connection, int remoteUid,
UserOfflineReasonType reason) {
debugPrint("remote user $remoteUid left channel");
setState(() {
_remoteUid = null;
});
},
),
);
// 加入频道,设置用户角色为主播
await _engine.joinChannel(
token: token,
channelId: channel,
options: const ChannelMediaOptions(
clientRoleType: ClientRoleType.clientRoleBroadcaster),
uid: 0,
);
}
// 构建 UI
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Agora Voice Call',
home: Scaffold(
appBar: AppBar(
title: Text('Agora Voice Call'),
),
body: Center(
child: Text('Have a voice call!'),
),
),
);
}
}
在项目根目录运行以下命令安装依赖项。
flutter packages get
在 Visual Studio Code 右下角状态栏选择需要运行项目的设备。运行以下命令启动示例项目。
flutter run
按照以下步骤测试你的 app:
本节提供了额外的信息供参考。
声网在 GitHub 上提供了一个开源的互动直播示例项目供你参考。