aadsm / jsmediatags

Media Tags Reader (ID3, MP4, FLAC)
Other
748 stars 128 forks source link

No Suitable FileReader Found Apache Cordova/Angularjs #39

Closed siddmegadeth closed 7 years ago

siddmegadeth commented 7 years ago

Hi,

I have written a file scanner for cordova android. It works as expexted and fetch me the files from both sdcard and internal memory.

But the problem is i am not able to extract the mp3 TAG info. This has become a real pain

Please find my code


scan : function(url,fileType)
        {
                   log = console.log.bind(console);
                   var defer = $q.defer();

            if(window.cordova)
            {

                    var fileTypeCollection = [];

                    url.forEach(function(element, index) 
                    {
                    //requestLocalFileSystemURL
                    log(element);
                    window.resolveLocalFileSystemURL(element,onRequestFileSystem, fail);

                    //log("Ends resolve");
                    });

                    function onRequestFileSystem(fileSystem) 
                    {
                    //log(fileSystem.toURL());
                    //log(fileSystem.toInternalURL());
                    var directoryReader = fileSystem.createReader();
                    directoryReader.readEntries(onReadEntries,fail);

                    } /*onRequestFile Ends*/

                    function onReadEntries(entries) 
                    {

                    entries.forEach( function(element, index) 
                    {
                        if (element.isDirectory === true) 
                        {
                        // Recursive -- call back into this subdirectory
                            onRequestFileSystem(element);
                        } 
                        else if(element.isFile == true) 
                        {

                        fileType.forEach(function(type)
                        {
                            if(element.name.indexOf(type) != -1)
                            {
                                log(element.name);
                                log(element.fullPath);

                                jsmediatags.read(element.name, 
                                {
                                    onSuccess: function(tag) 
                                    {
                                      var title = tag.tags.title;
                                      var author = tag.tags.author;
                                      log(tag);
                                      // Pour obtenir la durée du son, cf après          
                                    },
                                    onError: function(error) 
                                    {
                                      log(error);
                                    }
                                });

                                log(element.toURL());
                                //callback(fileTypeCollection);
                                fileTypeCollection.push(element);
                                defer.resolve(fileTypeCollection);

                            }

                         });    
                    } /*is File ENds*/

                    });  /*Entries For Each Ends*/

                    }  /*OnRead Ends*/

                    function fail(resp)
                    {
                    log(resp);
                    defer.resolve(fileTypeCollection);

                    }  /*Fail Ends*/

            }
            else
            {
                log("No Cordova Installed.Cannot Seek File");
                defer.reject();
                //callback(undefined);

            }
            //log(fileTypeCollection);
            //log("Before Resolve");
            return  defer.promise;

        },  //Scan Function Ends

I even tried to use JS-ID3-READER suggestion for cordova and it is also not working

ID3.loadTags(element.name, function() { var tags = ID3.getAllTags(element.name); log(tags.comment + " - " + tags.track + ", " + tags.lyrics); }, { dataReader: ID3.FileAPIReader(element) });

This is the affected code. For my whole media scanner please refer to the below link [https://github.com/siddmegadeth/Cordova-Media-Scanner]

Please suggest. The ID3 Reader does not read .

aadsm commented 7 years ago

Will FileAPI work under Cordova? That might be the issue. jsmediatags will try to figure out which file reader to use based on the element.name you're passing. I recommend to debug how BlobFileReader is working in your current scenario.

ButzYung commented 7 years ago

I ran into a similar problem when using jsmediatags on Electron (based on Chromium). When trying to read tags from a MP3 loaded as File, an error occurs saying "canReadFile is not a function" (similar to https://github.com/aadsm/jsmediatags/issues/31 ). The problem comes from the fact that the File object in Electron (and possibly some other customized Chromium/browser-based platforms) is not recognized as window.File (File instanceof window.File === false / File typeof == "object"). In my case, I simply comment out the "file instanceof File" check and then everything works so far.

beebase commented 7 years ago

Has anyone had any success reading an mp3 in cordova?

RoboDurden commented 7 years ago

No success either :-( This new jsmediatags api only has three mediaFileReaders: http-get, blob and FileReader. FileReader gives access to the html file tag. But with this the src can not be set by code for security reasons. That is why Cordova offers its File class which needs the Android WriteToFile access permission. The previous JavaScript-ID3-Reader api was able to make use of the cordova File class and there are many examples out there like https://dzone.com/articles/working-mp3s-id3-and Unfortunately, this api does not retrieve the copyright tags :-( At least not for the ccMixter mp3 files like http://ccmixter.org/content/SiobhanD/SiobhanD_-_The_Quiet_Hours.mp3

If Aadsm would port the cordova-mediaFileReader from his previous api to this new one and i then can read really all the embedded info, i happily would crowdfund this taskwith $5 :-)

Additional $5 for editing and saving id3 info would be

If someone has success with this plugin https://www.npmjs.com/package/cordova-plugin-audiometadata please send me a message

aadsm commented 7 years ago

Hey @RoboDurden,

If it works well for JavaScript-ID3-Reader then it should also work with this one as jsmediatags supports everything the previous library did and more.

As for the copyright tag I'm able to read it, here's what I did:

jsmediatags.read("http://....SiobhanD_-_The_Quiet_Hours.mp3", {
  onSuccess: function(tags) {
    console.log(tags.tags.TCOP.data);
  },
  onError: function(error) {
    console.log(error);
  }
});

The output is:

2017 Siobhan Dakay Licensed to the public under http://creativecommons.org/licenses/by-nc/3.0/ Verify at http://ccmixter.org/files/SiobhanD/55022

I would love to look more into this, I'm just lacking in time because of a busy work schedule I have right now. I'm happy to help anyone who's willing to debug it, maybe make a public request on twitter? As for the crowdfund, I would prefer if the money was redirected towards the "Girls Who Code" NPO: https://www.classy.org/checkout/donation?eid=77372.

aadsm commented 7 years ago

Btw, I just remember that a french blogger posted about this http://blog.luce.pro/2016/02/28/Phonegap-lire-les-metadatas-des-mp3s-avec-jsmediatags/. Unfortunately it seems the site is no longer up :\

RoboDurden commented 7 years ago

Okay i got confused by the confusing Cordova classes. This works (copied out of a bigger project..):

FolderID3 = function (sPath, o, rCallback)
{
    //sPath = "Music/ccMixter//";
    if (rCallback) o.rCallback = rCallback;
    var no = _No.bind(o);
    window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory + sPath,
    function (dir)
    {
        var reader = dir.createReader();
        reader.readEntries(function (entries)
        {
            entries.forEach(function (entry)
            {
                entry.file(function (file)
                {
                    window.jsmediatags.read(file, {
                        onSuccess: function (tag)
                        {
                            console.log(tag);
                        },
                        onError: _No
                    });
                });
            });
        });
    }, _No);
}

FileID3 = function (sPath, o, rCallback)
{
    if (rCallback) o.rCallback = rCallback;
    var no = _No.bind(o);
    window.resolveLocalFileSystemURL(sPath, function (oFile)
    {
        oFile.file(function (file)
        {
            window.jsmediatags.read(file, {
                onSuccess: function (tag)
                {
                    console.log(tag);
                },
                onError: _No
            });
        });
    }, no);
}

function _No(e)
{
    this.oError = e;
    this.rCallback(this);
}

Thank you António :-)