Double-Buffering of Sound Objects and Reusing Sound IDs
Is there a way to switch sound objects on the fly? What I'd like to do is start buffering a 2nd sound after the 1st sound is completely loaded... this way there is little or no gap inbetween sound plays, as the 2nd sound will already be fully (or at least partially) loaded and ready to stream.
Something like this in psudocode would be:
load and play 1st sound, streaming
when 1st sound is loaded:
start loading 2nd sound
when 1st sound is done playing:
replace sound #1 with sound #2
play sound #1 (which is now replaced with sound #2)
The goal is for this process to keep repeating, with the "loading sound" and the "playing sound" switching places continually.
The only problem is I can't figure out how to get sound #2 into sound #1's place. I'm pretty sure that if I was to use createSound() with #2's information (url, etc.) in #1's spot, I would lose my buffering progress on the 2nd sound. That is no good, so I need another way. Something to the effect of:
soundManager.getSoundById('current_track') = soundManager.getSoundById('next_track');
That code sample does not work, but I do know that this works fine if I play 'next_track' directly. However, then buttons and other functions lose their reference to the current sound ID, as it has switched from 'current_track' to 'next_track'.
I hope this makes sense. It's a bit difficult to explain. Any ideas?
Something like this in psudocode would be:
load and play 1st sound, streaming
when 1st sound is loaded:
start loading 2nd sound
when 1st sound is done playing:
replace sound #1 with sound #2
play sound #1 (which is now replaced with sound #2)
The goal is for this process to keep repeating, with the "loading sound" and the "playing sound" switching places continually.
The only problem is I can't figure out how to get sound #2 into sound #1's place. I'm pretty sure that if I was to use createSound() with #2's information (url, etc.) in #1's spot, I would lose my buffering progress on the 2nd sound. That is no good, so I need another way. Something to the effect of:
soundManager.getSoundById('current_track') = soundManager.getSoundById('next_track');
That code sample does not work, but I do know that this works fine if I play 'next_track' directly. However, then buttons and other functions lose their reference to the current sound ID, as it has switched from 'current_track' to 'next_track'.
I hope this makes sense. It's a bit difficult to explain. Any ideas?
1
person has this question
I have this question, too!
Tell me when someone answers.
The more people who ask this question, the more it gets noticed.
The more people who ask this question, the more it gets noticed.
Create a customer community for your own organization
Plans starting at $19/month
-
Inappropriate?The way SM2 (and under the hood, Flash) work, you are basically committed to one URL after creating a sound object, for the life of that object. To keep things simple I recommend only using one URL per sound object.
That being said, using SM2 you can load a new URL eg. mySound.load('/path/to/sound.mp3'), which will result in Flash recreating the sound object internally (it has to in the Flash 8 case, because Flash 8 doesn't allow loading of new URLs into a sound object) - in the case of Flash 9, you can load new URLs without having to recreate the object but SM2 (flash 9 version) trashes it anyway so as to release the memory previously allocated.
In either case, you will lose buffering and load state on a stream when you stop loading or destroy a sound object. If it's a static MP3 sound you're loading (a regular file and not a HTTP streaming "radio" type of connection), the browser will typically cache it provided it isn't huge - and then subsequent loads will fire onload() immediately.
Based on your description, I'd do something like:
var sound1 = soundManager.createSound({
id: 'mySound1',
url: '/path/to/1.mp3',
onload: function() {
// start loading the next sound when this one has loaded..
sound2.load();
}
});
var sound2 = soundManager.createSound({
id: 'mySound2',
url: '/path/to/2.mp3',
});
function doLoop() {
sound1.play({
onjustbeforefinish: function() {
// when "almost" done, start sound #2 (or use onfinish to wait until very end)
sound2.play({
// when sound #2 is finished, do the whole thing again
onjustbeforefinish: doLoop;
});
}
});
}
doLoop(); // start playing sound #1
I didn't actually test this code FYI, just typed it out as I was thinking. Should almost work, however? ;)
For best performance of sync/timing etc., I recommend also using the Flash 9 version and enabling "high performance" mode. Unfortunately Flash does not have the best timing/sync of sound playback, particularly on Windows XP and Vista, but these two things can help on the JS end.
soundManager.flashVersion = 9;
soundManager.useHighPerformance = true;
The company says
this answers the question
Loading Profile...



EMPLOYEE