[英]ID3 - base64 cannot set as src for img
I just came the ID3 API across to get the cover of an audio file. 我刚遇到ID3 API来获取音频文件的封面。 The base64 which is created by the API really good cannot be declarated in a
img
tag's src
. 由API创建的base64确实不错,无法在
img
标签的src
声明。 The script don't even try to set a new src
for the img
tag. 该脚本甚至不会尝试为
img
标签设置新的src
。
https://github.com/aadsm/JavaScript-ID3-Reader https://github.com/aadsm/JavaScript-ID3-Reader
Here is my try: 这是我的尝试:
<a class="uploaded_file"><img src="/symbol/audio.png"></a>
function getAudioCover(url) {
var tags = ID3.getAllTags(url);
var image = tags.picture;
if (image) {
var base64String = "";
for (var i = 0; i < image.data.length; i++) {
base64String += String.fromCharCode(image.data[i]);
}
var base64 = "data:" + image.format + ";base64," +
window.btoa(base64String);
return base64;
}
else {
return "/symbol/audio.png";
}
}
var uploadedFiles = 0;
var file_cover;
ID3.loadTags("audio.mp3", function() {
file_cover = getAudioCover("audio.mp3");
console.log(file_cover);
}, {
tags: ["picture"]
});
$('.uploaded_file:eq(' + uploadedFiles + ')').find('img').attr("src", file_cover);
The console gives me this out: https://jsfiddle.net/f1wzbtyh/ 控制台向我提供了这一点: https : //jsfiddle.net/f1wzbtyh/
How do I get this working? 我该如何工作?
Is there a overflow of string length for jquery or for src attribute? jQuery或src属性是否存在字符串长度溢出?
Place call to .attr()
within callback function. 在回调函数中进行对
.attr()
的调用。 file_cover
may not be defined when referenced outside of .loadTags
call. file_cover
时以外引用可以不定义.loadTags
呼叫。
ID3.loadTags("audio.mp3", function() {
file_cover = getAudioCover("audio.mp3");
console.log(file_cover);
$('.uploaded_file:eq(' + uploadedFiles + ')')
.find('img').attr("src", file_cover);
}, {
tags: ["picture"]
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.