Basic sounds.js 944 B

123456789101112131415161718192021222324252627282930
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.FreeCamera("FreeCamera", new BABYLON.Vector3(0, 0, 0), scene);
  4. var gunshot = new BABYLON.Sound("Gunshot", "sounds/gunshot.wav", scene, function () {
  5. console.log("Sound is now ready to be played.");
  6. // Play immediatly
  7. gunshot.play();
  8. // Play after 3 seconds
  9. gunshot.play(3);
  10. });
  11. // Load the sound and play it automatically once ready
  12. var music = new BABYLON.Sound("Violons", "sounds/violons11.wav", scene, null, { loop: true, autoplay: true });
  13. window.addEventListener("keydown", function (evt) {
  14. // Press space key to fire
  15. if (evt.keyCode === 32) {
  16. gunshot.play();
  17. }
  18. });
  19. // Stop the music after 5 seconds
  20. window.setTimeout(function () {
  21. music.stop();
  22. }, 10000);
  23. return scene;
  24. };