Coding
For Playing A Wav File
How do I play a wav file?
Answer:
Try this Code:
{
File soundFile1 = new File("C:\\won.wav");
if (!soundFile1.exists())
{
System.err.println("Wave file(s) not found!");
return;
}
AudioInputStream audioIn1 = AudioSystem.getAudioInputStream(soundFile1);
// Get a sound clip resource.
playing = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
playing.open(audioIn3);
// Play the file
playing.start();
}
catch (javax.sound.sampled.LineUnavailableException
e)
{
e.printStackTrace();
return;
}
catch (Exception
e)
{
e.printStackTrace();
return;
}
or
Use getAudioClip which is an Applet method.
Well, there is actually a very similar Applet method,
newAudioClip which is static so doesn't needs a real Applet context.
Code:
import java.net.*;
import java.applet.*;
public class a
{
public static void main(String args[])
{
try
{
AudioClip ac=Applet.newAudioClip(new
URL("file:///tmp/a.wav"));
ac.play();
Thread.sleep(5000);
}
catch(Exception e)
{
System.err.println(e);
}
}
}
or
Here is an example of an audioinputstream, maybe you
can mess with this to see whats wrong with yours!
Code:
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
public class AudioTemplate extends Object implements LineListener
{
File soundFile;
JDialog playingDialog;
Clip clip;
public static void main(String[] args) throws Exception
{
AudioTemplate s = new AudioTemplate();
}
public AudioTemplate() throws Exception {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
soundFile = chooser.getSelectedFile();
System.out.println("Playing " + soundFile.getName());
Line.Info linfo = new Line.Info(Clip.class);
Line line = AudioSystem.getLine(linfo);
clip = (Clip) line;
clip.addLineListener(this);
AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
clip.open(ais);
clip.start();
}
public void update(LineEvent le) {
LineEvent.Type type = le.getType();
if (type == LineEvent.Type.OPEN) {
System.out.println("OPEN");
} else if (type == LineEvent.Type.CLOSE)
{
System.out.println("CLOSE");
System.exit(0);
} else if (type == LineEvent.Type.START)
{
System.out.println("START");
playingDialog.setVisible(true);
} else if (type == LineEvent.Type.STOP)
{
System.out.println("STOP");
playingDialog.setVisible(false);
clip.close();
}
}
}
Do you have a Java Problem?
Ask It in The Java
Forum
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|