跳转到内容

DomSlimLyricPlayer

歌词播放组件,本框架的核心组件

尽可能贴切 Apple Music for iPad 的歌词效果设计,且做了力所能及的优化措施

Extends

Constructors

new DomSlimLyricPlayer()

new DomSlimLyricPlayer(): DomSlimLyricPlayer

Returns

DomSlimLyricPlayer

Overrides

LyricPlayerBase.constructor

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:87

Properties

currentLyricLineObjects

currentLyricLineObjects: LyricLineEl[] = []

Overrides

LyricPlayerBase.currentLyricLineObjects

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:21


innerSize

readonly innerSize: [number, number]

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:61


resizeObserver

resizeObserver: ResizeObserver

Inherited from

LyricPlayerBase.resizeObserver

Defined in

packages/core/src/lyric-player/base.ts:86


size

readonly size: [number, number]

Inherited from

LyricPlayerBase.size

Defined in

packages/core/src/lyric-player/base.ts:50


supportMaskImage

readonly supportMaskImage: boolean

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:60


supportPlusLighter

readonly supportPlusLighter: boolean

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:59

Accessors

baseFontSize

Get Signature

get baseFontSize(): number

Returns

number

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:84

Methods

addEventListener()

addEventListener(type, callback, options?): void

Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.

The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options’s capture.

When set to true, options’s capture prevents callback from being invoked when the event’s eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event’s eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event’s eventPhase attribute value is AT_TARGET.

When set to true, options’s passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.

When set to true, options’s once indicates that the callback will only be invoked once after which the event listener will be removed.

If an AbortSignal is passed for options’s signal, then the event listener will be removed when signal is aborted.

The event listener is appended to target’s event listener list and is not appended if it has the same type, callback, and capture.

MDN Reference

Parameters

type: string

callback: null | EventListenerOrEventListenerObject

options?: boolean | AddEventListenerOptions

Returns

void

Inherited from

LyricPlayerBase.addEventListener

Defined in

node_modules/.pnpm/typescript@5.6.3/node_modules/typescript/lib/lib.dom.d.ts:8303


calcLayout()

calcLayout(force?, reflow?): Promise<void>

重新布局定位歌词行的位置,调用完成后再逐帧调用 update 函数即可让歌词通过动画移动到目标位置。

函数有一个 force 参数,用于指定是否强制修改布局,也就是不经过动画直接调整元素位置和大小。

此函数还有一个 reflow 参数,用于指定是否需要重新计算布局

因为计算布局必定会导致浏览器重排布局,所以会大幅度影响流畅度和性能,故请只在以下情况下将其​设置为 true:

  1. 歌词页面大小发生改变时(这个组件会自行处理)
  2. 加载了新的歌词时(不论前后歌词是否完全一样)
  3. 用户自行跳转了歌曲播放位置(不论距离远近)

Parameters

force?: boolean

是否不经过动画直接修改布局定位

reflow?: boolean

是否进行重新布局(重新计算每行歌词大小)

Returns

Promise<void>

Overrides

LyricPlayerBase.calcLayout

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:179


dispatchEvent()

dispatchEvent(event): boolean

Dispatches a synthetic event event to target and returns true if either event’s cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

MDN Reference

Parameters

event: Event

Returns

boolean

Inherited from

LyricPlayerBase.dispatchEvent

Defined in

node_modules/.pnpm/typescript@5.6.3/node_modules/typescript/lib/lib.dom.d.ts:8309


dispose()

dispose(): void

销毁实现了该接口的对象实例,释放占用的资源

一般情况下,调用本函数后就不可以再调用对象的任何函数了

Returns

void

Overrides

LyricPlayerBase.dispose

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:215


getBottomLineElement()

getBottomLineElement(): HTMLElement

获取一个特殊的底栏元素,默认是空白的,可以往内部添加任意元素

这个元素始终在歌词的底部,可以用于显示歌曲创作者等信息

但是请勿删除该元素,只能在内部存放元素

Returns

HTMLElement

一个元素,可以往内部添加任意元素

Inherited from

LyricPlayerBase.getBottomLineElement

Defined in

packages/core/src/lyric-player/base.ts:819


getCurrentTime()

getCurrentTime(): number

获取当前歌词的播放位置

一般和最后调用 setCurrentTime 给予的参数一样

Returns

number

当前播放位置

Inherited from

LyricPlayerBase.getCurrentTime

Defined in

packages/core/src/lyric-player/base.ts:848


getElement()

getElement(): HTMLElement

获取这个类所对应的 HTML 元素实例

Returns

HTMLElement

Inherited from

LyricPlayerBase.getElement

Defined in

packages/core/src/lyric-player/base.ts:852


getEnableScale()

getEnableScale(): boolean

获取当前是否启用了歌词行缩放效果

Returns

boolean

是否启用歌词行缩放效果

Inherited from

LyricPlayerBase.getEnableScale

Defined in

packages/core/src/lyric-player/base.ts:245


getEnableSpring()

getEnableSpring(): boolean

获取当前是否启用了物理弹簧

Returns

boolean

是否启用物理弹簧

Inherited from

LyricPlayerBase.getEnableSpring

Defined in

packages/core/src/lyric-player/base.ts:315


getLyricLines()

getLyricLines(): LyricLine[]

获取当前歌词数组

一般和最后调用 setLyricLines 给予的参数一样

Returns

LyricLine[]

当前歌词数组

Inherited from

LyricPlayerBase.getLyricLines

Defined in

packages/core/src/lyric-player/base.ts:839


getWordFadeWidth()

getWordFadeWidth(): number

获取当前文字动画的渐变宽度,单位以歌词行的主文字字体大小的倍数为单位

Returns

number

当前文字动画的渐变宽度,单位以歌词行的主文字字体大小的倍数为单位

Inherited from

LyricPlayerBase.getWordFadeWidth

Defined in

packages/core/src/lyric-player/base.ts:253


onResize()

onResize(): void

Returns

void

Overrides

LyricPlayerBase.onResize

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:38


pause()

pause(): void

暂停部分效果演出,目前会暂停播放间奏点的动画

Returns

void

Overrides

LyricPlayerBase.pause

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:151


removeEventListener()

removeEventListener(type, callback, options?): void

Removes the event listener in target’s event listener list with the same type, callback, and options.

MDN Reference

Parameters

type: string

callback: null | EventListenerOrEventListenerObject

options?: boolean | EventListenerOptions

Returns

void

Inherited from

LyricPlayerBase.removeEventListener

Defined in

node_modules/.pnpm/typescript@5.6.3/node_modules/typescript/lib/lib.dom.d.ts:8315


resetScroll()

resetScroll(): void

重置用户滚动状态

请在用户完成滚动点击跳转歌词时调用本事件再调用 calcLayout 以正确滚动到目标位置

Returns

void

Inherited from

LyricPlayerBase.resetScroll

Defined in

packages/core/src/lyric-player/base.ts:827


resume()

resume(): void

恢复部分效果演出,目前会恢复播放间奏点的动画

Returns

void

Overrides

LyricPlayerBase.resume

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:159


setAlignAnchor()

setAlignAnchor(alignAnchor): void

设置目标歌词行的对齐方式,默认为 center

  • 设置成 top 的话将会向目标歌词行的顶部对齐
  • 设置成 bottom 的话将会向目标歌词行的底部对齐
  • 设置成 center 的话将会向目标歌词行的垂直中心对齐

Parameters

alignAnchor: "center" | "bottom" | "top"

歌词行对齐方式,详情见函数说明

Returns

void

Inherited from

LyricPlayerBase.setAlignAnchor

Defined in

packages/core/src/lyric-player/base.ts:285


setAlignPosition()

setAlignPosition(alignPosition): void

设置默认的歌词行对齐位置,相对于整个歌词播放组件的大小位置,默认为 0.5

Parameters

alignPosition: number

一个 [0.0-1.0] 之间的任意数字,代表组件高度由上到下的比例位置

Returns

void

Inherited from

LyricPlayerBase.setAlignPosition

Defined in

packages/core/src/lyric-player/base.ts:292


setCurrentTime()

setCurrentTime(time, isSeek): void

设置当前播放进度,单位为毫秒且必须是整数,此时将会更新内部的歌词进度信息 内部会根据调用间隔和播放进度自动决定如何滚动和显示歌词,所以这个的调用频率越快越准确越好

调用完成后,可以每帧调用 update 函数来执行歌词动画效果

Parameters

time: number

当前播放进度,单位为毫秒

isSeek: boolean = false

Returns

void

Inherited from

LyricPlayerBase.setCurrentTime

Defined in

packages/core/src/lyric-player/base.ts:457


setEnableBlur()

setEnableBlur(enable): void

设置是否启用歌词行的模糊效果

Parameters

enable: boolean

是否启用

Returns

void

Inherited from

LyricPlayerBase.setEnableBlur

Defined in

packages/core/src/lyric-player/base.ts:272


setEnableScale()

setEnableScale(enable): void

是否启用歌词行缩放效果,默认启用

如果启用,非选中的歌词行会轻微缩小以凸显当前播放歌词行效果

此效果对性能影响微乎其微,推荐启用

Parameters

enable: boolean = true

是否启用歌词行缩放效果

Returns

void

Inherited from

LyricPlayerBase.setEnableScale

Defined in

packages/core/src/lyric-player/base.ts:237


setEnableSpring()

setEnableSpring(enable): void

设置是否使用物理弹簧算法实现歌词动画效果,默认启用

如果启用,则会通过弹簧算法实时处理歌词位置,但是需要性能足够强劲的电脑方可流畅运行

如果不启用,则会回退到基于 transition 的过渡效果,对低性能的机器比较友好,但是效果会比较单一

Parameters

enable: boolean = true

Returns

void

Inherited from

LyricPlayerBase.setEnableSpring

Defined in

packages/core/src/lyric-player/base.ts:302


setHidePassedLines()

setHidePassedLines(hide): void

设置是否隐藏已经播放过的歌词行,默认不隐藏

Parameters

hide: boolean

是否隐藏已经播放过的歌词行,默认不隐藏

Returns

void

Inherited from

LyricPlayerBase.setHidePassedLines

Defined in

packages/core/src/lyric-player/base.ts:264


setIsSeeking()

setIsSeeking(isSeeking): void

Parameters

isSeeking: boolean

Returns

void

Inherited from

LyricPlayerBase.setIsSeeking

Defined in

packages/core/src/lyric-player/base.ts:257


setLinePosXSpringParams()

setLinePosXSpringParams(_params): void

设置所有歌词行在横坐标上的弹簧属性,包括重量、弹力和阻力。

Parameters

_params: Partial<SpringParams> = {}

Returns

void

Inherited from

LyricPlayerBase.setLinePosXSpringParams

Defined in

packages/core/src/lyric-player/base.ts:746


setLinePosYSpringParams()

setLinePosYSpringParams(params): void

设置所有歌词行在​纵坐标上的弹簧属性,包括重量、弹力和阻力。

Parameters

params: Partial<SpringParams> = {}

需要设置的弹簧属性,提供的属性将会覆盖原来的属性,未提供的属性将会保持原样

Returns

void

Inherited from

LyricPlayerBase.setLinePosYSpringParams

Defined in

packages/core/src/lyric-player/base.ts:752


setLineScaleSpringParams()

setLineScaleSpringParams(params): void

设置所有歌词行在​缩放大小上的弹簧属性,包括重量、弹力和阻力。

Parameters

params: Partial<SpringParams> = {}

需要设置的弹簧属性,提供的属性将会覆盖原来的属性,未提供的属性将会保持原样

Returns

void

Inherited from

LyricPlayerBase.setLineScaleSpringParams

Defined in

packages/core/src/lyric-player/base.ts:767


setLyricLines()

setLyricLines(lines, initialTime): void

设置当前播放歌词,要注意传入后这个数组内的信息不得修改,否则会发生错误

Parameters

lines: LyricLine[]

歌词数组

initialTime: number = 0

初始时间,默认为 0

Returns

void

Overrides

LyricPlayerBase.setLyricLines

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:118


setWordFadeWidth()

setWordFadeWidth(value): void

设置文字动画的渐变宽度,单位以歌词行的主文字字体大小的倍数为单位,默认为 0.5,即一个全角字符的一半宽度

如果要模拟 Apple Music for Android 的效果,可以设置为 1

如果要模拟 Apple Music for iPad 的效果,可以设置为 0.5

如果想要近乎禁用渐变效果,可以设置成非常接近 0 的小数(例如 0.0001 ),但是不可以为 0

Parameters

value: number = 0.5

需要设置的渐变宽度,单位以歌词行的主文字字体大小的倍数为单位,默认为 0.5

Returns

void

Overrides

LyricPlayerBase.setWordFadeWidth

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:106


update()

update(delta): void

更新动画,这个函数应该被逐帧调用或者在以下情况下调用一次:

  1. 刚刚调用完设置歌词函数的时候

Parameters

delta: number = 0

距离上一次被调用到现在的时长,单位为毫秒(可为浮点数)

Returns

void

Overrides

LyricPlayerBase.update

Defined in

packages/core/src/lyric-player/dom-slim/index.ts:167