Click here to Skip to main content
14,601,122 members
Rate this:
Please Sign up or sign in to vote.
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:

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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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);
    }
}
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

You were almost there. Try this:
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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100