HarmonyOS鸿蒙Next下逐句歌词&逐字歌词组件
HarmonyOS鸿蒙Next下逐句歌词&逐字歌词组件
效果图
逐行歌词 | 逐字歌词 |
---|---|
![]() |
![]() |
逐句歌词&逐字歌词
每行开头带时间戳的就是逐句歌词
[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
这种就属于逐字歌词之一,每个单词、文字都带时间戳
逐句歌词&逐字歌词组件库: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)
}
状态管理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)
效果图示例代码&仓库地址
更多关于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仓库链接。