我有个项目要做,用javascript做一个卡拉OK。当这首歌响起时,歌词就得跟着唱。
我的代码html:
<div id="lyric"></div>
<audio id="audioPlayer" controls>
<source src="http://supjs.fr/DEV2018/myKaraoke/music/LOR.mp3" type="audio/mpeg">
</audio>文本文件中的歌词:
00时:00.00Fanuilos heryn aglar
00:10.00R-aearath
00:20.00卡拉德安门I reniar
00:30.00Mi‘’aladhremmin ennorath
00:40.00A Elbereth!吉列尼尔!
00:50.00I ch a th圣保罗
01:00.00Fanuilos!莱恩马拉松
01:09.00Ne ndor haer (我所见)
01:20.00A elin na
01:30.00Ned -n ben-anor trerennin
01:39.00Sisilivrin ne信息素'waewib
01:48.00-塞尼姆·莱思·莱思·萨伊宁
01:58.00A Elbereth Gilthoniel
02:08.00男人的私生子
02:17.00 nu鸡nu‘’aladhath
02:27.00 aearath或ann-aearath
和javascript:
window.onload = function(){
var track = document.getElementById('audioPlayer');
track.ontimeupdate = function(){
console.log(this.currentTime);
if (this.currentTime > 0 && this.curentTime < 9){
getRSS(0);
}
};
};
function getRSS(i) {
var rss = new XMLHttpRequest();
console.log("hello");
rss.open('GET', 'lyrics.txt', false);
console.log("hello1");
rss.send(null);
console.log("hello");
var ligne = rss.responseText.split(/\n/g);/* Stock tout le fichier dans la variable (tableau)*/
console.log("hello");
var linkRss = ligne[i];
document.write(linkRss);
}它不工作,线出现,但音乐停止和音频消失,我不知道发生了什么事情,有人能帮助我吗?
发布于 2016-10-30 16:00:27
您的问题是,您正在用语句覆盖页面。
您必须将文本附加到div元素,而不是整个文档。
document.getElementById('lyric').innerHTML += linkRss;请注意,您将基于时间的代码与异步代码混合在一起,这将是很难处理的,并且不可能与预期的行为一起运行。
发布于 2016-10-30 15:57:54
试一试,也许:新日期().getTime()
https://stackoverflow.com/questions/40330750
复制相似问题