@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%')
}
}