Hello!

I write a command line program with java on eclipse. I use this code to get a sound from the beep.wav file. But i can not hear anything from the eclipse.

try
		{
			File curdir = new File (".");
			File soundFile=new File(curdir.getCanonicalPath() + "\\beep.wav");
			Clip clip = AudioSystem.getClip();
			AudioInputStream inputStream = AudioSystem.getAudioInputStream(soundFile);
			clip.open(inputStream);
			clip.start();
			clip.start();
			clip.start();
		}
		catch(Exception e)
		{
			System.out.println("error: " + e.toString());
		}

When i use beep.mp3 it gives me file is not supported format exception. But with wav i don't get any error.

So why it does not play any sound?

Thank you!

Dani AI

Generated

— short checklist and focused fixes based on the thread: your Windows code shows the right API usage, but Linux introduces two common failure modes: (1) the JVM/JavaSound can’t decode certain audio encodings (MP3 and some compressed WAV encodings aren’t supported by default), and (2) the OS sound stack or mixer/sink may be unavailable or muted. See Oracle’s Java Sound notes for format limits. (Also: was correct that MP3 isn’t supported out of the box.) Java Sound formats/troubleshooting.

Quick OS-level checks (do these first)

  • Confirm the OS can play the file outside Java: try paplay beep.wav (PulseAudio) or aplay beep.wav (ALSA). If those don’t produce sound, fix system audio first (volume, pavucontrol, ALSA mixer). paplay manpage · aplay.
  • Make sure no other daemon has the device open exclusively (Oracle’s TSG notes this can happen on Linux).

Java-side checks and small diagnostics

  • Enumerate available mixers and confirm a Clip is coming from a real output mixer (use AudioSystem.getMixerInfo()). AudioSystem API.
  • Print the file’s AudioFileFormat to confirm it’s PCM (Java likes 16-bit PCM WAV). If the WAV contains compressed data, Java may not be able to render it even though no exception was thrown earlier.
  • Avoid short fixed sleeps (your Thread.sleep(800)); Clip.start() is non-blocking. Wait for the clip to finish with a LineListener or by polling clip.isRunning() so the JVM doesn’t close the line before playback finishes. See the Java tutorial on using LineListener. Playing Back Audio / LineEvents.

If format is the problem

Tiebacks: ’s path advice is useful (confirm the file is where you think it is), and ’s MP3 note is correct — follow the OS checks and Java-side diagnostics above to narrow whether it’s a system mixer, file encoding, or JVM‑wait issue.

Recommended Answers

All 3 Replies

Instead of this:

File curdir = new File (".");
File soundFile=new File(curdir.getCanonicalPath() + "\\beep.wav");

try this:

File soundFile = new File(System.getProperty("user.dir") + "\\beep.wav");

As well make sure that "beep.wav" is spelled correctly, it is case sensitive including the extension. (as far as Java is concerned .WAV is different from .wav)

as for the exception, java doesn't support mp3 by default, you would need to get (or write :) ) packages that do that for you.

wav files don't give errors, because they are supported

I research it again.

try
		{
			File curdir = new File (".");
			File soundFile=new File(curdir.getCanonicalPath() + "//beep.wav");
			AudioInputStream stream = AudioSystem.getAudioInputStream(soundFile);      
			DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());      
			Clip clip2 = (Clip) AudioSystem.getLine(info);         
			clip2.open(stream);
			clip2.start();
			Thread.sleep(800);
			clip2.close();
		}
		catch(Exception e)
		{
			System.out.println("Can not play the sound file. Error details:" + e.toString());
		}

Now this code works on Windows 7. But it does not work on Linux and it does not give me any error. I just can not hear anything on Linux.

What you suggest?

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.