برنامه نویسی
01-harmonyos5-coreseechkit-case

شرح پرونده
این یک مورد متن به گفتار است که بر اساس خدمات صوتی اساسی AI اجرا شده است.
مراحل اجرای:
1. ماژول های لازم را وارد کنید
وارد کردن textToSpeech
وت promptAction
ماژول ها ، که به ترتیب برای متن به گفتار و نمایش پیام های سریع استفاده می شوند.
import { textToSpeech } from '@kit.CoreSpeechKit'
import { promptAction } from '@kit.ArkUI'
توضیح: textToSpeech
ماژول عملکرد متن به گفتار و promptAction
از ماژول برای نمایش پیام های سریع در رابط استفاده می شود.
2. مؤلفه را تعریف کنید
از @Entry
وت @ComponentV2
دکوراتورها برای تعریف مؤلفه ای به نام CoreSpeechKit
بشر
@Entry
@ComponentV2
struct CoreSpeechKit {
// Text content to be broadcast
text: string = `
In March at 45 degrees north latitude, time ties a gentle knot here. The morning mist by the Songhua River wraps fine snow, carving each branch into transparent glass. Ice crystals stack on the branches to form magnolias with a thousand petals. What rustles down when the wind passes through the branches is not snowflakes, but clearly the fragments of stars falling into the world.
At this time, the apricot blossoms in Jiangnan are brewing honey against the warm wind. The pink clouds of flower shadows spread over the white walls and black tiles, and even the air is permeated with a slightly intoxicating sweetness. The colorist of the seasons quietly divides the palette in half - the north is still persistently continuing the end of winter, using icicles as a pen to outline frost flowers on the window panes; the south is already eager to open the first page of spring, letting the swallows fly over the ink - painted alleys with the willow color.
This temperature difference of three thousand miles weaves two silk brocades at both ends of the twilight line: the north is a plain brocade embroidered with silver threads, wrapped with unspoken whispers in the cold; the south is a soft silk dyed by silk - reeling women, rippling with the newborn whispers in the warmth. When the rime in Harbin melts the first drop of brilliance in the morning sun, the magnolias in Hangzhou just shake off the third piece of moonlight on their shoulders. It turns out that spring is stepping on the latitude and longitude lines, composing the most moving polyphony between the harshness and the warmth in the world.
`
// Text-to-speech engine instance
ttsEngine?: textToSpeech.TextToSpeechEngine
// Local state to mark whether it is playing
@Local isPlaying: boolean = false
// Initialize the text-to-speech engine
async initTextToSpeechEngine() {
if (canIUse('SystemCapability.AI.TextToSpeech')) {
const params: textToSpeech.CreateEngineParams = {
language: 'zh-CN',
person: 0,
online: 1
}
this.ttsEngine = await textToSpeech.createEngine(params)
}
}
// Called when the component is about to appear
aboutToAppear(): void {
this.initTextToSpeechEngine()
}
// Called when the component is about to disappear
aboutToDisappear(): void {
if (canIUse('SystemCapability.AI.TextToSpeech')) {
this.ttsEngine?.stop()
this.ttsEngine?.shutdown()
}
}
// Play voice
play() {
if (canIUse('SystemCapability.AI.TextToSpeech')) {
if (this.ttsEngine?.isBusy()) {
return promptAction.showToast({ message: 'Playing...' })
}
const params: textToSpeech.SpeakParams = {
requestId: '10000'
}
this.ttsEngine?.speak(this.text, params)
this.isPlaying = true
}
}
// Build the component interface
build() {
Stack({ alignContent: Alignment.BottomEnd }) {
List() {
ListItem() {
Text(this.text)
.lineHeight(32)
}
}
.padding({ left: 15, right: 15 })
.height('100%')
.width('100%')
Row() {
Image(this.isPlaying ? $r('sys.media.AI_playing') : $r('sys.media.AI_play'))
.width(24)
.onClick(() => this.play())
}
.borderRadius(24)
.shadow({
color: Color.Gray,
offsetX: 5,
offsetY: 5,
radius: 15
})
.width(48)
.aspectRatio(1)
.justifyContent(FlexAlign.Center)
.margin({ right: 50, bottom: 50 })
}
.height('100%')
.width('100%')
}
}
توضیح:
-
text
محتوای متن را برای پخش ذخیره می کند. -
ttsEngine
نمونه ای از موتور متن به گفتار است. -
isPlaying
برای نشان دادن اینکه آیا صدا پخش می شود استفاده می شود. - در
initTextToSpeechEngine
از روش برای اولیه سازی موتور متن به گفتار استفاده می شود. - در
aboutToAppear
روش هنگامی که این مؤلفه برای شروع موتور به نظر می رسد ، روش فراخوانی می شود. - در
aboutToDisappear
روش هنگامی که مؤلفه در حال ناپدید شدن برای متوقف کردن و خاموش کردن موتور است ، روش فراخوانی می شود. - در
play
از روش برای پخش صدا استفاده می شود. اگر موتور شلوغ باشد ، پیام سریع را نمایش می دهد. - در
build
روش رابط مؤلفه ، از جمله لیستی برای نمایش متن و یک دکمه پخش را ایجاد می کند.
خلاصه:
نکات کلیدی
- از
textToSpeech
ماژول برای اجرای عملکرد متن به گفتار. - موتور را در روشهای چرخه عمر مؤلفه شروع کرده و خاموش کنید.
- نمایشگر دکمه پخش را از طریق کنترل کنید
isPlaying
دولت
رمز کامل
import { textToSpeech } from '@kit.CoreSpeechKit'
import { promptAction } from '@kit.ArkUI'
@Entry
@ComponentV2
struct CoreSpeechKit {
text: string = `
In March at 45 degrees north latitude, time ties a gentle knot here. The morning mist by the Songhua River wraps fine snow, carving each branch into transparent glass. Ice crystals stack on the branches to form magnolias with a thousand petals. What rustles down when the wind passes through the branches is not snowflakes, but clearly the fragments of stars falling into the world.
At this time, the apricot blossoms in Jiangnan are brewing honey against the warm wind. The pink clouds of flower shadows spread over the white walls and black tiles, and even the air is permeated with a slightly intoxicating sweetness. The colorist of the seasons quietly divides the palette in half - the north is still persistently continuing the end of winter, using icicles as a pen to outline frost flowers on the window panes; the south is already eager to open the first page of spring, letting the swallows fly over the ink - painted alleys with the willow color.
This temperature difference of three thousand miles weaves two silk brocades at both ends of the twilight line: the north is a plain brocade embroidered with silver threads, wrapped with unspoken whispers in the cold; the south is a soft silk dyed by silk - reeling women, rippling with the newborn whispers in the warmth. When the rime in Harbin melts the first drop of brilliance in the morning sun, the magnolias in Hangzhou just shake off the third piece of moonlight on their shoulders. It turns out that spring is stepping on the latitude and longitude lines, composing the most moving polyphony between the harshness and the warmth in the world.
`
ttsEngine?: textToSpeech.TextToSpeechEngine
@Local isPlaying: boolean = false
async initTextToSpeechEngine() {
if (canIUse('SystemCapability.AI.TextToSpeech')) {
const params: textToSpeech.CreateEngineParams = {
language: 'zh-CN',
person: 0,
online: 1
}
this.ttsEngine = await textToSpeech.createEngine(params)
}
}
aboutToAppear(): void {
this.initTextToSpeechEngine()
}
aboutToDisappear(): void {
if (canIUse('SystemCapability.AI.TextToSpeech')) {
this.ttsEngine?.stop()
this.ttsEngine?.shutdown()
}
}
play() {
if (canIUse('SystemCapability.AI.TextToSpeech')) {
if (this.ttsEngine?.isBusy()) {
return promptAction.showToast({ message: 'Playing...' })
}
const params: textToSpeech.SpeakParams = {
requestId: '10000'
}
this.ttsEngine?.speak(this.text, params)
this.isPlaying = true
}
}
build() {
Stack({ alignContent: Alignment.BottomEnd }) {
List() {
ListItem() {
Text(this.text)
.lineHeight(32)
}
}
.padding({ left: 15, right: 15 })
.height('100%')
.width('100%')
Row() {
Image(this.isPlaying ? $r('sys.media.AI_playing') : $r('sys.media.AI_play'))
.width(24)
.onClick(() => this.play())
}
.borderRadius(24)
.shadow({
color: Color.Gray,
offsetX: 5,
offsetY: 5,
radius: 15
})
.width(48)
.aspectRatio(1)
.justifyContent(FlexAlign.Center)
.margin({ right: 50, bottom: 50 })
}
.height('100%')
.width('100%')
}
}