954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can not play sound file

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!

selma_aktra
Newbie Poster
4 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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)

turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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!

selma_aktra
Newbie Poster
4 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You