文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明
指南与API参考API参考AISpeech Kit(场景化语音服务)ArkTS组件TextReaderIconV2(朗读听筒图标)

TextReaderIconV2(朗读听筒图标)

朗读听筒图标,可以作为动态组件加载,并配置成为播放面板的主入口。

在应用使用ArkTS的状态管理V1装饰器时,需要通过TextReaderIcon组件接口拉起朗读听筒图标;在应用使用状态管理V2装饰器时,需要通过TextReaderIconV2组件接口拉起朗读听筒图标。

起始版本: 6.1.1(24)

导入模块

收起
自动换行
深色代码主题
复制
  1. import { TextReaderIconV2, UpReadState } from '@kit.SpeechKit';

UpReadState

type UpReadState = (readState:ReadStateCode)=>void

用于听筒图标组件触发父组件状态更新的回调函数。

元服务API: 从版本6.1.1(24)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Component.TextReader

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.1.1(24)

参数:

展开
名称 类型 必填 说明
readState ReadStateCode 播报状态。

TextReaderIconV2

朗读听筒图标,可以作为动态组件加载。设置onClick回调,在用户点击听筒图标时启动朗读控件。

装饰器类型: @ComponentV2

元服务API: 从版本6.1.1(24)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Component.TextReader

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.1.1(24)

参数:

展开
名称 类型 必填 装饰器类型 说明
readState ReadStateCode @Param

播报状态。

说明:

readState使用@Param装饰器:父子单向同步

upReadState UpReadState @Event

回调函数,更新播报状态。

说明:

upReadState使用@Event装饰器:子组件通过回调函数触发父组件状态更新

build

build(): void

用于创建TextReaderIconV2对象的构造函数。

元服务API: 从版本6.1.1(24)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Component.TextReader

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.1.1(24)

示例:

收起
自动换行
深色代码主题
复制
  1. import {TextReader,ReadStateCode,TextReaderIconV2, UpReadState} from '@kit.SpeechKit'
  2. @Entry
  3. @ComponentV2
  4. struct Index {
  5. /**
  6. * 待加载的文章
  7. */
  8. @Local readInfoList: TextReader.ReadInfo[] = [];
  9. @Local selectedReadInfo: TextReader.ReadInfo = this.readInfoList[0];
  10. /**
  11. * 播放状态
  12. */
  13. @Local readState: ReadStateCode = ReadStateCode.WAITING;
  14. /**
  15. * 初始化状态
  16. */
  17. @Local isInit: boolean = false;
  18. async aboutToAppear(){
  19. /**
  20. * 加载数据
  21. */
  22. let readInfoList: TextReader.ReadInfo[] = [{
  23. id: '001',
  24. title: {
  25. text:'水调歌头.明月几时有',
  26. isClickable:true
  27. },
  28. author:{
  29. text:'宋.苏轼',
  30. isClickable:true
  31. },
  32. date: {
  33. text:'2024/01/01',
  34. isClickable:false
  35. },
  36. bodyInfo: '明月几时有?把酒问青天。'
  37. }];
  38. this.readInfoList = readInfoList;
  39. this.selectedReadInfo = this.readInfoList[0];
  40. await this.init();
  41. }
  42. /**
  43. * 初始化
  44. */
  45. async init() {
  46. const readerParam: TextReader.ReaderParam = {
  47. isVoiceBrandVisible: true,
  48. businessBrandInfo: {
  49. panelName: '小艺朗读',
  50. panelIcon: $r('app.media.startIcon')
  51. }
  52. };
  53. try {
  54. let context: Context | undefined = this.getUIContext().getHostContext()
  55. if (context) {
  56. await TextReader.init(context, readerParam);
  57. this.isInit = true;
  58. }
  59. } catch (err) {
  60. console.error(`TextReader failed to init. Code: ${err.code}, message: ${err.message}`);
  61. }
  62. }
  63. // 设置操作监听
  64. setActionListener() {
  65. TextReader.on('stateChange', (state: TextReader.ReadState) => {
  66. this.onStateChanged(state);
  67. });
  68. TextReader.on('requestMore', () => {
  69. TextReader.loadMore([], true);
  70. });
  71. }
  72. onStateChanged = (state: TextReader.ReadState) => {
  73. if (this.selectedReadInfo?.id === state.id) {
  74. this.readState = state.state;
  75. } else {
  76. this.readState = ReadStateCode.WAITING;
  77. }
  78. };
  79. updateReadState: UpReadState = (readState: ReadStateCode) => {
  80. this.readState = readState
  81. console.info(`TextReader new readState:${readState}`)
  82. }
  83. build() {
  84. Column() {
  85. TextReaderIconV2({ readState: this.readState,upReadState:this.updateReadState})
  86. .margin({ right: 20 })
  87. .width(32)
  88. .height(32)
  89. .onClick(async () => {
  90. try {
  91. this.setActionListener();
  92. await TextReader.start(this.readInfoList, this.selectedReadInfo?.id);
  93. } catch (err) {
  94. console.error(`TextReader failed to start. Code: ${err.code}, message: ${err.message}`);
  95. }
  96. })
  97. }
  98. .height('100%')
  99. }
  100. }

组件如下图:

静止状态

播放状态