Delen via


QuickStart: Ondertiteling toevoegen aan uw aanroepende app

Vereisten

Notitie

Houd er rekening mee dat u een app voor spraakgesprekken moet hebben met azure Communication Services-aanroepende SDK's voor toegang tot de functie ondertiteling die in deze handleiding wordt beschreven.

Modellen

Name Beschrijving
CaptionsCallFeature API voor bijschriften
CaptionsCommon Basisklasse voor bijschriften
StartCaptionOptions Opties voor ondertiteling, zoals gesproken taal
CaptionsHandler Callback-definitie voor het verwerken van de gebeurtenis CaptionsReceivedEventType
CaptionsInfo Gegevensstructuur ontvangen voor elke captionsReceivedEventType-gebeurtenis

Functie voor ondertiteling ophalen

let captionsCallFeature: SDK.CaptionsCallFeature = call.feature(SDK.Features.Captions);

Ondertitelingsobject ophalen

U moet het object Captions ophalen en casten om specifieke functies voor bijschriften te gebruiken.

let captions: SDK.Captions;
if (captionsCallFeature.captions.kind === 'Captions') {
    captions = captionsCallFeature.captions as SDK.Captions;
}

Abonneren op listeners

Een listener toevoegen om de actieve/inactieve status van bijschriften te ontvangen

const captionsActiveChangedHandler = () => {
    if (captions.isCaptionsFeatureActive) {
        /* USER CODE HERE - E.G. RENDER TO DOM */
    }
}
captions.on('CaptionsActiveChanged', captionsActiveChangedHandler);

Een listener toevoegen voor ontvangen bijschriftgegevens

Het geretourneerde captionsInfo-gegevensobject verwerken.

Opmerking: het object bevat een resultType prop die aangeeft of de gegevens een gedeeltelijk bijschrift of een definitieve versie van het bijschrift zijn. ResultType Partial geeft live niet-bewerkt bijschrift aan, terwijl Final een definitieve geïnterpreteerde versie van de zin wordt aangegeven (dat wil bijvoorbeeld interpunctie en hoofdlettergebruik bevatten).

const captionsReceivedHandler : CaptionsHandler = (data: CaptionsInfo) => { 
    /** USER CODE HERE - E.G. RENDER TO DOM 
     * data.resultType
     * data.speaker
     * data.spokenLanguage
     * data.spokenText
     * data.timeStamp
    */
   // Example code:
   // Create a dom element, i.e. div, with id "captionArea" before proceeding with the sample code
    let mri: string;
    switch (data.speaker.identifier.kind) {
        case 'communicationUser': { mri = data.speaker.identifier.communicationUserId; break; }
        case 'phoneNumber': { mri = data.speaker.identifier.phoneNumber; break; }
    }
    const outgoingCaption = `prefix${mri.replace(/:/g, '').replace(/-/g, '')}`;

    let captionArea = document.getElementById("captionArea");
    const captionText = `${data.timestamp.toUTCString()}
        ${data.speaker.displayName}: ${data.spokenText}`;

    let foundCaptionContainer = captionArea.querySelector(`.${outgoingCaption}[isNotFinal='true']`);
    if (!foundCaptionContainer) {
        let captionContainer = document.createElement('div');
        captionContainer.setAttribute('isNotFinal', 'true');
        captionContainer.style['borderBottom'] = '1px solid';
        captionContainer.style['whiteSpace'] = 'pre-line';
        captionContainer.textContent = captionText;
        captionContainer.classList.add(outgoingCaption);

        captionArea.appendChild(captionContainer);
    } else {
        foundCaptionContainer.textContent = captionText;

        if (captionData.resultType === 'Final') {
            foundCaptionContainer.setAttribute('isNotFinal', 'false');
        }
    }
}; 
captions.on('CaptionsReceived', captionsReceivedHandler); 

Een listener toevoegen om de gewijzigde status van de gesproken taal te ontvangen

// set a local variable currentSpokenLanguage to track the current spoken language in the call
let currentSpokenLanguage = ''
const spokenLanguageChangedHandler = () => {
    if (captions.activeSpokenLanguage !== currentSpokenLanguage) {
        /* USER CODE HERE - E.G. RENDER TO DOM */
    }
}
captions.on('SpokenLanguageChanged', spokenLanguageChangedHandler)

Bijschriften starten

Zodra u al uw listeners hebt ingesteld, kunt u nu beginnen met het toevoegen van bijschriften.

try {
    await captions.startCaptions({ spokenLanguage: 'en-us' });
} catch (e) {
    /* USER ERROR HANDLING CODE HERE */
}

Bijschriften stoppen

try {
    captions.stopCaptions(); 
} catch (e) {
    /* USER ERROR HANDLING CODE HERE */
}

Afmelden voor listeners

captions.off('CaptionsActiveChanged', captionsActiveChangedHandler);
captions.off('CaptionsReceived', captionsReceivedHandler); 

Ondersteuning voor gesproken taal

Een lijst met ondersteunde gesproken talen ophalen

Haal een lijst op met ondersteunde gesproken talen waaruit uw gebruikers kunnen kiezen bij het inschakelen van ondertiteling. De eigenschap retourneert een matrix met talen in bcp 47-indeling.

const spokenLanguages = captions.supportedSpokenLanguages; 

Gesproken taal instellen

Geef een waarde door vanuit de matrix met ondersteunde gesproken talen om ervoor te zorgen dat de aangevraagde taal wordt ondersteund. Als contoso standaard geen taal of een niet-ondersteunde taal biedt, wordt de gesproken taal standaard ingesteld op 'en-us'.

// bcp 47 formatted language code
const language = 'en-us'; 

// Alternatively, pass a value from the supported spoken languages array
const language = spokenLanguages[0]; 

try {
    captions.setSpokenLanguage(language);
} catch (e) {
    /* USER ERROR HANDLING CODE HERE */
}

Een listener toevoegen om de status gewijzigd van ondertiteling te ontvangen

Het type bijschriften kan worden gewijzigd van bijschriften in TeamsCaptions als een Teams/CTE-gebruiker deelneemt aan de oproep of als de oproep wordt gewijzigd in een gesprekstype voor interoperabiliteit. Opnieuw aanmelden bij listeners van Teams-bijschriften is vereist om de ervaring met bijschriften voort te zetten. TeamsCaptions-soort kan niet worden overgeschakeld of gewijzigd in ondertitelingstype in een gesprek zodra TeamsCaptions in de oproep wordt gebruikt.

const captionsKindChangedHandler = () => {
    /* USER CODE HERE - E.G. SUBSCRIBE TO TEAMS CAPTIONS */
}
captions.on('CaptionsKindChanged', captionsKindChangedHandler)

Vereisten

Notitie

Houd er rekening mee dat u een app voor spraakgesprekken moet hebben met azure Communication Services-aanroepende SDK's voor toegang tot de functie ondertiteling die in deze handleiding wordt beschreven.

Modellen

Name Beschrijving
CaptionsCallFeature API voor aanroepfunctie voor bijschriften
CommunicationCaptions API voor communicatiebijschriften
StartCaptionOptions Opties voor ondertiteling, zoals gesproken taal
CommunicationCaptionsReceivedEventArgs Gegevensobject dat is ontvangen voor elke ontvangen communicatiebijschriften

Functie voor ondertiteling ophalen

U moet het object Captions ophalen en casten om specifieke functies voor bijschriften te gebruiken.

CaptionsCallFeature captionsCallFeature = call.Features.Captions;
CallCaptions callCaptions = await captionsCallFeature.GetCaptionsAsync();
if (callCaptions.CaptionsKind == CaptionsKind.CommunicationCaptions)
{
    CommunicationCaptions communicationCaptions = callCaptions as CommunicationCaptions;
} 

Abonneren op listeners

Een listener toevoegen voor het ontvangen van ondertitelingsstatus ingeschakeld/uitgeschakeld

communicationCaptions.CaptionsEnabledChanged += OnIsCaptionsEnabledChanged;

private void OnIsCaptionsEnabledChanged(object sender, PropertyChangedEventArgs args)
{
    if (communicationCaptions.IsEnabled)
    {
    }
}

Een listener toevoegen om het type bijschriften te ontvangen dat is gewijzigd

Deze gebeurtenis wordt geactiveerd wanneer het bijschrifttype wordt gewijzigd van waaruit CommunicationCaptions TeamsCaptions microsoft 365-gebruikers worden uitgenodigd voor ALLEEN ACS-aanroepen.

captionsCallFeature.ActiveCaptionsTypeChanged += OnIsCaptionsTypeChanged;

private void OnIsCaptionsTypeChanged(object sender, PropertyChangedEventArgs args)
{
    // get captions
}

Listener toevoegen voor ontvangen bijschriftgegevens

communicationCaptions.CaptionsReceived += OnCaptionsReceived;

private void OnCaptionsReceived(object sender, CommunicationCaptionsReceivedEventArgs eventArgs)
{
    // Information about the speaker.
    // eventArgs.Speaker
    // The original text with no transcribed.
    // eventArgs.SpokenText
    // language identifier for the speaker.
    // eventArgs.SpokenLanguage
    // Timestamp denoting the time when the corresponding speech was made.
    // eventArgs.Timestamp
    // CaptionsResultKind is Partial if text contains partially spoken sentence.
    // It is set to Final once the sentence has been completely transcribed.
    // eventArgs.ResultKind
}

Een listener toevoegen om de status actieve gesproken taal te ontvangen

communicationCaptions.ActiveSpokenLanguageChanged += OnIsActiveSpokenLanguageChanged;

private void OnIsActiveSpokenLanguageChanged(object sender, PropertyChangedEventArgs args)
{
    // communicationCaptions.ActiveSpokenLanguage
}

Bijschriften starten

Zodra u al uw listeners hebt ingesteld, kunt u nu beginnen met het toevoegen van bijschriften.


private async void StartCaptions()
{
    var options = new StartCaptionsOptions
    {
        SpokenLanguage = "en-us"
    };
    try
    {
        await communicationCaptions.StartCaptionsAsync(options);
    }
    catch (Exception ex)
    {
    }
}

Bijschriften stoppen

private async void StopCaptions()
{
    try
    {
        await communicationCaptions.StopCaptionsAsync();
    }
    catch (Exception ex)
    {
    }
}

Ondertiteling ontvangen listener verwijderen

communicationCaptions.CaptionsReceived -= OnCaptionsReceived;

Ondersteuning voor gesproken taal

Lijst met ondersteunde gesproken talen ophalen

Haal een lijst op met ondersteunde gesproken talen waaruit uw gebruikers kunnen kiezen bij het inschakelen van ondertiteling.

// bcp 47 formatted language code
IReadOnlyList<string> sLanguages = communicationCaptions.SupportedSpokenLanguages;```

### Set spoken language 
When the user selects the spoken language, your app can set the spoken language that it expects captions to be generated from. 

``` cs 
public async void SetSpokenLanguage()
{
    try
    {
        await communicationCaptions.SetSpokenLanguageAsync("en-us");
    }
    catch (Exception ex)
    {
    }
}

Opschonen

Meer informatie over het opschonen van resources vindt u hier.

Vereisten

Notitie

Houd er rekening mee dat u een app voor spraakgesprekken moet hebben met azure Communication Services-aanroepende SDK's voor toegang tot de functie ondertiteling die in deze handleiding wordt beschreven.

Modellen

Name Beschrijving
CaptionsCallFeature API voor aanroepfunctie voor bijschriften
CommunicationCaptions API voor communicatiebijschriften
StartCaptionOptions Opties voor ondertiteling, zoals gesproken taal
CommunicationCaptionsListener Listener voor CommunicationCaptions addOnCaptionsReceivedListener
CommunicationCaptionsReceivedEvent Gegevensobject ontvangen voor elke CommunicationCaptionsListener-gebeurtenis

Functie voor ondertiteling ophalen

U moet het object Captions ophalen en casten om specifieke functies voor bijschriften te gebruiken.

CaptionsCallFeature captionsCallFeature = call.feature(Features.CAPTIONS);
captionsCallFeature.getCaptions().whenComplete(
    ((captions, throwable) -> {
        if (throwable == null) {
            CallCaptions callCaptions = captions;
            if (captions.getCaptionsType() == CaptionsType.COMMUNICATION_CAPTIONS) {
            // communication captions
            CommunicationCaptions communicationCaptions = (CommunicationCaptions) captions;
            }
        } else {
        // get captions failed
        // throwable is the exception/cause
        }
    }));

Abonneren op listeners

Een listener toevoegen voor het ontvangen van ondertitelingsstatus ingeschakeld/uitgeschakeld

public void addOnIsCaptionsEnabledChangedListener() {
    communicationCaptions.addOnCaptionsEnabledChangedListener( (PropertyChangedEvent args) -> {
        if(communicationCaptions.isEnabled()) {
            // captions enabled
        }
    });
}

Een listener toevoegen om het type bijschriften te ontvangen dat is gewijzigd

Deze gebeurtenis wordt geactiveerd wanneer het bijschrifttype wordt gewijzigd van waaruit CommunicationCaptions TeamsCaptions microsoft 365-gebruikers worden uitgenodigd voor ALLEEN ACS-aanroepen.

public void addOnIsCaptionsTypeChangedListener() {
    captionsCallFeature.addOnActiveCaptionsTypeChangedListener( (PropertyChangedEvent args) -> {
        if(communicationCaptions.isEnabled()) {
            // captionsCallFeature.getCaptions();
        }
    });
}

Listener toevoegen voor ontvangen bijschriftgegevens

CommunicationCaptionsListener captionsListener = (CommunicationCaptionsReceivedEvent args) -> {
  // Information about the speaker.
  // CallerInfo participantInfo = args.getSpeaker();
  // The original text with no transcribed.
  // args.getSpokenText();
  // language identifier for the speaker.
  // args.getSpokenLanguage();
  // Timestamp denoting the time when the corresponding speech was made.
  // args.getTimestamp();
  // CaptionsResultType is Partial if text contains partially spoken sentence.
  // It is set to Final once the sentence has been completely transcribed.
  // args.getResultType() == CaptionsResultType.FINAL;
}; 
public void addOnCaptionsReceivedListener() {
  communicationCaptions.addOnCaptionsReceivedListener(captionsListener); 
}

Een listener toevoegen om de status actieve gesproken taal te ontvangen

public void addOnActiveSpokenLanguageChangedListener() {
    communicationCaptions.addOnActiveSpokenLanguageChangedListener( (PropertyChangedEvent args) -> {
       // communicationCaptions.getActiveSpokenLanguage()
    });
}

Bijschriften starten

Zodra u al uw listeners hebt ingesteld, kunt u nu beginnen met het toevoegen van bijschriften.

public void startCaptions() {
    StartCaptionsOptions startCaptionsOptions = new StartCaptionsOptions();
    startCaptionsOptions.setSpokenLanguage("en-us");
    communicationCaptions.startCaptions(startCaptionsOptions).whenComplete((result, error) -> {
        if (error != null) {
        }
    });
}

Bijschriften stoppen

public void stopCaptions() {
    communicationCaptions.stopCaptions().whenComplete((result, error) -> {
        if (error != null) {
        }
    });
}

Ondertiteling ontvangen listener verwijderen

public void removeOnCaptionsReceivedListener() {
    communicationCaptions.removeOnCaptionsReceivedListener(captionsListener);
}

Ondersteuning voor gesproken taal

Lijst met ondersteunde gesproken talen ophalen

Haal een lijst op met ondersteunde gesproken talen waaruit uw gebruikers kunnen kiezen bij het inschakelen van ondertiteling.

// bcp 47 formatted language code
communicationCaptions.getSupportedSpokenLanguages();

Gesproken taal instellen

Wanneer de gebruiker de gesproken taal selecteert, kan uw app de gesproken taal instellen waaruit wordt verwacht dat bijschriften worden gegenereerd.

public void setSpokenLanguage() {
    communicationCaptions.setSpokenLanguage("en-us").whenComplete((result, error) -> {
        if (error != null) {
        }
    });
}

Opschonen

Meer informatie over het opschonen van resources vindt u hier.

Vereisten

Notitie

Houd er rekening mee dat u een app voor spraakgesprekken moet hebben met azure Communication Services-aanroepende SDK's voor toegang tot de functie ondertiteling die in deze handleiding wordt beschreven.

Modellen

Name Beschrijving
CaptionsCallFeature API voor aanroepfunctie voor bijschriften
CommunicationCaptions API voor communicatiebijschriften
StartCaptionOptions Opties voor ondertiteling, zoals gesproken taal
CommunicationCaptionsDelegate Gemachtigde voor communicatiebijschriften
CommunicationCaptionsReceivedEventArgs Gegevensobject dat is ontvangen voor elke ontvangen communicatiebijschriften

Functie voor ondertiteling ophalen

U moet het object Captions ophalen en casten om specifieke functies voor bijschriften te gebruiken.

if let call = self.call {
    @State var captionsCallFeature = call.feature(Features.captions)
    captionsCallFeature.getCaptions{(value, error) in
        if let error = error {
            // failed to get captions
        } else {
            if (value?.type == CaptionsType.communicationCaptions) {
                // communication captions
                @State var communicationCaptions = value as? CommunicationCaptions
            }
        }
    }
}

Abonneren op listeners

Een listener toevoegen voor het ontvangen van bijschriften ingeschakeld/uitgeschakeld, type, gesproken taal, status van bijschrifttaal gewijzigd en ontvangen gegevens

De gebeurtenis didChangeActiveCaptionsType wordt geactiveerd wanneer het bijschrifttype wordt gewijzigd van CommunicationCaptions naar TeamsCaptions bij het uitnodigen van Microsoft 365-gebruikers voor alleen ACS-aanroepen.

extension CallObserver: CommunicationCaptionsDelegate {
    // listener for receive captions enabled/disabled status
    public func communicationCaptions(_ communicationCaptions: CommunicationCaptions, didChangeCaptionsEnabledState args: PropertyChangedEventArgs) {
        // communicationCaptions.isEnabled
    }
    
    // listener for active spoken language state change
    public func communicationCaptions(_ communicationCaptions: CommunicationCaptions, didChangeActiveSpokenLanguageState args: PropertyChangedEventArgs) {
        // communicationCaptions.activeSpokenLanguage
    }
    
    // listener for captions data received
    public func communicationCaptions(_ communicationCaptions: CommunicationCaptions, didReceiveCaptions:CommunicationCaptionsReceivedEventArgs) {
            // Information about the speaker.
            // didReceiveCaptions.speaker
            // The original text with no transcribed.
            // didReceiveCaptions.spokenText
            // language identifier for the speaker.
            // didReceiveCaptions.spokenLanguage
            // Timestamp denoting the time when the corresponding speech was made.
            // didReceiveCaptions.timestamp
            // CaptionsResultType is Partial if text contains partially spoken sentence.
            // It is set to Final once the sentence has been completely transcribed.
            // didReceiveCaptions.resultType
    }
}

communicationCaptions.delegate = self.callObserver

extension CallObserver: CaptionsCallFeatureDelegate {
    // captions type changed
    public func captionsCallFeature(_ captionsCallFeature: CaptionsCallFeature, didChangeActiveCaptionsType args: PropertyChangedEventArgs) {
        // captionsCallFeature.getCaptions to get captions
    }
}

captionsCallFeature.delegate = self.callObserver

Bijschriften starten

Zodra u al uw listeners hebt ingesteld, kunt u nu beginnen met het toevoegen van bijschriften.

func startCaptions() {
    guard let communicationCaptions = communicationCaptions else {
        return
    }
    let startCaptionsOptions = StartCaptionsOptions()
    startCaptionsOptions.spokenLanguage = "en-us"
    communicationCaptions.startCaptions(startCaptionsOptions: startCaptionsOptions, completionHandler: { (error) in
        if error != nil {
            
        }
    })
}

Bijschriften stoppen

func stopCaptions() {
    communicationCaptions.stopCaptions(completionHandler: { (error) in
        if error != nil {
            
        }
    })
}

Ondertiteling ontvangen listener verwijderen

communicationCaptions?.delegate = nil

Ondersteuning voor gesproken taal

Lijst met ondersteunde gesproken talen ophalen

Haal een lijst op met ondersteunde gesproken talen waaruit uw gebruikers kunnen kiezen bij het inschakelen van ondertiteling.

// bcp 47 formatted language code
let spokenLanguage : String = "en-us"
for language in communicationCaptions?.supportedSpokenLanguages ?? [] {
    // choose required language
    spokenLanguage = language
}

Gesproken taal instellen

Wanneer de gebruiker de gesproken taal selecteert, kan uw app de gesproken taal instellen waaruit wordt verwacht dat bijschriften worden gegenereerd.

func setSpokenLanguage() {
    guard let communicationCaptions = self.communicationCaptions else {
        return
    }

    communicationCaptions.set(spokenLanguage: spokenLanguage, completionHandler: { (error) in
        if let error = error {
        }
    })
}

Opschonen

Meer informatie over het opschonen van resources vindt u hier.

Resources opschonen

Als u een Communication Services-abonnement wilt opschonen en verwijderen, kunt u de resource of resourcegroep verwijderen. Als u de resourcegroep verwijdert, worden ook alle bijbehorende resources verwijderd. Meer informatie over het opschonen van resources.

Volgende stappen

Raadpleeg voor meer informatie de volgende artikelen: