Skip to content

Commit

Permalink
新增播控中心歌词支持。
Browse files Browse the repository at this point in the history
  • Loading branch information
shanyan-wcx committed Oct 17, 2024
1 parent efacf22 commit 094d4a6
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion entry/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,29 @@ struct Index {
}
}

convertToLrc(lyrics: StructuredLyric | null): string {
if (!lyrics || !lyrics.line || lyrics.line.length === 0) {
return "";
}
const lrcLines: string[] = [];
const addedTimestamps = new Set<string>();
lyrics.line.forEach(line => {
if (line.start !== undefined) {
const minutes = Math.floor(line.start / 60000).toString().padStart(2, '0');
const seconds = ((line.start % 60000) / 1000).toFixed(2);
let timeTag = `[${minutes}:${seconds.padStart(5, '0')}]`;
if (timeTag === '[00:00.00]') {
timeTag = '[00:00.01]';
}
if (!addedTimestamps.has(timeTag)) {
lrcLines.push(`${timeTag}${line.value}`);
addedTimestamps.add(timeTag);
}
}
});
return lrcLines.join("\n") + "\n";
}

getPixelMap(cover: ArrayBuffer): PixelMap {
const imageSource: image.ImageSource = image.createImageSource(cover);
let pixelMap = imageSource.createPixelMapSync()
Expand Down Expand Up @@ -271,7 +294,8 @@ struct Index {
title: this.nowPlayingSong!.title,
mediaImage: this.getPixelMap(await getCover(this.nowPlayingSong!.id, 512)),
duration: this.nowPlayingSong!.duration * 1000,
artist: this.nowPlayingSong!.artist
artist: this.nowPlayingSong!.artist,
lyric: this.convertToLrc(this.nowPlayingLyrics),
};
this.session!.setAVMetadata(metadata).then(() => {
console.info(`SetAVMetadata successfully`);
Expand Down

0 comments on commit 094d4a6

Please sign in to comment.