aadsm / JavaScript-ID3-Reader

ID3 tags reader in JavaScript (ID3v1, ID3v2 and AAC)
http://www.aadsm.net/libraries/id3/
Other
555 stars 145 forks source link

Tags are empty for local file, reading from within firefox addon #43

Closed bobbyrne01 closed 9 years ago

bobbyrne01 commented 9 years ago

Any idea why this is not working for local files within a firefox addon?

function getID3 (file) {
    var url = file.name;
    console.log(url); // console.log: addon: /home/user/audio.mp3
    ID3.loadTags(url, function () {
            var tags = ID3.getAllTags(url);
            console.log(tags); // console.log: addon: {}
            console.log(tags.artist); // console.log: addon: null
    }, {
            dataReader: new FileAPIReader(file)
    });
}

var f = new File([""], "/home/user/audio.mp3");
getID3(f);

On filesystem ..

rob@work:~$ pwd
/home/rob
rob@work:~$ ls audio.mp3 
audio.mp3

Using your test page: http://web.ist.utl.pt/antonio.afonso/www.aadsm.net/libraries/id3/, it prints the following output for the local file ..

Artist
    A Perfect Circle
Title
    Annihilation
Album
    eMOTIVe
Year
    2004
Comment
Genre
    Pop/Rock
Track
    1/12
aadsm commented 9 years ago

I'm not sure how permissions work in Firefox addons but I would expect at least to have to use the file:// protocol like file:///home/user/audio.mp3. Are you sure the addons are able to read the user's FS just like that?

bobbyrne01 commented 9 years ago

Yeah, I have similar code to actually play an audio file from local FS which works fine e.g

// document.getElementById('player') is an <audio> element
document.getElementById('player').src = 'file:///home/user/audio.mp3';
document.getElementById('player').play();

Most recently I've tried:

function getID3 (file) {
    var url = file.name;
    console.log(url); // console.log: addon: /home/user/audio.mp3
    ID3.loadTags(url, function () {
        var tags = ID3.getAllTags(url);
        console.log(tags); // console.log: addon: {}
        console.log(tags.artist); // console.log: addon: null
    }, {
        dataReader: new FileAPIReader(file)
    });
}

var reader = new FileReader();

reader.onload = function(){
    var dataURL = reader.result;
    getID3(reader.result);
};

reader.readAsDataURL(new File([""], "file:///home/user/audio.mp3"));

Which outputs this error:

id3-minimized.js, line 9: TypeError: Argument 1 of FileReader.readAsBinaryString is not an object.
aadsm commented 9 years ago

It seems that you're giving a dataURL to the FileAPIReader constructor. You should give the File object directly, the FileAPIReader will to all the necessary reading. So I guess it should be as simple as getID3(new File([""], "file:///home/user/audio.mp3").

bobbyrne01 commented 9 years ago

@aadsm im going to close this, as i've got it working outside of firefox so its not due to the library itself. Thanks though!