Ask coding questions

← Back to all posts
How to add sound
h
RiZeFrostbite (1)

When I try to go to sites that use sound it does not work. Why and how do you fix it?

Comments
hotnewtop
[deleted]
const audio = require('@replit/audio');

(async () => {
  const source = await audio.playFile({filePath: './audio.wav'});

  let volume = 1;
  let loops = 0;

  console.log('type "up" or "down" to change volume');
  console.log('type "loop" increase loops');
  console.log('press enter to play/pause');

  while(true) {
    console.log(
      'volume is at', volume*100, 'with',
       await source.getRemainingLoops(), 'loops remaining'
    );

    const input = prompt('');

    if (input === 'up') {
      volume += .25
      await source.setVolume(volume);
    } else if (input === 'down') {
      volume -= .25
      await source.setVolume(volume);
    } else if (input === 'loop') {
      loops++;
      await source.setLoop(loops)
    } else {
      await source.togglePlaying();
    }
  }
})();