Search code examples
javascriptsoundmanager2

How to return song duration in soundmanager


How can i return song duration in soundmanager with function ?

function item_duration(){
    var song_item = soundManager.createSound({
        id:'etc',
        url:'etc',
        onload: function() {
                    if(this.readyState == 'loaded' ||
                    this.readyState == 'complete' ||
                    this.readyState == 3){
                    return = this.duration;         
                    }
       }
   });

   song_item.load();

}

This is my try, but it's not working


Solution

  • return is a keyword, not a variable. return this.duration; is what you'd want; skip the = (which will just give you a syntax error)

    … but that won't help much, because where are you returning it to? You'll need to call another function, that does something with the duration. The item_duration function returns immediately after calling createSound, which then loads the file asynchronously

    Try something like this

    function doSomethingWithTheSoundDuration(duration) {
        alert(duration); // using alert() as an example…
    }
    
    soundManager.createSound({
        id:  …,
        url: …,
        onload: function() {
            // no need to compare with anything but the number 3
            // since readyState is a number - not a string - and
            // 3 is the value for "load complete"
            if( this.readyState === 3 ) { 
                doSomethingWithTheSoundDuration(this.duration);
            }
        }
    });