请检查 ID3 标签格式的结构(https://en.wikipedia.org/wiki/ID3#Layout),这是 v1 的格式:
| Field | Offset | Length | Value |
|--------|--------|--------|---------|
| Header | 0 | 3 | TAG |
| Title | 3 | 30 | |
| Artist | 33 | 30 | |
| ... ect |
- 将文件中的 128 个字节读入 blob。
- 创建一个阅读器来阅读 Blob。
- 创建一个 DataView 来读取 ArrayBuffer。
- 对于每个字段,将缓冲区转换为带有
String.fromCharCode
.
function readString(dataView, offset, length) {
var o = '';
for (var i = offset; i < offset + length; i++) {
// keep only printable characters
if (i >= 32) o += String.fromCharCode(dataView.getUint8(i));
}
return o;
}
var file = fileElm.files[0];
var blob = file.slice(file.size - 128, file.size);
var reader = new FileReader();
reader.onload = function(evt) {
var buff = evt.target.result;
var dataView = new DataView(buff)
console.log('TAG:', readString(dataView, 0, 3));
console.log('title: ', readString(dataView, 3, 30)); // title
console.log('artist: ', readString(dataView, 33, 30)); // artist
console.log('album: ', readString(dataView, 63, 30)); // album
console.log('year: ', readString(dataView, 93, 4)); // year
}
reader.readAsArrayBuffer(blob);