runthis Posted April 7, 2013 Share Posted April 7, 2013 (edited) So since I run an online hacking game focused on "Uplink" nostalgia, I needed music, now this is obviously something you want to give a user control over and should not be something you added to be "generous" as most users might find what you consider to be "music" annoying. However this, with help of html5 will get you on the right path to getting a soundtrack going. This code will play 5 tracks assuming they are named track1 - track5 ogg or mp3 (it is recommended you have both for browser compatibility) and is playing them from a folder named "sounds" and when finished with track5 (last track), it will loop back to track1. var snd; //declare this so you can create options in other parts of your game later to pause songs, change volume, etc. function music(track) { if (window.HTMLAudioElement) { snd = new Audio(''); if(snd.canPlayType('audio/ogg')) { snd = new Audio('sounds/' + track + '.ogg'); } else if(snd.canPlayType('audio/mp3')) { snd = new Audio('sounds/' + track + '.mp3'); } snd.volume=.60; // 60 percent is a good way to be less obtrusive snd.play(); if(track == 'track1'){ snd.addEventListener('ended', function() { music('track2'); }, false); } if(track == 'track2'){ snd.addEventListener('ended', function() { music('track3'); }, false); } if(track == 'track3'){ snd.addEventListener('ended', function() { music('track4'); }, false); } if(track == 'track4'){ snd.addEventListener('ended', function() { music('track5'); }, false); } if(track == 'track5'){ snd.addEventListener('ended', function() { music('track1'); }, false); } } else { } } Usage example music('track1'); Some usage examples that can be programmed with a bit of javascript to a slider to change volume or tied to a onclick event snd.volume="1.0"; // Maximum snd.volume="0.1"; // Pretty low snd.muted = true; // Mute snd.pause(); // Pause music snd.play(); // Play music Demo Wont be available when my new game is released. http://www.purevirus.com/demo Edited April 7, 2013 by runthis Quote Link to comment Share on other sites More sharing options...
Ishraq Posted April 7, 2013 Share Posted April 7, 2013 Is there a demo for this. I really like these types of addons. Quote Link to comment Share on other sites More sharing options...
runthis Posted April 7, 2013 Author Share Posted April 7, 2013 Sure, I went ahead and made a demo, just view the page source to see basically the exact same code as above except with more tracks http://www.purevirus.com/demo Quote Link to comment Share on other sites More sharing options...
Ishraq Posted April 7, 2013 Share Posted April 7, 2013 nice, aint there a way to change music from the users point of view? Like next track, previous track. Quote Link to comment Share on other sites More sharing options...
runthis Posted April 7, 2013 Author Share Posted April 7, 2013 Sure there are, but I have only showed you the door, you must open it Quote Link to comment Share on other sites More sharing options...
Ishraq Posted April 7, 2013 Share Posted April 7, 2013 Sure there are, but I have only showed you the door, you must open it ahh, thanks for showing me the door, but the door is locked, i need a key :( only joking hehe, thanks Quote Link to comment Share on other sites More sharing options...
bluegman991 Posted April 7, 2013 Share Posted April 7, 2013 Lol here's your key. button.onclick=function() { music('track4'); }; Quote Link to comment Share on other sites More sharing options...
Sim Posted April 8, 2013 Share Posted April 8, 2013 now I am just guesing here, but i would say snd.next? =) Quote Link to comment Share on other sites More sharing options...
bluegman991 Posted April 8, 2013 Share Posted April 8, 2013 now I am just guesing here, but i would say snd.next? =) Nah, you would have to put a list of mp3 file paths in an array and iterate through them. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.