HarmonyOS鸿蒙Next下逐句歌词&逐字歌词组件

HarmonyOS鸿蒙Next下逐句歌词&逐字歌词组件

效果图

逐行歌词 逐字歌词
https://alliance-communityfile-drcn.dbankcdn.com/FileServer/getFile/cmtybbs/300/315/732/0900086000300315732.20250910094435.38871187792931063970663745470643:50001231000000:2800:3244CFDDDD24002F086210729A38336B60CA84A6D2373FFA70AE027101CFE6EF.gif https://alliance-communityfile-drcn.dbankcdn.com/FileServer/getFile/cmtybbs/300/315/732/0900086000300315732.20250910091135.55618749708731932730344277063386:50001231000000:2800:F9739FBD90F8671BC6FA4DD276EE055B3F977D0926B3ACA9977A7E9EB39A5987.gif

逐句歌词&逐字歌词

每行开头带时间戳的就是逐句歌词

[00:08.598]I will run I will climb I will soar
[00:12.753]I'm undefeated
[00:16.540]Jumping out of my skin pull the chord
[00:20.732]Yeah I believe it
[00:23.703]The past is everything we were don't make us who we are
[00:31.500]So I'll dream until I make it real and all I see is stars
[00:38.744]It's not until you fall that you fly
[00:43.630]When your dreams come alive you're unstoppable
[00:47.498]Take a shot chase the sun find the beautiful
[00:51.558]We will glow in the dark turning dust to gold
[00:55.473]And we'll dream it possible
[01:03.043]Possible

这种就属于逐字歌词之一,每个单词、文字都带时间戳

https://alliance-communityfile-drcn.dbankcdn.com/FileServer/getFile/cmtybbs/300/315/732/0900086000300315732.20250910093234.99465025292155459751400438316225:50001231000000:2800:93F980AA4A16C7634E9E2961E190B87CE86D2DBDD139493945844DA2F3C8DCF6.png

逐句歌词&逐字歌词组件库:lyric-view

安装

ohpm install [@jackiehou](/user/jackiehou)/lyric-view

解析逐句歌词&逐字歌词

import { LrcLine,  parseLyrics} from '[@jackiehou](/user/jackiehou)/lyric-view'

@Entry
@ComponentV2
struct SamplePageV2 {
  @Local lrcLines: LrcLine[] = []
    
  async aboutToAppear() {
    this.lrcLines = parseLyrics(lrcString)
  }
}

基础用法,推荐使用状态管理V2

import { LrcLine, LrcPosition, LrcView, parseLyrics} from '[@jackiehou](/user/jackiehou)/lyric-view'
  @Local lrcLines: LrcLine[] = []
  @Local isPlay:boolean = true
  player:media.AVPlayer|undefined
  lrcPosition :LrcPosition = new LrcPosition(() =>this.player?.currentTime ?? 0)

  async aboutToAppear() {
    this.lrcLines = parseLyrics(lrcString)
  }

  build() {
    Column({space:10}) {
      LrcView({
        lrcLines:this.lrcLines,
        isPlay:this.isPlay,
        lrcPosition:this.lrcPosition,
        normalFontColor:$r('sys.color.font_secondary'),
        selectFontColor:$r('sys.color.font_primary'),
        highlightFontColor:$r('sys.color.font_emphasize'),
        onLrcLineClick:(start) =>{
          this.player?.seek(start)
          this.lrcPosition.seekTo(start)
          return true
        },
      })
        .width('100%')
        .layoutWeight(1)

      SymbolGlyph(this.isPlay ? $r('sys.symbol.pause_circle') :$r('sys.symbol.play_circle'))
        .fontSize(50)
        .fontColor([$r('sys.color.font_primary')])
        .onClick(() =>{
          this.isPlay = !this.isPlay
        })
    }
    .height('100%')
    .width('100%')
    .padding(15)
  }

https://alliance-communityfile-drcn.dbankcdn.com/FileServer/getFile/cmtybbs/300/315/732/0900086000300315732.20250910094822.67722264811253830733235056784954:50001231000000:2800:6CCF1381DABDAF7C7F0710E8425DE8A1EB230A551DF7686FFAD3383D53C79CE0.jpg

状态管理V1可参考

https://gitee.com/jackiehou/lyric-view/blob/master/entry/src/main/ets/pages/SamplePage.ets

高度只有两行的逐字歌词

LrcView({
  lrcLines: this.lrcLines,
  isPlay: this.isPlay,
  lrcPosition: this.lrcPosition,
  fadingEdgeFraction: 0,//无渐隐效果
  contentStartOffset: 0,
  font: middleFont,
  maxFont: middleFont,
  normalFontColor: $r('sys.color.font_secondary'),
  selectFontColor: $r('sys.color.font_primary'),
  highlightFontColor: $r('sys.color.font_emphasize'),
  isSingleLine: true,//短行歌词效果
  enableScroll: false//禁用滑动
})
  .width('100%')
  .height(middleFont.lineHeight * 2 + 10)

https://alliance-communityfile-drcn.dbankcdn.com/FileServer/getFile/cmtybbs/300/315/732/0900086000300315732.20250910130012.90740607002265782510363406521200:50001231000000:2800:E7DB90B5D65E26D7431E807718E9B20A2E03FEAEFB8AE770406BFDB1827E5A19.gif

效果图示例代码&仓库地址

https://gitee.com/jackiehou/lyric-view


更多关于HarmonyOS鸿蒙Next下逐句歌词&逐字歌词组件的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

安装了,很好用👍

更多关于HarmonyOS鸿蒙Next下逐句歌词&逐字歌词组件的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


鸿蒙Next中逐句歌词和逐字歌词组件基于ArkUI框架开发,使用Text组件结合动画能力实现。逐句歌词通过分段文本与定时器控制切换,逐字歌词需借助Text组件的onAppear及属性动画逐字渲染。推荐使用Lottie或自定义绘制方案实现高级特效。数据层可通过媒体会话服务获取歌词并解析为时间轴文本数组。

在HarmonyOS Next中实现逐句歌词和逐字歌词功能,使用lyric-view组件库是一个高效的选择。该组件支持LRC格式的歌词解析,能够根据时间戳动态高亮显示当前播放的歌词行或单词。

通过parseLyrics方法解析歌词字符串后,结合LrcView组件进行渲染。组件支持自定义字体颜色、播放状态同步以及点击跳转功能。对于逐字歌词,确保歌词源包含每个单词的时间戳,组件会自动处理逐字高亮效果。

使用状态管理V2(如示例中的@Local装饰器)可以优化性能,确保歌词与播放进度实时同步。禁用滚动和设置单行模式适合紧凑布局,如锁屏界面或迷你播放器。

示例代码已展示基础集成方法,更多高级配置可参考提供的Gitee仓库链接。

回到顶部