I've read prior issues about java AudioClip and have done my research, yet I still cannot figure out why this AudioClip does not play. The .wav clip plays fine in Eclipse IDE, it is also in the appropriate directory; if the file is in the incorrect directory this code snippet throws an error message.
My professor asked that we play the audioClip using this format also, audioClip = Applet.newAudioClip(new File(filePath).toURI().toURL());
When executed the play method at the bottom does get invoked however NO SOUND! Any help would be appreciated.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.MalformedURLException;
public class PlaySoundApplet extends Applet implements ActionListener
{
private static final long serialVersionUID = 1L;
Button play,stop;
AudioClip audioClip;
public void init()
{
play = new Button("Play");
add(play);
play.addActionListener(this);
stop = new Button("Stop");
add(stop);
stop.addActionListener(this);
try
{
String filePath = "." + File.separator + "audio" + File.separator + "island_music.wav";
File file = new File(filePath);
if (file.exists())
{
audioClip = Applet.newAudioClip(file.toURI().toURL());
}
else
{
throw new RuntimeException("Directory " + filePath + " does not exist"); //debugging
}
}
catch (MalformedURLException malformedURLException)
{
throw new RuntimeException("Malformed URL: " + malformedURLException); //debugging
}
}
public void actionPerformed(ActionEvent ae)
{
Button source = (Button)ae.getSource();
if (source.getLabel() == "Play")
{
audioClip.play();
System.out.println("Play was executed");
}
else if(source.getLabel() == "Stop")
{
audioClip.stop();
System.out.println("Stop was executed");
}
}
}
Try the code with the simple leftright.wav.
..the leftright.wav works!!
Most media formats (sound, image, video etc.) are what is known as 'container formats'. The encoding of the media might be in any number of types. Java reads some encodings, but not others.
So I am guessing I have to try a different type of .wav file?
If it is for an application resource, I'd tend to convert the current WAV to a type that is compatible with Java Sound.