Monday, 26 October 2020

cant get length of m4a audio file on webpage

I am trying to make a website in nodejs that will retrieve the duration of an audio file. My current method uses objectURL and works for mp3, flac, and wav files. But stalls when given an m4a file.

//this function receives a list of files
    async function getFileTaggerData(songs){
        return new Promise(async function (resolve, reject) {
            var numberOfSongs = songs.length;
           
            console.log('getFileTaggerData() songs = ', songs)
            //for each song
            for (i = 0; i < numberOfSongs; i++) {
                console.log('songs[i].type = ', songs[i].type)
                if(!songs[i].type.includes('image')){
                    
                    //call function to get song length
                    let songLength = await getSongLength(songs[i]);
                    console.log('songLength = ', songLength)
                   
                }
            }
            resolve('done')
        })
    }

//get audio length
function getSongLength(song) {
    return new Promise(function (resolve, reject) {
        
        //create objectURL and audio object for songs[i]
        objectURL = URL.createObjectURL(song);
        mySound = new Audio([objectURL])
        
        //when song metadata is loaded:
        console.log('getSongLength listener time')

        mySound.addEventListener("canplaythrough", function (e) {
            var seconds = e.currentTarget.duration;
            console.log('e.currentTarget = ', e.currentTarget)
            resolve(seconds)
        });

    });
}

If I try with mp3 files, it works fine:

getFileTaggerData() songs =  FileList {0: File, 1: File, length: 2}

0: File {name: "10 - Omni Trio - Thru The Vibe (2 On 1 Mix).mp3", lastModified: 1600884031649, lastModifiedDate: Wed Sep 23 2020 11:00:31 GMT-0700 (Pacific Daylight Time), webkitRelativePath: "", size: 14530560, …} 

1: File {name: "13-Show Me A Miracle (Produced By Redlight).mp3", lastModified: 1602033613091, lastModifiedDate: Tue Oct 06 2020 18:20:13 GMT-0700 (Pacific Daylight Time), webkitRelativePath: "", size: 7717751, …}length: 2__proto__: FileList


tagger.js:107 songs[i].type =  audio/mpeg
tagger.js:971 getSongLength listener time
tagger.js:975 e.currentTarget =  <audio preload=​"auto" src=​"blob:​http:​/​/​localhost:​8080/​3b08cc31-b890-4f9c-9f30-8971af51652f">​</audio>​
tagger.js:110 songLength =  360.672653

tagger.js:107 songs[i].type =  audio/mpeg
tagger.js:971 getSongLength listener time
tagger.js:975 e.currentTarget =  <audio preload=​"auto" src=​"blob:​http:​/​/​localhost:​8080/​9b379fa8-a730-4b0e-b1d2-883e63b14b5a">​</audio>​
tagger.js:110 songLength =  192.313469

But If I try and use an m4a audio file, my getSongLength() function just stalls:

getFileTaggerData() songs =  FileList {0: File, 1: File, length: 2}

0: File {name: "01 anchors.m4a", lastModified: 1601269451193, lastModifiedDate: Sun Sep 27 2020 22:04:11 GMT-0700 (Pacific Daylight Time), webkitRelativePath: "", size: 36390912, …}

1: File {name: "02 the man i love.m4a", lastModified: 1601269468667, lastModifiedDate: Sun Sep 27 2020 22:04:28 GMT-0700 (Pacific Daylight Time), webkitRelativePath: "", size: 53100155, …}length: 2__proto__: 

tagger.js:107 songs[i].type =  audio/x-m4a
tagger.js:971 getSongLength listener time

Is there possibly something I'm missing with my current method to make it work with m4a audio files? I tried using a different method for m4a files I found online:

https://medium.com/@dineshvasudevan/duration-of-an-audio-file-via-javascript-e8d78f26b15f

This article gives some example code I tried to replicate;


function getSongLength(song) {
    return new Promise(function (resolve, reject) {
       console.log('new method!')
       var file = song;
       var objectUrl = URL.createObjectURL(file);
       var myAudio = $("<audio>");
       myAudio.on("canplaythrough", function(e){
        console.log('listenable!')
        var seconds = e.currentTarget.duration;
        console.log('m4a duration = ', seconds)
       });
       
       myAudio.prop("src", objectUrl);
    });
}

but this new method also just hands at listenable! and never prints the m4a file duration :(

I am trying to accomplish this without downloading the file



from cant get length of m4a audio file on webpage

No comments:

Post a Comment