angular-file-upload icon indicating copy to clipboard operation
angular-file-upload copied to clipboard

How to get audio file duration before uploading ?

Open BriceN opened this issue 8 years ago • 4 comments

I'd like to get audio file metadata to store those informations in my database, but I don't really know how to proceed.

I tried to create a new Audio() and to set its src to some url, in order to easily get the duration, but fileitem object generated by angular-file-upload doesn't seem to have any sort or uri/blob.

Any help ?

BriceN avatar Aug 02 '17 10:08 BriceN

@BriceN I also need audio duration before upload to it display beneath ng-thumb... any hacks you got ??

BahodurSaidov avatar Sep 07 '17 16:09 BahodurSaidov

Hi @BriceN and @BahodurSaidov, a simple solution should be add onAfterAddingFile a function that handle FileItem return by angular-file-upload and then create a new Audio FIle. With this, you should be able to resolve:

       uploader.onAfterAddingFile = function(fileItem) {
            console.info('onAfterAddingFile', fileItem);
            var urlSt = URL.createObjectURL(fileItem._file);
            var audio = new Audio(urlSt);
            //Do what you want
            
        };

Can find a example, on plunk

vRuslan avatar Sep 16 '17 19:09 vRuslan

Hi @CaosZero , you were right ! I struggled with the asynchronous audio object, finally got it working ! here is my final code:

uploader.onAfterAddingFile = function(fileItem) {
          console.info('onAfterAddingFile', fileItem);
            var urlSt = URL.createObjectURL(fileItem._file);
            var audio = new Audio(urlSt);
            // fileItem.file.audioDuration = audio.duration; //won't work
            audio.addEventListener('loadeddata',function(){
              fileItem.file.audioDuration = audio.duration;
              $scope.$apply();//required to update view
});

Thank you @BriceN you can have a look too if you are still in this problem.

BahodurSaidov avatar Oct 19 '17 17:10 BahodurSaidov