Search code examples
javascriptflashsoundmanager2

accessing SoundManager 2 across functions


Ok this thing is awesome! I got SoundManager 2 setup within my .js file and have audio playing on my page. My only question at the moment, is figuring out how to play audio outside of the soundManager.setup({...}). For example the following works great...

function mSound() {
/*SETUP SOUND MANAGER 2*/
soundManager.setup({
// where to find flash audio SWFs, as needed
url: 'audio/',
onready: function() {
console.log('SM2 is ready to play audio!');

    /*MY SOUND COLLECTIONS*/ 
    soundManager.createSound({
    id: 'myIntro',
    url: 'audio/Indonesia.mp3',
    autoPlay: false,
    volume: 15
    });
    soundManager.play('myIntro');

}
});
}

But if I try to place soundManager.play('myIntro'), into another function like...

function mIntro() {
 soundManager.play('myIntro');
}

...the audio does not play. Any advice will be great!

Thanks


Solution

  • I think I solved it. By setting up local variables as parameters for my mSound() function like so...

    function mSound(id,url,volume) {
    this.id = id;
    this.url = url;
    this.volume = volume;
    
    /*SETUP SOUND MANAGER 2*/
    soundManager.setup({
    url: 'audio/',
    onready: function() {
             //console.log('SM2 is ready to play audio!');
         /*MY SOUND COLLECTIONS*/ 
         soundManager.createSound({
         id: id,
         url: 'audio/'+ url,
         volume: volume
         });
         soundManager.play(id);
         }
    }); 
    }
    

    ...I'm now able to do cool stuff like this within other javascript functions and play sound!

    mSound('myIntro','Indonesia.mp3',5);
    

    And you can still still use the soundManager global object properties after you load your custom function. For example you can pause your track like this later in your code...

     soundManager.togglePause('myIntro');
    

    Hope this helps someone :)