|
2 | 2 | title: Get started with Azure Communication Services closed caption on Android
|
3 | 3 | titleSuffix: An Azure Communication Services quickstart document
|
4 | 4 | description: Learn about the Azure Communication Services Closed Captions in Android apps
|
5 |
| -author: RinaRish |
6 |
| -manager: visho |
| 5 | +author: Kunaal |
7 | 6 | services: azure-communication-services
|
8 |
| - |
9 |
| -ms.author: ektrishi |
10 |
| -ms.date: 02/03/2022 |
| 7 | +ms.subservice: calling |
| 8 | +ms.author: valindrae |
| 9 | +ms.date: 04/15/2024 |
11 | 10 | ms.topic: include
|
12 | 11 | ms.service: azure-communication-services
|
13 | 12 | ---
|
14 | 13 |
|
15 | 14 | ## Prerequisites
|
| 15 | +- Azure account with an active subscription, for details see [Create an account for free.](https://azure.microsoft.com/free/) |
| 16 | +- Azure Communication Services resource. See [Create an Azure Communication Services resource](../../../../quickstarts/create-communication-resource.md?tabs=windows&pivots=platform-azp). Save the connection string for this resource. |
| 17 | +- An app with voice and video calling, refer to our [Voice](../../../../quickstarts/voice-video-calling/getting-started-with-calling.md) and [Video](../../../../quickstarts/voice-video-calling/get-started-with-video-calling.md) calling quickstarts. |
16 | 18 |
|
17 |
| -Refer to the [Voice Calling Quickstart](../../getting-started-with-calling.md?pivots=platform-android) to set up a sample app with voice calling. |
| 19 | +>[!NOTE] |
| 20 | +>Please note that you will need to have a voice calling app using Azure Communication Services calling SDKs to access the closed captions feature that is described in this guide. |
18 | 21 |
|
19 | 22 | ## Models
|
| 23 | +| Name | Description | |
| 24 | +| ---------------------------------- | ----------------------------------------------------------------- | |
| 25 | +| CaptionsCallFeature | API for captions call feature | |
| 26 | +| CommunicationCaptions | API for communication captions | |
| 27 | +| StartCaptionOptions | Closed caption options like spoken language | |
| 28 | +| CommunicationCaptionsListener | Listener for CommunicationCaptions addOnCaptionsReceivedListener | |
| 29 | +| CommunicationCaptionsReceivedEvent | Data object received for each CommunicationCaptionsListener event | |
| 30 | + |
| 31 | +## Get closed captions feature |
| 32 | +You need to get and cast the Captions object to utilize Captions specific features. |
| 33 | + |
| 34 | +``` java |
| 35 | +CaptionsCallFeature captionsCallFeature = call.feature(Features.CAPTIONS); |
| 36 | +captionsCallFeature.getCaptions().whenComplete( |
| 37 | + ((captions, throwable) -> { |
| 38 | + if (throwable == null) { |
| 39 | + CallCaptions callCaptions = captions; |
| 40 | + if (captions.getCaptionsType() == CaptionsType.COMMUNICATION_CAPTIONS) { |
| 41 | + // communication captions |
| 42 | + CommunicationCaptions communicationCaptions = (CommunicationCaptions) captions; |
| 43 | + } |
| 44 | + } else { |
| 45 | + // get captions failed |
| 46 | + // throwable is the exception/cause |
| 47 | + } |
| 48 | + })); |
| 49 | +``` |
20 | 50 |
|
21 |
| -| Name | Description | |
22 |
| -| - | - | |
23 |
| -| CaptionsCallFeature | Used to start and manage closed captions. | |
24 |
| -| StartCaptionsOptions | Used for representing options to start closed captions. | |
25 |
| -| CaptionsListener | Used to handle captions events. | |
26 |
| -| CaptionsInfo | Used to representing captions data. | |
| 51 | +## Subscribe to listeners |
27 | 52 |
|
28 |
| -## Methods |
29 |
| -### Start captions |
| 53 | +### Add a listener to receive captions enabled/disabled status |
30 | 54 |
|
31 |
| -1. Get the ongoing call object established during the prerequisite steps. |
32 |
| -2. Get the Captions Feature object |
33 |
| -```java |
34 |
| -CaptionsCallFeature captionsCallFeature = call.api(Features.CAPTIONS); |
35 |
| -``` |
36 |
| -3. Define the CaptionsListener |
37 |
| -```java |
38 |
| -CaptionsListener captionsListener = new CaptionsListener() { |
39 |
| - @Override |
40 |
| - public void onCaptions(CaptionsInfo captionsInfo) { |
41 |
| - String captionsText = captionsInfo.getText(); // get transcribed text |
42 |
| - String speakerName = captionsInfo.getSpeaker().getDisplayName; // get display name of current speaker |
43 |
| - Date timeStamp = captionsInfo.getTimestamp(); // get timestamp corresponding to caption |
44 |
| - |
45 |
| - // display captionsText and information as needed... |
46 |
| - } |
47 |
| -}; |
| 55 | +``` java |
| 56 | +public void addOnIsCaptionsEnabledChangedListener() { |
| 57 | + communicationCaptions.addOnCaptionsEnabledChangedListener( (PropertyChangedEvent args) -> { |
| 58 | + if(communicationCaptions.isEnabled()) { |
| 59 | + // captions enabled |
| 60 | + } |
| 61 | + }); |
| 62 | +} |
48 | 63 | ```
|
49 |
| -4. Register the `captionsListener` |
50 |
| -5. Invoke `startCaptions` with the desired options. |
51 |
| -```java |
52 |
| -public void startCallCaptions() { |
53 |
| - captionsCallFeature.addOnCaptionsReceivedListener(captionsListener); |
54 |
| - if (!captionsCallFeature.isCaptionsActive) { |
55 |
| - StartCaptionsOptions startCaptionsOptions = new StartCaptionsOptions(); |
56 |
| - startCaptionsOptions.setLanguage("en-us"); |
57 |
| - |
58 |
| - try { |
59 |
| - captionsCallFeature.startCaptions(startCaptionsOptions); |
60 |
| - } catch (Exception e) { |
61 |
| - e.printStackTrace(); |
| 64 | + |
| 65 | +### Add a listener to receive captions type changed |
| 66 | +This event will be triggered when the caption type changes from `CommunicationCaptions` to `TeamsCaptions` upon inviting Microsoft 365 users to ACS-only calls. |
| 67 | + |
| 68 | +``` java |
| 69 | +public void addOnIsCaptionsTypeChangedListener() { |
| 70 | + captionsCallFeature.addOnActiveCaptionsTypeChangedListener( (PropertyChangedEvent args) -> { |
| 71 | + if(communicationCaptions.isEnabled()) { |
| 72 | + // captionsCallFeature.getCaptions(); |
62 | 73 | }
|
63 |
| - } |
| 74 | + }); |
64 | 75 | }
|
65 | 76 | ```
|
66 | 77 |
|
| 78 | +### Add listener for captions data received |
| 79 | + |
| 80 | +``` java |
| 81 | +CommunicationCaptionsListener captionsListener = (CommunicationCaptionsReceivedEvent args) -> { |
| 82 | + // Information about the speaker. |
| 83 | + // CallerInfo participantInfo = args.getSpeaker(); |
| 84 | + // The original text with no transcribed. |
| 85 | + // args.getSpokenText(); |
| 86 | + // language identifier for the speaker. |
| 87 | + // args.getSpokenLanguage(); |
| 88 | + // Timestamp denoting the time when the corresponding speech was made. |
| 89 | + // args.getTimestamp(); |
| 90 | + // CaptionsResultType is Partial if text contains partially spoken sentence. |
| 91 | + // It is set to Final once the sentence has been completely transcribed. |
| 92 | + // args.getResultType() == CaptionsResultType.FINAL; |
| 93 | +}; |
| 94 | +public void addOnCaptionsReceivedListener() { |
| 95 | + communicationCaptions.addOnCaptionsReceivedListener(captionsListener); |
| 96 | +} |
| 97 | +``` |
67 | 98 |
|
68 |
| -### Stopping captions |
| 99 | +### Add a listener to receive active spoken language changed status |
69 | 100 |
|
70 |
| -Remove the previously registered `captionsListener`. |
| 101 | +``` java |
| 102 | +public void addOnActiveSpokenLanguageChangedListener() { |
| 103 | + communicationCaptions.addOnActiveSpokenLanguageChangedListener( (PropertyChangedEvent args) -> { |
| 104 | + // communicationCaptions.getActiveSpokenLanguage() |
| 105 | + }); |
| 106 | +} |
| 107 | +``` |
71 | 108 |
|
72 |
| -```java |
73 |
| -public void stopCaptions() { |
74 |
| - captionsCallFeature.removeOnCaptionsReceivedListener(captionsListener); |
| 109 | +## Start captions |
| 110 | + |
| 111 | +Once you've set up all your listeners, you can now start adding captions. |
| 112 | + |
| 113 | +``` java |
| 114 | +public void startCaptions() { |
| 115 | + StartCaptionsOptions startCaptionsOptions = new StartCaptionsOptions(); |
| 116 | + startCaptionsOptions.setSpokenLanguage("en-us"); |
| 117 | + communicationCaptions.startCaptions(startCaptionsOptions).whenComplete((result, error) -> { |
| 118 | + if (error != null) { |
| 119 | + } |
| 120 | + }); |
75 | 121 | }
|
76 | 122 | ```
|
77 | 123 |
|
78 |
| -### Get available languages |
| 124 | +## Stop captions |
79 | 125 |
|
80 |
| -Call the `getAvailableLanguages` method on the `CaptionsCallFeature` API. |
| 126 | +``` java |
| 127 | +public void stopCaptions() { |
| 128 | + communicationCaptions.stopCaptions().whenComplete((result, error) -> { |
| 129 | + if (error != null) { |
| 130 | + } |
| 131 | + }); |
| 132 | +} |
| 133 | +``` |
81 | 134 |
|
82 |
| -```java |
83 |
| -String[] captionsAvailableLanguages = captionsCallFeature.getAvailableLanguages(); |
| 135 | +## Remove caption received listener |
| 136 | + |
| 137 | +``` java |
| 138 | +public void removeOnCaptionsReceivedListener() { |
| 139 | + communicationCaptions.removeOnCaptionsReceivedListener(captionsListener); |
| 140 | +} |
84 | 141 | ```
|
85 | 142 |
|
86 |
| -### Update language |
| 143 | +## Spoken language support |
| 144 | + |
| 145 | +### Get list of supported spoken languages |
| 146 | +Get a list of supported spoken languages that your users can select from when enabling closed captions. |
87 | 147 |
|
88 |
| -Pass a value in from the available languages array to ensure that the requested language is supported. |
| 148 | +``` java |
| 149 | +// bcp 47 formatted language code |
| 150 | +communicationCaptions.getSupportedSpokenLanguages(); |
| 151 | +``` |
| 152 | + |
| 153 | +### Set spoken language |
| 154 | +When the user selects the spoken language, your app can set the spoken language that it expects captions to be generated from. |
89 | 155 |
|
90 |
| -```java |
91 |
| -public void switchCaptionsLanguage() { |
92 |
| - if (!captionsCallFeature.isCaptionsActive) { |
93 |
| - return; |
94 |
| - } |
95 |
| - try { |
96 |
| - captionsCallFeature.select(captionsAvailableLanguages[0]); |
97 |
| - } catch (Exception e) { |
98 |
| - e.printStackTrace(); |
99 |
| - } |
| 156 | +``` java |
| 157 | +public void setSpokenLanguage() { |
| 158 | + communicationCaptions.setSpokenLanguage("en-us").whenComplete((result, error) -> { |
| 159 | + if (error != null) { |
| 160 | + } |
| 161 | + }); |
100 | 162 | }
|
101 | 163 | ```
|
102 | 164 |
|
103 | 165 | ## Clean up
|
104 |
| -Learn more about [cleaning up resources here.](../../../create-communication-resource.md?pivots=platform-azp&tabs=windows#clean-up-resources) |
| 166 | +Learn more about [cleaning up resources here.](../../../create-communication-resource.md?pivots=platform-azp&tabs=windows#clean-up-resources) |
0 commit comments