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!

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.