你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

管理音频筛选器

重要

本文中所述的功能目前以公共预览版提供。 此预览版在提供时没有附带服务级别协议,我们不建议将其用于生产工作负荷。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款

了解如何使用 Azure 通信服务 SDK 管理音频处理功能。 了解在通话之前和通话期间如何使用音频筛选器来应用不同的音频功能。

目前,有五个不同的筛选器可供控制。

模拟自动增益控制

模拟自动增益控制是在通话前可用的筛选器。 默认情况下会启用此筛选器。

数字自动增益控制

数字自动增益控制是在通话前可用的筛选器。 默认情况下会启用此筛选器。

音乐模式

音乐模式是在通话前和通话期间可用的筛选器。 在此处详细了解音乐模式。 请注意,音乐模式仅适用于原生平台上的 1:1 通话和群组通话。 目前,音乐模式不适用于原生平台与 Web 之间的 1:1 通话。 默认情况下会禁用音乐模式。

回声消除

回声消除是在通话前和通话期间可用的筛选器。 仅当启用了音乐模式时,才能切换回声消除。 默认情况下会启用此筛选器。

噪声抑制

噪声抑制是在通话前和通话期间可用的筛选器。 当前可用的模式为 OffAutoLowHigh。 默认情况下,此功能设置为 High 模式。

先决条件

安装 SDK

找到项目级别的 build.gradle 文件,并将 mavenCentral() 添加到 buildscriptallprojects 下的存储库列表中

buildscript {
    repositories {
    ...
        mavenCentral()
    ...
    }
}
allprojects {
    repositories {
    ...
        mavenCentral()
    ...
    }
}

然后,在模块级别的 build.gradle 文件中,将以下行添加到 dependencies 部分

dependencies {
    ...
    implementation 'com.azure.android:azure-communication-calling:1.0.0'
    ...
}

初始化所需的对象

若要创建 CallAgent 实例,必须对 CallClient 实例调用 createCallAgent 方法。 此调用将异步返回 CallAgent 实例对象。

createCallAgent 方法采用 CommunicationUserCredential 作为参数来封装访问令牌

若要访问 DeviceManager,必须先创建 callAgent 实例。 然后,可以使用 CallClient.getDeviceManager 方法获取 DeviceManager

String userToken = '<user token>';
CallClient callClient = new CallClient();
CommunicationTokenCredential tokenCredential = new CommunicationTokenCredential(userToken);
android.content.Context appContext = this.getApplicationContext(); // From within an activity, for instance
CallAgent callAgent = callClient.createCallAgent(appContext, tokenCredential).get();
DeviceManager deviceManager = callClient.getDeviceManager(appContext).get();

若要为主叫方设置显示名称,请使用以下替代方法:

String userToken = '<user token>';
CallClient callClient = new CallClient();
CommunicationTokenCredential tokenCredential = new CommunicationTokenCredential(userToken);
android.content.Context appContext = this.getApplicationContext(); // From within an activity, for instance
CallAgentOptions callAgentOptions = new CallAgentOptions();
callAgentOptions.setDisplayName("Alice Bob");
DeviceManager deviceManager = callClient.getDeviceManager(appContext).get();
CallAgent callAgent = callClient.createCallAgent(appContext, tokenCredential, callAgentOptions).get();

音频筛选器功能允许将不同的音频预处理应用于传出音频。 有两种类型的音频筛选器:OutgoingAudioFiltersLiveOutgoingAudioFilters。 在通话开始前使用 OutgoingAudioFilters 更改设置,在通话进行中使用 LiveOutgoingAudioFilters 更改设置。

你首先需要导入通话 SDK 和关联的类:

import com.azure.android.communication.calling.OutgoingAudioOptions;
import com.azure.android.communication.calling.OutgoingAudioFilters;
import com.azure.android.communication.calling.LiveOutgoingAudioFilters;

通话开始前

OutgoingAudioFilters 可以在通话开始时应用。

首先创建一个 OutgoingAudioFilters 并将其传递给 OutgoingAudioOptions,如以下代码所示:

OutgoingAudioOptions outgoingAudioOptions = new OutgoingAudioOptions();
OutgoingAudioFilters filters = new OutgoingAudioFilters();
filters.setNoiseSuppressionMode(NoiseSuppressionMode.HIGH);
filters.setAnalogAutomaticGainControlEnabled(true);
filters.setDigitalAutomaticGainControlEnabled(true);
filters.setMusicModeEnabled(true);
filters.setAcousticEchoCancellationEnabled(true); 
outgoingAudioOptions.setAudioFilters(filters);

通话过程中

在通话开始后,你可以应用 LiveOutgoingAudioFilters。在通话开始后,你可以从通话对象中检索此对象。 若要更改 LiveOutgoingAudioFilters 中的设置,请将类中的成员设置为有效值,然后便会应用它们。

在通话进行期间,OutgoingAudioFilters 提供的筛选器只有一部分可用:音乐模式、回声消除和噪声抑制模式。

LiveOutgoingAudioFilters filters = call.getLiveOutgoingAudioFilters();
filters.setMusicModeEnabled(false);
filters.setAcousticEchoCancellationEnabled(false);
filters.setNoiseSuppressionMode(NoiseSuppressionMode.HIGH);

设置系统

创建 Xcode 项目

在 Xcode 中,创建新的 iOS 项目,并选择“单视图应用”模板。 本快速入门使用 SwiftUI 框架,因此应将“语言”设置为“Swift”,并将“接口”设置为“SwiftUI”

在此快速入门过程中,无需创建测试。 请随意清除“包括测试”复选框

显示用于在 Xcode 中创建项目的窗口的屏幕截图。

使用 CocoaPods 安装包和依赖项

  1. 为应用程序创建 Podfile,如此示例所示:

    platform :ios, '13.0'
    use_frameworks!
    target 'AzureCommunicationCallingSample' do
        pod 'AzureCommunicationCalling', '~> 1.0.0'
    end
    
  2. 运行 pod install

  3. 使用 Xcode 打开 .xcworkspace

请求访问麦克风

若要访问设备的麦克风,需要使用 NSMicrophoneUsageDescription 更新应用的信息属性列表。 将关联的值设置为将要包含在系统用于向用户请求访问权限的对话框中的字符串。

右键单击项目树的 Info.plist 条目,然后选择“打开为...”>“源代码”。 将以下代码行添加到顶层 <dict> 节,然后保存文件。

<key>NSMicrophoneUsageDescription</key>
<string>Need microphone access for VOIP calling.</string>

设置应用框架

打开项目的 ContentView.swift 文件。 将 import 声明添加到文件顶部以导入 AzureCommunicationCalling 库。 此外,导入 AVFoundation。 你将需要用它来处理代码中的音频权限请求。

import AzureCommunicationCalling
import AVFoundation

初始化 CallAgent

若要从 CallClient 创建 CallAgent 实例,必须使用 callClient.createCallAgent 方法,该方法在初始化后异步返回 CallAgent 对象。

若要创建通话客户端,请传递 CommunicationTokenCredential 对象:

import AzureCommunication

let tokenString = "token_string"
var userCredential: CommunicationTokenCredential?
do {
    let options = CommunicationTokenRefreshOptions(initialToken: token, refreshProactively: true, tokenRefresher: self.fetchTokenSync)
    userCredential = try CommunicationTokenCredential(withOptions: options)
} catch {
    updates("Couldn't created Credential object", false)
    initializationDispatchGroup!.leave()
    return
}

// tokenProvider needs to be implemented by Contoso, which fetches a new token
public func fetchTokenSync(then onCompletion: TokenRefreshOnCompletion) {
    let newToken = self.tokenProvider!.fetchNewToken()
    onCompletion(newToken, nil)
}

将创建的 CommunicationTokenCredential 对象传递给 CallClient 并设置显示名称:

self.callClient = CallClient()
let callAgentOptions = CallAgentOptions()
options.displayName = " iOS Azure Communication Services User"

self.callClient!.createCallAgent(userCredential: userCredential!,
    options: callAgentOptions) { (callAgent, error) in
        if error == nil {
            print("Create agent succeeded")
            self.callAgent = callAgent
        } else {
            print("Create agent failed")
        }
})

音频筛选器功能允许将不同的音频预处理应用于传出音频。 有两种类型的音频筛选器:OutgoingAudioFiltersLiveOutgoingAudioFilters。 在通话开始前使用 OutgoingAudioFilters 更改设置,在通话进行中使用 LiveOutgoingAudioFilters 更改设置。

你首先需要导入通话 SDK:

import AzureCommunicationCalling

通话开始前

OutgoingAudioFilters 可以在通话开始时应用。

首先创建一个 OutgoingAudioFilters 并将其传递给 OutgoingAudioOptions,如以下代码所示:

let outgoingAudioOptions = OutgoingAudioOptions()
let filters = OutgoingAudioFilters()
filters.NoiseSuppressionMode = NoiseSuppressionMode.high
filters.analogAutomaticGainControlEnabled = true
filters.digitalAutomaticGainControlEnabled = true
filters.musicModeEnabled = true
filters.acousticEchoCancellationEnabled = true
outgoingAudioOptions.audioFilters = filters

通话过程中

在通话开始后,你可以应用 LiveOutgoingAudioFilters。在通话开始后,你可以从通话对象中检索此对象。 若要更改 LiveOutgoingAudioFilters 中的设置,请将类中的成员设置为有效值,然后便会应用它们。

在通话进行期间,OutgoingAudioFilters 提供的筛选器只有一部分可用:音乐模式、回声消除和噪声抑制模式。

LiveOutgoingAudioFilters filters = call.liveOutgoingAudioFilters
filters.musicModeEnabled = true
filters.acousticEchoCancellationEnabled = true
filters.NoiseSuppressionMode = NoiseSuppressionMode.high

设置系统

创建 Visual Studio 项目

对于 UWP 应用,请在 Visual Studio 2022 中创建新的“空白应用(通用 Windows)”项目。 输入项目名称后,可随意选择任何版本高于 10.0.17763.0 的 Windows SDK。

对于 WinUI 3 应用,请使用“已打包空白应用(桌面中的 WinUI 3)”模板创建新项目,以设置单页 WinUI 3 应用。 需要 Windows App SDK 版本 1.3 或更高版本。

使用 NuGet 包管理器安装包和依赖项

可通过 NuGet 包公开提供通话 SDK API 和库。

以下步骤举例说明了如何查找、下载和安装通话 SDK NuGet 包:

  1. 选择“工具”>“NuGet 包管理器”>“管理解决方案的 NuGet 包”,以打开 NuGet 包管理器
  2. 选择“浏览”,然后在搜索框中输入 Azure.Communication.Calling.WindowsClient
  3. 确保已选中“包括预发行版”复选框
  4. 选择 Azure.Communication.Calling.WindowsClient 包,然后选择 Azure.Communication.Calling.WindowsClient1.4.0-beta.1 或更高版本。
  5. 在右侧选项卡上选中与 Azure 通信服务项目对应的复选框。
  6. 选择“安装”按钮。

音频筛选器功能允许将不同的音频预处理应用于传出音频。 有两种类型的音频筛选器:OutgoingAudioFiltersLiveOutgoingAudioFilters。 在通话开始前使用 OutgoingAudioFilters 更改设置,在通话进行中使用 LiveOutgoingAudioFilters 更改设置。

你首先需要导入通话 SDK:

using Azure.Communication;
using Azure.Communication.Calling.WindowsClient;

通话开始前

OutgoingAudioFilters 可以在通话开始时应用。

首先创建一个 OutgoingAudioFilters 并将其传递给 OutgoingAudioOptions,如以下代码所示:

var outgoingAudioOptions = new OutgoingAudioOptions();
var filters = new OutgoingAudioFilters()
{
    AnalogAutomaticGainControlEnabled = true,
    DigitalAutomaticGainControlEnabled = true,
    MusicModeEnabled = true,
    AcousticEchoCancellationEnabled = true,
    NoiseSuppressionMode = NoiseSuppressionMode.High
};
outgoingAudioOptions.Filters = filters;

通话过程中

在通话开始后,你可以应用 LiveOutgoingAudioFilters。在通话开始后,你可以从通话对象中检索此对象。 若要更改 LiveOutgoingAudioFilters 中的设置,请将类中的成员设置为有效值,然后便会应用它们。

在通话进行期间,OutgoingAudioFilters 提供的筛选器只有一部分可用:音乐模式、回声消除和噪声抑制模式。

LiveOutgoingAudioFilters filter = call.LiveOutgoingAudioFilters;
filter.MusicModeEnabled = true;
filter.AcousticEchoCancellationEnabled = true;
filter.NoiseSuppressionMode = NoiseSuppressionMode.Auto;

后续步骤