Jump to content

java play sound

technovore

EDIT: Dear forum moderators, please move this over to programming please!
sorry for the mistake

Hey guys, I'm working on a project for school and I'm trying to play an audios file(wav) in java.
I've looked all over the web and they all basically suggest the same code.

problem is that I don't hear anything.
I do hear it if I play the sound in vlc.

code:

package AudioTest;public class Main {    public static void main(String[] args) {        Sound.play("C:\\Users\\Jelle\\IdeaProjects\\AudioTest\\src\\AudioTest\\1.wav");    }}

more code:

package AudioTest;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.Clip;import java.io.File;public class Sound {    public static synchronized void play(final String fileName)    {        new Thread(new Runnable() {            public void run() {                try {                    Clip clip = AudioSystem.getClip();                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(fileName));                    clip.open(inputStream);                    clip.start();                } catch (Exception e) {                    System.out.println("play sound error: " + e.getMessage() + " for " + fileName);                }            }        }).start();    }}

Can anyone help?
thanks!

Grammar nazis are people too!
Treat your local grammar nazi nicely and he might teach you a thing or two. (Note that I'm Belgian and not a native English speaker.)
Chivalry isn't dead!

Link to comment
Share on other sites

Link to post
Share on other sites

Wrong sextion.

Corsair 760T White | Asus X99 Deluxe | Intel i7-5930k @ 4.4ghz | Corsair H110 | G.Skill Ripjawz 2400mhz | Gigabyte GTX 970 Windforce G1 Gaming (1584mhz/8000mhz) | Corsair AX 760w | Samsung 850 pro | WD Black 1TB | IceModz Sleeved Cables | IceModz RGB LED pack

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Wrong sextion.

 oh my god, I'm so sorry!

could a mod please move it over to programming please?

Grammar nazis are people too!
Treat your local grammar nazi nicely and he might teach you a thing or two. (Note that I'm Belgian and not a native English speaker.)
Chivalry isn't dead!

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not sure why you are implementing runnable and all that stuff. It's not necessary to just play sound.

 

 

First make an InputStream object from the .wav file 

then decorate it as a buffered input stream 

 

InputStream    is;

BufferedInputStream bis;

AudioInputStream     stream;

Clip          clip;

       bis 		    = new BufferedInputStream(is);       try       {    	   stream       = AudioSystem.getAudioInputStream(bis);    	   clip 		= AudioSystem.getClip();    	   clip.open(stream);    	   clip.start();    	       	          }       catch (Exception e)       {    	          }
CPU: Intel Core i5 2500K | Case: Bitfenix Prodigy | Motherboard: GA-H61N-USB3 | RAM: Corsair 8GB 1333 MHz Video CardEVGA GTX 660 Superclocked 2GB DDR5

Power Supply: Corsair CX 430 | SSD: Samsung 840 120GB | HDD: 2X Seagate Barracuda 500GB 7200rpm | Monitor Asus PB238Q & Asus PB278Q

Mouse: Lenovo N50 | Keyboard: Apple Pro Keyboard | Operating Systems: Hackintosh OS X 10.8.5 & Windows 8.1

Link to comment
Share on other sites

Link to post
Share on other sites

technovore I could be wrong, given that I have never worked with sound in Java, but if it is anything like powershell sound the reason why you might not be hearing anything is your program exits before the sound is played.  Since at a glance you appear to be running the sound code in a separate thread the main thread is free to finish, and when the main thread finishes it closes all other threads. *I could be wrong, but I think I am right in this case*

 

After you call sound.play, try calling Thread.sleep(10000); //10 seconds to delay before the main thread gets stopped, and thus stopping the sound playing

 

Anyways hope this helps, let me know if I am right or wrong :P

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

EDIT:

Tagged the OP

[member=technovore]

End Edit.

 

I will share with you the code that I wrote for one of my old games.

 

import java.io.*;import java.net.URL;import javax.sound.sampled.*;import javax.swing.*;   // To play sound using Clip, the process need to be alive.// Hence, I use a Swing application.public class audio extends JFrame {static Clip activeClip;   // Constructor   public audio() {         try {         // Open an audio input stream.       File soundFile = new File("audio/x.wav");       AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);         // Get a sound clip resource.         Clip clip = AudioSystem.getClip();         activeClip = clip;         // Open audio clip and load samples from the audio input stream.         clip.open(audioIn);         clip.start();      } catch (UnsupportedAudioFileException e) {         e.printStackTrace();      } catch (IOException e) {         e.printStackTrace();      } catch (LineUnavailableException e) {         e.printStackTrace();      }   }         public static void main(String[] args) {         }}

I made this audio.java.

 

In the main file, EXAMPLE: Main.java, you need to actually start the audio class by creating a new instance.

new audio();

You can then stop the audio by closing the active clip.

audio.activeClip.stop();

If you would like me to explain a little better please let me know. I hope this helps.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×