开发者

How to return song duration in soundmanager

开发者 https://www.devze.com 2023-03-31 01:46 出处:网络
How can i return song duration in soundmanager with function ? function item_duration(){ var song_item = soundManager.createSound({

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){
                    ret开发者_JAVA技巧urn = this.duration;         
                    }
       }
   });

   song_item.load();

}

This is my try, but it's not working


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);
        }
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号