Ask coding questions

← Back to all posts
How to play sounds in java
Wendigo0937 (0)

Hello there, I am trying to create a program (in java) that takes input representing music notes, and then plays them back. In order to have this program work, I need to be able to have my program tell the user's computer to play sounds. I have a solution that worked in eclipse, but does not seem to be working in repl. The file I have attached is not supposed to do any of the reading (that part works fine), just supposed to play the wav file I added to it.

a link to my repl that does not work and that contains all the same code as my fully functional eclipse program can be found here : https://repl.it/@Wendigo0937/Sound2

Commentshotnewtop
mwilki7 (280)

I'm not certain that this is what's happening but I think the console output you're seeing from your program is what the repl server is sending to your browser. The sound takes place on the repl server (if it even has speakers) which is why you hear sound on your eclipse project since eclipse is run on your local machine.

Wendigo0937 (0)

@mwilki7 thank you so much! That was a very fast response. Do you have any idea how I might be able to play a sound on the user's machine instead?

mwilki7 (280)

@Wendigo0937
Just thinking out loud.
You could send that sound file to your browser and have your browser immediately play it. But this means you'd have to have an HTTP (or some sort of file exchange app) server running alongside the java code dedicated only to sending files.

https://stackoverflow.com/questions/3732109/simple-http-server-in-java-using-only-java-se-api

Playing the sound in your browser is easy. Just need to get it from the server is the more challenging part.