吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 373|回复: 14
收起左侧

[已解决] 【HTML】歌词不显示

[复制链接]
hdxzd12 发表于 2025-3-16 14:24
本帖最后由 hdxzd12 于 2025-3-16 19:06 编辑

现在用HTML做了一个带歌词的播放器,但是播放过程中不显示歌词,这个问题应该如何解决
播放器的全部文件:链接: https://pan.baidu.com/s/13hRVDqWRZ-ZOn2zZEZTYLg?pwd=9999 提取码: 9999 复制这段内容后打开百度网盘手机App,操作更方便哦
代码:
[HTML] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>MIDI Player</title>
        <script src='midijs/midi.js'></script>
        <script src="midijs/jquery.min.js"></script>
        <style>
                html {
                        background: silver;
                }
 
                body {
                        overflow: hidden;
                        width: 100%;
                        height: 100%;
                        margin: 0;
                        font-family: Arial, sans-serif;
                }
 
                ul {
                        padding: 0;
                        margin: 0;
                        display: none;
                }
 
                li {
                        padding: 10px;
                        cursor: pointer;
                        list-style: none;
                        background-color: #f9f9f9;
                        border-bottom: 1px solid #ddd;
                }
 
                #playlist > ul > li:nth-child(2n) {
                        background-color: #eee;
                }
 
                li:hover,
                #playlist > ul > li:nth-child(2n):hover {
                        background: #C3FF00;
                        color: #000;
                }
 
                #folders {
                        width: 100%;
                        color: black;
                        position: fixed;
                        top: 30px;
                        left: 0;
                        padding: 7px;
                        background: darkgray;
                        height: 36px;
                        border-bottom: 1px solid gray;
                }
 
                #console {
                        width: 100%;
                        background: black;
                        color: #C3FF00;
                        border: 5px solid black;
                        position: fixed;
                        top: 0;
                        left: 0;
                        height: 30px;
                        padding: 3px;
                        line-height: 30px;
                        font-size: 14px;
                }
 
                #console:hover {
                        background: red;
                        border: 5px solid red;
                        cursor: not-allowed;
                }
 
                #playlist {
                        position: absolute;
                        top: 67px;
                        background: white;
                        left: 0;
                        width: 100%;
                        height: calc(100% - 67px);
                        overflow-y: scroll;
                        overflow-x: hidden;
                }
 
                .folder {
                        cursor: pointer;
                        text-align: center;
                        padding: 5px 10px;
                        background: #ccc;
                        border: 1px solid #999;
                        margin-right: 5px;
                }
 
                .folder:hover {
                        background: #C3FF00;
                        color: #000;
                }
 
                button.fClicked {
                        background-color: gray;
                        color: #dfdfdf;
                        border: 1px solid gray;
                        box-shadow: inset 1px 1px #000, 1px 0 #dfdfdf, 0 1px #dfdfdf, 1px 1px #dfdfdf;
                }
 
                #download {
                        display: none;
                        padding: 5px 10px;
                        background: #ccc;
                        border: 1px solid #999;
                        cursor: pointer;
                }
 
                .playing {
                        background: #C3FF00 !important;
                        color: #000 !important;
                }
                 
                 
                                /* 原有样式保持不变,新增以下歌词相关样式 */
                #console {
                        display: flex;
                        flex-direction: column;
                        height: 60px; /* 增加高度以显示歌词 */
                }
                #status-bar {
                        height: 30px;
                        line-height: 30px;
                }
                #lyric-container {
                        flex: 1;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                        padding: 0 5px;
                }
        </style>
</head>
<body>
        <div id='console'>
                <div id="status-bar">Choose a song.</div>
                <div id="lyric-container"></div>
        </div>
        <div id='folders'>
                <button id='download'>Download</button>
        </div>
        <div id="playlist"></div>
 
        <script>
                // 新增歌词相关变量
                let lyrics = [];
                let timeStamps = [];
                let lyricIndex = 0;
                let playbackStartTime = 0;
                let lyricInterval;
                var current = 0;
                var midiFile = "";
 
                $(function () {
                        // Load song list from song-list.txt
                        $.get('song-list.txt', function (data) {
                                var folders = {};
                                var lines = data.split('\n');
 
                                // Parse song list
                                lines.forEach(function (line) {
                                        if (line.trim() === "") return;
                                        var parts = line.split('/');
                                        if (parts.length === 2) {
                                                var folderId = parts[0];
                                                var songName = parts[1].trim();
                                                if (!folders[folderId]) {
                                                        folders[folderId] = [];
                                                }
                                                folders[folderId].push(songName);
                                        }
                                });
 
                                // Generate folders and songs
                                Object.keys(folders).forEach(function (folderId) {
                                        $('#folders').prepend(
                                                $('<button>').addClass('folder').text(folderId)
                                                        .data('folderId', folderId)
                                        );
 
                                        var $ul = $('<ul>').attr('id', 'f_' + folderId).hide();
                                        folders[folderId].forEach(function (song) {
                                                $ul.append($('<li>').addClass('song').text(song));
                                        });
                                        $('#playlist').append($ul);
                                });
                        });
 
 
                        // Folder click handler
                        $('#folders').on('click', '.folder', function () {
                                var folderId = $(this).data('folderId');
                                var $ul = $('#f_' + folderId);
                                $ul.slideToggle(function () {
                                        $(this).toggleClass('fClicked', $ul.is(':visible'));
                                });
                        });
 
                        // Song click handler
                        $('#playlist').on('click', '.song', function () {
                                lyricIndex = 0;
                                clearTimeout(window.playbackTimeout);
                                MIDIjs.stop();
                                clearInterval(lyricInterval);
                                $('#lyric-container').text('');
                                var $song = $(this);
                                var folderId = $song.closest('ul').attr('id').replace('f_', '');
                                midiFile = 'songs/' + folderId + '/' + $song.text();
 
                                $('.song').removeClass('playing');
                                $song.addClass('playing');
                                $('#download').show();
                                $('#console').text('Loading: ' + $song.text() + '...');
                                current = $('.song').index($song);
 
                                // 加载歌词文件
                                const lrcFile = midiFile.replace(/\.mid$/, '.lrc');
                                fetch(lrcFile)
                                        .then(response => response.text())
                                        .then(parseLRC)
                                        .catch(() => console.log('No lyrics found'));
 
                                // 更新状态显示
                                $('#status-bar').text('Loading: ' + $song.text() + '...');
                         
                                MIDIjs.play(midiFile);
                                playbackStartTime = Date.now();
                         
                                // 启动歌词更新
                                lyricInterval = setInterval(updateLyrics, 100);
 
                                // Playback ended handler
                                MIDIjs.onended = function () {
                                        next(current + 1);
                                };                               
                                 
                        });
 
                        // Console click handler
                        $('#console').click(function () {
                                MIDIjs.stop();
                                clearInterval(lyricInterval);
                                $('#lyric-container').text('');
                                $('#console').text('Playback stopped');
                                $('.playing').removeClass('playing');
                                setTimeout(function () {
                                        $('#console').text('Choose a song');
                                }, 2000);
                        });
 
                        // Download handler
                        $('#download').click(function () {
                                window.location.href = midiFile;
                        });
                });
                 
                function parseLRC(lrcContent) {
                        lyrics = [];
                        timeStamps = [];
                        const lines = lrcContent.split('\n');
                         
                        lines.forEach(line => {
                                const matches = line.match(/\[(\d{2}):(\d{2})\.(\d{2,3})\](.*)/);
                                if (matches) {
                                        const min = parseInt(matches[1]);
                                        const sec = parseInt(matches[2]);
                                        const ms = parseInt(matches[3].padEnd(3, '0'));
                                        const time = min * 60 + sec + ms / 1000;
                                        timeStamps.push(time);
                                        lyrics.push(matches[4].trim());
                                }
                        });
                }
 
                function updateLyrics() {
                        if (!playbackStartTime) return;
                         
                        const elapsed = (Date.now() - playbackStartTime) / 1000;
                        while (lyricIndex < timeStamps.length && timeStamps[lyricIndex] <= elapsed) {
                                lyricIndex++;
                        }
                         
                        const currentLyric = lyricIndex > 0 ? lyrics[lyricIndex - 1] : '';
                        $('#lyric-container').text(currentLyric);
                }
 
                function next(index) {
                        var $songs = $('.song');
                        if (index >= $songs.length) index = 0;
                        if (index < 0) index = $songs.length - 1;
                        $songs.eq(index).trigger('click');
                }
        </script>
</body>
</html>

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

FitContent 发表于 2025-3-16 17:10

我直接说原因,代码被压缩了要花时间改动,更多的还需要楼主改动。

在最初的 index.html 文件中,有这样一段代码:

<div id='console'>
        <div id="status-bar">Choose a song.</div>
        <div id="lyric-container"></div>
</div>

歌词内容会写入到 #lyric-containerdiv
但是在播放歌曲的时候,midi.js 中直接修改了 #console 这个 div 的内容,比如:$('#console').text(e) 等还有其它的操作。

这就导致调用 updateLyrics 方法写入歌词时失败 —— 这个写入歌词的 div 不见了。
对应的写入歌词代码为:$('#lyric-container').text(currentLyric)

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
hdxzd12 + 1 + 1 我很赞同!
liyitong + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

FitContent 发表于 2025-3-16 18:21
hdxzd12 发表于 2025-3-16 18:14
那也就是说把这个移动到除了CONSOLE这个DIV的其他的地方就可以解决问题吗

是的,移动的时候注意样式问题。因为页面中其它的部分元素使用 position 定位,你会发现把显示单词的 div 移动放到其它位置之后,在页面中看不到,需要调整一下其它元素的样式,这个你让 deppseek 等 AI 操作一下就好。

免费评分

参与人数 1吾爱币 +2 热心值 +1 收起 理由
hdxzd12 + 2 + 1 感谢您的宝贵建议,我们会努力争取做得更好!

查看全部评分

cnJingfeng 发表于 2025-3-16 14:32
可能是歌词文件没加载、歌词解析失败、歌词容器的问题。
 楼主| hdxzd12 发表于 2025-3-16 14:45
本帖最后由 hdxzd12 于 2025-3-16 14:46 编辑
cnJingfeng 发表于 2025-3-16 14:32
可能是歌词文件没加载、歌词解析失败、歌词容器的问题。

可能不是歌词文件没加载、歌词解析失败,因为HTTP的请求状态就是200-OK'
image.png


不知道问题和这个中控台有没有关系
midi.js:53  [Deprecation]The ScriptProcessorNode is deprecated. Use AudioWorkletNode instead. (https://bit.ly/audio-worklet)
o.onload @ midi.js:53
midi.js:241  Uncaught ReferenceError: nextTimeout is not defined
    at midi.js:241:42
    at h (midi.js:194:87)
    at i.onload (midi.js:109:21)
(匿名) @ midi.js:241
h @ midi.js:194
i.onload @ midi.js:109
[新] 使用 Edge 中的 Copilot 来解释控制台错误: 单击
         
         以说明错误。
        了解更多信息
        不再显示

Pablo 发表于 2025-3-16 14:47
检查歌词文件路径和存在性:确保与每个 MIDI 文件对应的 .lrc 歌词文件存在,且路径正确。

验证歌词文件格式:确保歌词文件内容符合标准的 LRC 格式,便于正确解析。

调试 parseLRC 函数:在该函数内添加日志输出,确认歌词文件内容被正确解析。

调试 updateLyrics 函数:确保该函数能够正确更新歌词显示区域的内容。

验证定时器启动情况:确保 setInterval 成功启动,且 updateLyrics 函数被定期调用。
mojue 发表于 2025-3-16 16:00
Pablo 发表于 2025-3-16 14:47
检查歌词文件路径和存在性:确保与每个 MIDI 文件对应的 .lrc 歌词文件存在,且路径正确。

验证歌词文件 ...

狠狠的顶一下
天才笨蜀黍 发表于 2025-3-16 16:30
lrc的编码问题?
 楼主| hdxzd12 发表于 2025-3-16 16:44

编码应该是什么,
。。现在是UTF8
 楼主| hdxzd12 发表于 2025-3-16 18:14
FitContent 发表于 2025-3-16 17:10
[md]
我直接说原因,代码被压缩了要花时间改动,更多的还需要楼主改动。

那也就是说把这个<div id="lyric-container"></div>移动到除了CONSOLE这个DIV的其他的地方就可以解决问题吗
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-23 10:23

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表