iOS&Mac

Updated at:

The real-time caption feature provides a speech-to-text service for scenarios such as online meetings and online education. This feature helps attendees better understand the content. DingRTC provides this feature. This topic describes how to integrate real-time captions on iOS and Mac clients.

1. Integration instructions

Integrate version 3.8.0 or later of the software development kit (SDK).

2. Implement basic caption features

1. Before you use the caption feature on an iOS or Mac client, your application server must integrate the Smart Minutes feature and start it by calling the StartCloudNote API.

2. Follow the instructions in the Quick Start section to create an engine, join a channel, and publish and subscribe to audio and video streams.

3. Obtain the caption manager.

DingRtcEngineSubtitleManager *subtitleEngine = [rtcEngine getSubtitleManager];

4. Set the spoken language type.

[subtitleEngine setCurrentTranslateLanguages:@"zg"];

5. Set the translation language list.

NSMutableSet<NSString *> *selectedTransLanguageIds;
[selectedTransLanguageIds addObject:@"zh"];
[subtitleEngine setCurrentTranslateLanguages:selectedTransLanguageIds.allObjects];

6. Set the caption listener.

// Implement the subtitle manager delegate protocol
@interface MySubtitleDelegate : NSObject <DingRtcEngineSubtitleManagerDelegate>

@end

@implementation MySubtitleDelegate

// Callback for language list updates
- (void)onLanguageListsUpdated {
    NSLog(@"Language lists updated. Please refresh the language list.");
}

// Callback for subtitle service status changes
- (void)onSubtitleServiceStatusChanged:(DingRtcSubtitleStatus)status {
    NSLog(@"Subtitle service status changed to: %d", status);
}

// Callback for subtitle messages
- (void)onSubtitleMessage:(DingRtcSubtitleMessage *)message {
    NSLog(@"Received subtitle message: %@", message.text);
}

@end

// Create a delegate instance
MySubtitleDelegate *delegate = [[MySubtitleDelegate alloc] init];

// Set the delegate for the subtitle manager
[subtitleEngine setDelegate:delegate];

7. Enable captions.

[subtitleEngine enableSubtitle:YES];

III. iOS/Mac API reference

1. Caption Manager

@interface DingRtcEngineSubtitleManager : NSObject

/**
 * @ingroup OC_DingRtcEngineSubtitle
 * 
 * @since 3.8
 * @brief Sets the caption manager listener.
 * @param delegate  Used to receive events from the caption manager.
 */
- (void)setDelegate:(id<DingRtcEngineSubtitleManagerDelegate>_Nullable)delegate;

/**
 * @ingroup OC_DingRtcEngineSubtitle
 * 
 * @since 3.8
 * @brief Enables or disables captions.
 * @param enable
 * - Specifies whether to enable or disable captions.
 * - YES: Enables captions.
 * - NO: Disables captions.
 * @return
 * - 0: Success.
 * - A non-zero value: Failure.
 */
- (int)enableSubtitle:(BOOL)enable;

/**
 * @ingroup OC_DingRtcEngineSubtitle
 *
 * @since 3.8
 * @brief Gets the list of supported spoken languages.
 * @return The list of supported spoken languages.
 */
- (NSArray<NSString *> * _Nullable)getSupportedSpeakLanguages;

/**
 * @ingroup OC_DingRtcEngineSubtitle
 *
 * @since 3.8
 * @brief Selects the spoken language.
 * @param language The spoken language.
 * @return
 * - 0: Success.
 * - A non-zero value: Failure.
 */
- (int)setCurrentSpeakLanguage:(NSString * _Nonnull)language;

/**
 * @ingroup OC_DingRtcEngineSubtitle
 *
 * @since 3.8
 * @brief Gets the list of supported translation languages.
 * @return The list of supported translation languages.
 */
- (NSArray<NSString *> * _Nullable)getSupportedTranslateLanguages;

/**
 * @ingroup OC_DingRtcEngineSubtitle
 *
 * @since 3.8
 * @brief Selects the translation languages.
 * @param languages The translation languages.
 * @return
 * - 0: Success.
 * - A non-zero value: Failure.
 */
- (int)setCurrentTranslateLanguages:(NSArray<NSString *> * _Nonnull)languages;

@end

2. Caption listener

@protocol DingRtcEngineSubtitleManagerDelegate <NSObject>
@optional

/**
 * @ingroup OC_DingRtcEngineSubtitle
 * 
 * @since 3.8
 * @brief Callback for language list updates.
 */
- (void)onLanguageListsUpdated;

/**
 * @ingroup OC_DingRtcEngineSubtitle
 *
 * @since 3.8
 * @brief Callback for changes in the subtitle service status.
 * @param status The status of the subtitle service.
 */
- (void)onSubtitleServiceStatusChanged:(DingRtcSubtitleStatus)status;

/**
 * @ingroup OC_DingRtcEngineSubtitle
 *
 * @since 3.8
 * @brief Callback for subtitle messages.
 * @param message The subtitle message.
 */
- (void)onSubtitleMessage:(DingRtcSubtitleMessage*)message;

@end

3. Related data classes

typedef NS_ENUM(NSUInteger, DingRtcSubtitleStatus) {
    /** The subtitle service is enabled. */
    DingRtcSubtitleStatusOpen = 0,
    /** The subtitle service is disabled. */
    DingRtcSubtitleStatusClose = 1,
    /** An error occurred in the subtitle service. */
    DingRtcSubtitleStatusError = 2,
};

@interface DingRtcSubtitleMessage : NSObject

/** The user ID for the subtitle. */
@property (nonatomic, copy) NSString * _Nonnull userId;
/** The content of the subtitle. */
@property (nonatomic, copy) NSString * _Nonnull subtitle;
/** The index of the subtitle sentence. */
@property (nonatomic, assign) uint64_t sentenceIndex;
/** A flag that indicates whether the sentence has ended. */
@property (nonatomic, assign) BOOL sentenceEnd;
/** A flag that indicates whether to translate the subtitle. */
@property (nonatomic, assign) BOOL translate;
/** The language of the subtitle. */
@property (nonatomic, copy) NSString * _Nonnull language;
/** The start time of the subtitle sentence. */
@property (nonatomic, assign) uint64_t beginTime;
/** The end time of the subtitle sentence. */
@property (nonatomic, assign) uint64_t endTime;

@end