1.下载文件地址https://github.com/aadsm/jsmediatags
2.引用dist下面的jsmediatags.min.js文件
<script src="jsmediatags.min.js"></script>
3.body测试代码
<body>
<form>
<input id="$inputTypeFile" type="file" name="audio" accept="audio/*">
<input type="submit" name="submit" value="add music"/>
</form>
<img src="" alt="" width="500px" height="500px">
</body>
4.js代码
<script>
// From File
$inputTypeFile.addEventListener("change", function(event) {
var file = event.target.files[0];
jsmediatags.read(file, {
onSuccess: function(tag) {
console.log(tag);
var picture = tag.tags.picture; // create reference to track art
var base64String = "";
for (var i = 0; i < picture.data.length; i++) {
base64String += String.fromCharCode(picture.data[i]);
}
var imageUri = "data:" + picture.format + ";base64," + window.btoa(base64String);
document.querySelector('img').src=imageUri;
// use ajax to upload tag info, or create some new form elements
},
onError: function(error) {
console.log(error);
}
});
}, false);
</script>