Forum Moderators: open

Message Too Old, No Replies

Can I swap MP3 files like I swap image files?

Like a rollover for MP3 files

         

MichaelBluejay

10:53 am on May 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



We all know how to swap images with Javascript. Can the same thing be done somehow with MP3 files? I tried this but it doesn't work:

<EMBED SRC="test.mp3" CONTROLLER=True name=song>
<a href="#" onclick="document.song.src='test2.mp3'; return false">swap</a>

siMKin

12:58 pm on May 5, 2006 (gmt 0)

10+ Year Member



i tried this myself and changing the source does work in fact, it just doesn't play it. So I think the real problem lies in the fact that you should:
- stop the playback
- change the source
- start the playback

i found an article that dealt with this, but was not able to reproduce it:

[boutell.com...]

the Stop() function worked for me, but the Play(), Rewind() en DoPlay() didn't.

So i came up with another solution, that is far less elegant, but it does work :-)

define your mp3 as follows:

<div id="mp3container"> 
<EMBED SRC="song1.mp3" autostart="true" id="mp3song"/>
</div>
<a href="#" onClick="swapMP3();">swap</a>

and create this function:

<script type="text/javascript"> 
function swapMP3()
{
document.getElementById('mp3container').innerHTML = '<EMBED SRC="song2.mp3" autostart="true" id="mp3song" />';
}
</script>

this causes the whole embed-tag to be replaced by a different one, containing the new song

MichaelBluejay

9:40 am on May 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's a clever solution. Unfortunately the old sound keeps playing when the new file is loaded.

I'm starting to think this isn't possible with JavaScript. Looks like I'm probably going to have to learn Flash. :(

siMKin

9:51 am on May 8, 2006 (gmt 0)

10+ Year Member



try to alter the swap function accordingly:


function swapMP3()
{
document.getElementById('mp3song').Stop();
document.getElementById('mp3container').innerHTML = '<EMBED SRC="song2.mp3" autostart="true" id="mp3song" />';
}