在客戶端上顯示通話轉譯狀態
注意
通話轉譯狀態僅適用於Teams會議。 目前不支援呼叫轉譯狀態,Azure 通訊服務 Azure 通訊服務 通話。
使用通話轉譯時,您可能想要讓使用者知道正在轉譯通話。 方法如下。
必要條件
- 具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶。
- 已部署的通訊服務資源。 建立通訊服務資源。
- 使用者存取令牌,可啟用呼叫用戶端。 如需詳細資訊,請參閱 建立和管理存取令牌。
- 選擇性:完成快速入門以 將語音通話新增至您的應用程式
安裝 SDK
找出您的專案層級 build.gradle 檔案,並將 新增mavenCentral()
至 和 allprojects
下的buildscript
存放庫清單:
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();
警告
直到 1.1.0 版和 beta 版本 1.1.0-beta.1 的 Azure 通訊服務 呼叫 Android SDK 為止isTranscriptionActive
,且addOnIsTranscriptionActiveChangedListener
屬於 Call
物件。 針對新的 Beta 版本,這些 API 已移轉為 的擴充功能 Call
,就像下面所述一樣。
呼叫轉譯是核心 Call
對象的擴充功能。 您必須先取得轉譯功能物件:
TranscriptionCallFeature callTranscriptionFeature = call.feature(Features.TRANSCRIPTION);
然後,若要檢查呼叫是否正在轉譯,請檢查 isTranscriptionActive
的 callTranscriptionFeature
屬性。 它會傳回 boolean
。
boolean isTranscriptionActive = callTranscriptionFeature.isTranscriptionActive();
您也可以訂閱轉譯中的變更:
private void handleCallOnIsTranscriptionChanged(PropertyChangedEvent args) {
boolean isTranscriptionActive = callTranscriptionFeature.isTranscriptionActive();
}
callTranscriptionFeature.addOnIsTranscriptionActiveChangedListener(handleCallOnIsTranscriptionChanged);
設定系統
建立 Xcode 專案
在 Xcode 中,建立新的 iOS 專案,然後選取 [單一檢視應用程式 ] 範本。 本快速入門使用 SwiftUI 架構,因此您應該將 Language 設定為 Swift,並將 Interface 設定為 SwiftUI。
在本快速入門期間,您不會建立測試。 您可以隨意清除 [ 包含測試 ] 複選框。
使用 CocoaPods 安裝套件和相依性
為您的應用程式建立 Podfile,如下列範例所示:
platform :ios, '13.0' use_frameworks! target 'AzureCommunicationCallingSample' do pod 'AzureCommunicationCalling', '~> 1.0.0' end
執行
pod install
。使用 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
若要從 建立 CallAgent
實例,您必須使用callClient.createCallAgent
方法,以異步方式在物件初始化之後傳回CallAgent
物件。CallClient
若要建立呼叫用戶端,請傳遞 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")
}
})
警告
在1.1.0版和beta版1.1.1.0-beta.1之前,呼叫iOS SDK的 Azure 通訊服務具有 isTranscriptionActive
作為物件的一部分,Call
且didChangeTranscriptionState
屬於委派的CallDelegate
一部分。 針對新的 Beta 版本,這些 API 已移轉為 的擴充功能 Call
,就像下面所述一樣。
呼叫轉譯是核心 Call
對象的擴充功能。 您必須先取得轉譯功能物件:
let callTranscriptionFeature = call.feature(Features.transcription)
然後,若要檢查呼叫是否已轉譯,請檢查 isTranscriptionActive
的 callTranscriptionFeature
屬性。 它會傳回 Bool
。
let isTranscriptionActive = callTranscriptionFeature.isTranscriptionActive;
您也可以使用 事件didChangeTranscriptionState
在 類別上實TranscriptionCallFeatureDelegate
作委派,以訂閱轉譯變更:
callTranscriptionFeature.delegate = self
// didChangeTranscriptionState is a member of TranscriptionCallFeatureDelegate
public func transcriptionCallFeature(_ transcriptionCallFeature: TranscriptionCallFeature, didChangeTranscriptionState args: PropertyChangedEventArgs) {
let isTranscriptionActive = callTranscriptionFeature.isTranscriptionActive
}
設定系統
建立 Visual Studio 專案
針對 UWP 應用程式,在 Visual Studio 2022 中建立新的 空白應用程式 (通用 Windows) 專案。 輸入專案名稱之後,您可以選擇 10.0.17763.0 之後的任何 Windows SDK。
針對 WinUI 3 應用程式,使用 [空白應用程式]、[傳統型] 範本建立新的專案,以設定單頁 WinUI 3 應用程式。 需要 Windows 應用程式 SDK 1.3 版或更新版本。
使用 NuGet 封裝管理員 安裝套件和相依性
呼叫 SDK API 和連結庫可透過 NuGet 套件公開使用。
下列步驟示範如何尋找、下載及安裝呼叫 SDK NuGet 套件:
- 選取 [工具>][NuGet 封裝管理員 管理方案的 NuGet 套件],以開啟 NuGet 封裝管理員。>
- 選取 [ 瀏覽],然後在搜尋方塊中輸入
Azure.Communication.Calling.WindowsClient
。 - 請確定已選取 [ 包含發行前版本 ] 複選框。
- 選取套件
Azure.Communication.Calling.WindowsClient
,然後選取Azure.Communication.Calling.WindowsClient
1.4.0-beta.1 或更新版本。 - 選取對應至右側索引標籤上 [通訊服務] 項目的複選框。
- 選取 [安裝] 按鈕。
呼叫轉譯是核心 Call
對象的擴充功能。 您必須先取得轉譯功能物件:
TranscriptionCallFeature transcriptionFeature = call.Features.Transcription;
然後,若要檢查呼叫是否正在轉譯,請檢查 IsTranscriptionActive
的 transcriptionFeature
屬性。 它會傳回 boolean
。
boolean isTranscriptionActive = transcriptionFeature.isTranscriptionActive;
您也可以訂閱轉譯中的變更:
private async void Call__OnIsTranscriptionActiveChanged(object sender, PropertyChangedEventArgs args)
boolean isTranscriptionActive = transcriptionFeature.IsTranscriptionActive();
}
transcriptionFeature.IsTranscriptionActiveChanged += Call__OnIsTranscriptionActiveChanged;