如何从JavaScript中的MP3文件读取元数据属性?
我知道之前有人问过类似的问题,但所有的答案通常都涉及两件事中的一件,这两件事我都想避免:如何从JavaScript中的MP3文件读取元数据属性?,javascript,mp3,Javascript,Mp3,我知道之前有人问过类似的问题,但所有的答案通常都涉及两件事中的一件,这两件事我都想避免: 导入新库,或 据我所知,使用FileReader只对用户输入的文件有效 我正在做一个小游戏,包括选项菜单中的几个音乐曲目之间的切换选项,我想能够显示曲目标题和艺术家。我可以调整我的song对象,以便在代码中输入标题、艺术家等信息,但将其作为元数据包含在文件中似乎更简单,这样我只需输入文件名。有没有什么方法可以在不使用额外库的情况下从用户未输入的文件中读取元数据?下面是我正在使用的Song对象,尽管它非常简单
var Song = function(filename)
{
this.intro = new Audio(); // the introduction of the song that leads into a loop
this.intro.src = filename + "-intro.mp3";
var temp = this;
this.intro.addEventListener("ended", function() {
this.currentTime = 0;
temp.loop.currentTime = 0;
temp.loop.play();
});
this.loop = new Audio(); // the main file that will be looped. Also has the relevant metadata
this.loop.src = filename + ".mp3";
this.loop.addEventListener("ended", function() {
this.currentTime = 0;
this.play();
});
};
var songs = [
new Song("track1"),
new Song("track2"),
new Song("track3")
];
为此使用mutag
在html中添加:
在您的js中添加:
const mutag=window.mutag代码>
并使用变量filename
mutag.fetch(filename).then((tags) => {
console.log(tags);
});
我尝试使用源字符串和音频对象,但对于这两个对象,它都表示,“未能在'FileReader'上执行'readAsArrayBuffer':参数1不是'Blob'类型。”哦,您将必须grt文件Blob,一旦我得到它,我将对此提供帮助home@KRLW890快速提问:您的应用程序是web应用程序还是js应用程序?你也在这里使用nodejs吗?我没有使用Node.js,而且我很确定这是一个js应用程序(有点新手,所以我不能100%确定区别是什么)。但是我通过合并您提供的代码设法找到了一些有用的东西:var getTags=function(audio){fetch(audio.src).then(response=>response.blob()).then(mutag.fetch).then((tags)=>{console.log(tags);})代码>