Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the code to play a .wav file working. however, there doesn't seem to be any actual audio playing. The program even prints the little test thing at the end. I can't seem to figure out why it won't produce any sound.

Thanks for the help in advance.

What I have tried:

Java
import java.applet.*;
import java.io.File;
import java.net.*;

public class playAudio{
	public static void main(String args[]) {
		try {
			AudioClip clip = Applet.newAudioClip(new URL("file:/------"));
			clip.play();
			System.out.println("test");
		} 
		catch (MalformedURLException murle) {
			System.out.println(murle);
		}
	}
}
Posted
Updated 13-Dec-19 4:19am
v2

Try this

public static void play(String filename)
{
    try
    {
        Clip clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(new File(filename)));
        clip.start();
    }
    catch (Exception exc)
    {
        exc.printStackTrace(System.out);
    }
}
 
Share this answer
 
Comments
Sammyw10 12-Oct-17 10:35am    
wseng, i tried your suggestion, and it tells me that it "could not get audio input stream from input file."
wseng 23-Oct-17 12:25pm    
Make sure the URL is correct
You were almost there. Try this:
Java
FileInputStream fs = null;
try {
    fs = new FileInputStream(wavSound);
    AudioClip clip = new JavaSoundAudioClip(fs);
    clip.play();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}


Or for a more complete class that you can use as-is:

Minicraft/Sound.java
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900