The read receiver icon can be loaded as a dynamic component and configured as the main entry of the playback panel.
Start version: 5.0.0(12)
Read receiver icon, which can be loaded as a dynamic component. The onClick callback is set to start the TextReader when a user taps the receiver icon.
Decorator type: @Component
System capability: SystemCapability.AI.Component.TextReader
Start version: 5.0.0(12)
Parameters:
Name | Type | Decorator Type | Description |
|---|---|---|---|
readState | @Link | Reading status. Note: The ReadState class uses @Link Decorator: Two-Way Synchronization Between Parent and Child Components. | |
build | void | - | Build method. |
import { TextReader, TextReaderIcon, ReadStateCode } from '@kit.SpeechKit';
@Entry
@Component
struct Index {
@State message: string = '';
/**
* Articles to load.
*/
@State readInfoList: TextReader.ReadInfo[] = [];
@State selectedReadInfo: TextReader.ReadInfo = this.readInfoList[0];
/**
* Playback state.
*/
@State readState: ReadStateCode = ReadStateCode.WAITING;
/**
* Display the button status on the current page.
*/
@State isInit: boolean = false;
@State isListening: boolean = false;
async aboutToAppear(){
/**
* Load the data.
*/
console.info(`ReadStateCode: ${JSON.stringify(this.readState)}`);
let readInfoList: TextReader.ReadInfo[] = [{
id: '001',
title: {
text:'Title of the text to read',
isClickable:true
},
author:{
text:'Author of the text to read',
isClickable:true
},
date: {
text:'2024/01/01',
isClickable:false
},
bodyInfo: 'Text body to read'
}];
this.readInfoList = readInfoList;
this.selectedReadInfo = this.readInfoList[0];
this.init();
}
/**
* Initialization
*/
async init() {
const readerParam: TextReader.ReaderParam = {
isVoiceBrandVisible: true,
businessBrandInfo: {
panelName: 'AI Reader',
panelIcon: $r('app.media.startIcon')
}
}
try{
await TextReader.init(getContext(this), readerParam);
this.isInit = true;
} catch (err) {
console.error(`TextReader failed to init. Code: ${err.code}, message: ${err.message}`);
}
}
// Set the operation listener.
setActionListener() {
TextReader.on('setArticle', async (id: string) => { console.info(`setArticle ${id}`) });
TextReader.on('clickArticle', (id: string) => {console.info(`onClickArticle ${id}`) });
TextReader.on('clickAuthor', (id: string) => { console.info(`onClickAuthor ${id}`) });
TextReader.on('clickNotification', (id: string) => { console.info(`onClickNotification ${id}`) });
TextReader.on('showPanel', () => { console.info(`onShowPanel`) });
TextReader.on('hidePanel', () => { console.info(`onHidePanel`) });
TextReader.on('stop', () => { console.info(`onStop`) });
TextReader.on('release', () => { console.info(`onRelease`) });
TextReader.on('stateChange', (state: TextReader.ReadState) => {
this.onStateChanged(state)
});
TextReader.on('requestMore', () => this.onStateChanged);
}
onStateChanged = (state: TextReader.ReadState) => {
if (this.selectedReadInfo?.id === state.id) {
this.readState = state.state;
} else {
this.readState = ReadStateCode.WAITING;
}
}
// Set the event listener.
setEventListener(){
TextReader.on('eventNotification', (event: TextReader.NotificationEvent) => {
console.info(`event: ${JSON.stringify(event)}`)
})
TextReader.on('eventPanel', (event: TextReader.PanelEvent) => {
console.info(`event: ${JSON.stringify(event)}`)
})
TextReader.on('eventReadList', (event: Array<TextReader.ListEventState>) => {
console.info(`event: ${JSON.stringify(event)}`)
})
}
build() {
Column() {
TextReaderIcon({ readState: this.readState })
.margin({ right: 20 })
.width(32)
.height(32)
.onClick(async () => {
try {
this.setActionListener();
this.setEventListener();
await TextReader.start(this.readInfoList, this.selectedReadInfo?.id);
} catch (err) {
console.error(`TextReader failed to start. Code: ${err.code}, message: ${err.message}`);
}
})
}
.height('100%')
}
}