12-HarmonyOS5-SpeechKit-AICaptionComponent-Case

4 months ago 5

Case Study of SpeechKit AICaption Component in HarmonyOS 5.0 Abstract This article introduces how to use @hms.ai.AICaption and @kit.AudioKit to implement voice recording and AI caption display functions in HarmonyOS 5.0. By creating the SpeechKitAICaptionComponent component, users can control the start and end of recording and display AI captions during recording. import { AICaptionComponent, AICaptionController, AudioData } from '@hms.ai.AICaption'; import { audio } from '@kit.AudioKit';

@Entry @Component struct SpeechKitAICaptionComponent {

controller = new AICaptionController audioCapturer?: audio.AudioCapturer @State isShown: boolean = false async start () { let audioStreamInfo: audio.AudioStreamInfo = { samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000, channels: audio.AudioChannel.CHANNEL_1, sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW }; let audioCapturerInfo: audio.AudioCapturerInfo = { source: audio.SourceType.SOURCE_TYPE_MIC, capturerFlags: 0 }; let audioCapturerOptions: audio.AudioCapturerOptions = { streamInfo: audioStreamInfo, capturerInfo: audioCapturerInfo }; this.audioCapturer = await audio.createAudioCapturer(audioCapturerOptions) this.audioCapturer.on('readData', (buffer) => { const audioData: AudioData = { data: new Uint8Array(buffer) } this.controller.writeAudio(audioData) }) await this.audioCapturer.start() this.isShown = true } async stop () { await this.audioCapturer?.release() this.isShown = false } build() { Column({ space: 15 }) { Button('Start Recording') .onClick(() => { this.start() }) Button('Stop Recording') .onClick(() => { this.stop() }) if (this.isShown) { AICaptionComponent({ isShown: this.isShown, controller: this.controller, options: { initialOpacity: 1, onPrepared: () => { // }, onError: (e) => { // } } }) .width('100%') .height(100) } } .padding(15) .height('100%') .width('100%') } }
Read Entire Article