本文介绍如何在你的项目中集成和使用微软实时语音转写&翻译(多语种)插件(以下简称“微软转写及翻译插件”),包括 Android 和 iOS 平台。
微软实时语音转写&翻译插件是对微软语音 SDK 核心 API 的封装。通过声网 SDK v4.x 提供的 setExtensionProperty 或 setExtensionPropertyWithVendor 方法,传入指定的 key
和 value
参数,你可以快速集成微软的语音转写能力。
以 setExtensionProperty
为例,其中的 key
参数与微软的 API 名称完全对应,value
参数以 JSON 格式包装该 API 的部分或全部参数。因此,调用 setExtensionProperty
时只要传入指定的 key
和 value
,就可以调用对应的微软 API,实现语音转写的有关功能。setExtensionPropertyWithVendor
同理。
微软实时语音转写&翻译插件需要与声网视频 SDK v4.x 搭配使用。参考以下文档集成视频 SDK v4.x 并实现基础的视频通话:
具体步骤参考免费试用语音服务。后续初始化插件时,需要传入你的密钥和区域。
参考如下步骤在你的项目中集成微软转写及翻译插件:
Android
.aar
文件保存到项目文件夹的 /app/libs
路径。app/build.gradle
文件,在 dependencies
中添加如下行:implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])
iOS
.framework
库文件保存到你的项目文件夹下。以如下项目结构为例,你可以把库文件保存到 <ProjectName>
路径下。
.
├── <ProjectName>
├── <ProjectName>.xcodeproj
本节介绍插件相关接口的调用流程。接口的参数解释详见接口说明。
初始化声网 RtcEngine
时,调用 enableExtension
开启插件。
RtcEngineConfig config = new RtcEngineConfig();
// 监听插件事件,用于接收 onEvent 回调
config.mExtensionObserver = this;
config.addExtension("AgoraMicrosoftExtension");
mRtcEngine = RtcEngine.create(config);
// 开启插件
mRtcEngine.enableExtension("Microsoft", "Speech_Recognition", enabled);
iOS
初始化声网 AgoraRtcEngine
时,调用 enableExtensionWithVendor
启用插件。
AgoraRtcEngineConfig *config = [AgoraRtcEngineConfig new];
// 监听插件事件,用于接收 onEvent 回调
config.eventDelegate = self;
self.agoraKit = [AgoraRtcEngineKit sharedEngineWithConfig:config
delegate:self];
// 开启插件
[self.agoraKit enableExtensionWithVendor:@"Microsoft"
extension:@"Speech_Recognition"
enabled:self.enable];
调用 setExtensionProperty
(Android)或 setExtensionPropertyWithVendor
(iOS),指定 key 为 init_speech_recognition
并在 value 中传入 subscription
和 region
等参数。
Android
// 方便后续多次调用 setExtensionProperty
private void setExtensionProperty(String key, String property) {
mRtcEngine.setExtensionProperty("Microsoft", "Speech_Recognition", key, property);
}
// 初始化语音转写
try {
JSONObject jsonObject = new JSONObject();
// 传入 Azure 资源密钥
jsonObject.put("subscription", "<paste-your-speech-key-here>");
// 传入 Azure 资源所在区域
jsonObject.put("region", "<paste-your-speech-location/region-here>");
JSONArray jsonArray = new JSONArray();
// 指定源语言
jsonArray.put("en-US");
jsonArray.put("zh-CN");
jsonObject.put("source_languages", jsonArray);
// 开启语种自动检测
jsonObject.put("enable_auto_detect", true);
setExtensionProperty("init_speech_recognition", jsonObject.toString());
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
iOS
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
// 传入 Azure 资源密钥
@"subscription": subscription,
// 传入 Azure 资源所在区域
@"region": region,
// 指定源语言
@"source_languages": @[
@"zh-CN", @"en-US"
],
// 开启语种自动检测
@"enable_auto_detect": true
}
options:NSJSONWritingPrettyPrinted
error:&error];
[self.agoraKit
setExtensionPropertyWithVendor:@"Microsoft"
extension:@"Speech_Recognition"
key:@"init_speech_recognition"
value:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
调用 setExtensionProperty
(Android)或 setExtensionPropertyWithVendor
(iOS),指定 key 为 init_translate_recognition
并在 value 中传入 subscription
等参数。
Android
// 方便后续多次调用 setExtensionProperty
private void setExtensionProperty(String key, String property) {
mRtcEngine.setExtensionProperty("Microsoft", "Speech_Recognition", key, property);
}
// 初始化语音翻译
try {
JSONObject jsonObject = new JSONObject();
// 传入 Azure 资源密钥
jsonObject.put("subscription", "<paste-your-speech-key-here>");
JSONArray jsonArray = new JSONArray();
// 指定源语言
jsonArray.put("en-US");
jsonArray.put("zh-CN");
jsonObject.put("source_languages", jsonArray);
// 指定目标语言
jsonArray.put("fr");
jsonArray.put("ja");
jsonObject.put("target_languages", jsonArray);
// 开启语种自动检测
jsonObject.put("enable_auto_detect", true);
setExtensionProperty("init_speech_recognition", jsonObject.toString());
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
iOS
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
// 传入 Azure 资源密钥
@"subscription": subscription,
// 指定源语言
@"source_languages": @[
@"zh-CN", @"en-US"
],
// 指定目标语言
@"target_languages": @[
@"fr", @"ja"
],
// 开启语种自动检测
@"enable_auto_detect": true
}
options:NSJSONWritingPrettyPrinted
error:&error];
[self.agoraKit
setExtensionPropertyWithVendor:@"Microsoft"
extension:@"Speech_Recognition"
key:@"init_speech_recognition"
value:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
调用 setExtensionProperty
(Android)或 setExtensionPropertyWithVendor
(iOS),指定 key 为 start_continuous_recognition_async
。
Android
setExtensionProperty("start_continuous_recognition_async", "{}");
iOS
[self.agoraKit setExtensionPropertyWithVendor:@"Microsoft"
extension:@"Speech_Recognition"
key:@"start_continuous_recognition_async"
value:@"{}"];
成功开始识别后,微软转写及翻译插件会通过 onEvent
回调返回识别结果。识别结果的含义详见 onEvent 回调。
调用 setExtensionProperty
(Android)或 setExtensionPropertyWithVendor
(iOS),指定 key 为 start_continuous_translate_async
。
Android
setExtensionProperty("start_continuous_translate_async", "{}");
iOS
[self.agoraKit setExtensionPropertyWithVendor:@"Microsoft"
extension:@"Speech_Recognition"
key:@"start_continuous_translate_async"
value:@"{}"];
成功开始识别后,微软转写及翻译插件会通过 onEvent
回调返回识别结果。识别结果的含义详见 onEvent 回调。
需要结束转写时,调用 setExtensionProperty
(Android)或 setExtensionPropertyWithVendor
(iOS)方法并指定 key 为 stop_continuous_recognition_async
、stop_continuous_translate_async
。
Android
setExtensionProperty("stop_continuous_recognition_async", "{}");
setExtensionProperty("stop_continuous_translate_async", "{}");
iOS
[self.agoraKit setExtensionPropertyWithVendor:@"Microsoft"
extension:@"Speech_Recognition"
key:@"stop_continuous_recognition_async"
value:@"{}"];
[self.agoraKit setExtensionPropertyWithVendor:@"Microsoft"
extension:@"Speech_Recognition"
key:@"stop_continuous_translate_async"
value:@"{}"];
在 GitHub 上查看完整的示例代码和项目结构:
平台 | 语言 | 示例项目 |
---|---|---|
Android | Java | Microsoft/android |
iOS | Objective-C | Microsoft/ios |
Android
克隆仓库:
git clone https://github.com/AgoraIO-Community/AgoraMarketPlace.git
从声网云市场下载页面下载微软转写及翻译插件的 Android 插件包。解压后,将所有 .aar
文件保存到 Microsoft/android/app/libs
路径。
在 Android Studio 中打开示例项目 Microsoft/android
。
将项目与 Gradle 文件同步。
打开 Microsoft/android/app/src/main/java/io/agora/rte/extension/Microsoft/example/Config.java
文件,进行如下修改:
<YOUR_APP_ID>
替换为你的 App ID。获取 App ID 请参考开始使用声网平台。<YOUR_SUBSCRIPTION>
和 <YOUR_REGION>
分别替换为你的 Azure 资源密钥和 Azure 资源所在区域。获取方式详见使用 Azure 门户创建认知服务资源。public interface Config {
String mAppId = "<YOUR_APP_ID>";
String mSubscription = "<YOUR_SUBSCRIPTION>";
String mRegion = "<YOUR_REGION>";
}
连接一台 Android 真机(非模拟器),运行项目。
iOS
克隆仓库:
git clone https://github.com/AgoraIO-Community/AgoraMarketPlace.git
从声网云市场下载页面下载微软转写及翻译插件的 iOS 插件包。解压后,将所有 .framework
库文件保存到 Microsoft/ios/ExtensionExample
。
在终端中进入 Microsoft/ios
目录,运行以下命令使用 CocoaPods 安装依赖:
pod install
在 Xcode 中打开项目 Microsoft/ios/ExtensionExample.xcworkspace
。
打开 Config.h
文件,进行如下修改:
<YOUR_APP_ID>
替换为你的 App ID。获取 App ID 请参考开始使用声网平台。<YOUR_SUBSCRIPTION>
和 <YOUR_REGION>
分别替换为你的 Azure 资源密钥和 Azure 资源所在区域。获取方式详见使用 Azure 门户创建认知服务资源。NSString *const appID = @"<YOUR_APP_ID>";
NSString *const subscription = @"<YOUR_SUBSCRIPTION>";
NSString *const region = @"<YOUR_REGION>";
连接一台 iOS 真机(非模拟器),运行项目。
运行成功后,示例项目会安装到你的 Android 或 iOS 设备上。
enableExtension
和 Start ASR
按钮enableExtension
开启插件。Start ASR
开始语音转写。此时 Start ASR
按钮会变成 Stop ASR
按钮。Stop ASR
结束语音转写。Start Translating
开始语音翻译。此时 Start Translating
按钮会变成 Stop Translating
按钮。Stop Translating
结束语音翻译。插件所有相关接口的参数解释详见接口说明。