Results 1 to 7 of 7
Thread: Play the wav file
- 03-27-2011, 06:24 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Play the wav file
I tried to play a wav file.....but it is giving an exception noclassDefoundError here is the code..
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.DataLine.Info;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class PlayWaveFile
{
public static void main(String a[])
{
String wavfile=a[0];
FileInputStream inputStream;
try
{
inputStream=new FileInputStream(wavfile);
}
catch(FileNotFoundException e)
{return;}
PlaySound playSound=new PlaySound(inputStream);
try{
playSound.play();
}
catch(PlayWaveException e)
{return;}
}
}
class PlaySound {
private InputStream waveStream;
private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
/**
* CONSTRUCTOR
*/
public PlaySound(InputStream waveStream) {
this.waveStream = waveStream;
}
public void play() throws PlayWaveException {
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(this.waveStream);
} catch (UnsupportedAudioFileException e1) {
throw new PlayWaveException(e1);
} catch (IOException e1) {
throw new PlayWaveException(e1);
}
// Obtain the information about the AudioInputStream
AudioFormat audioFormat = audioInputStream.getFormat();
Info info = new Info(SourceDataLine.class, audioFormat);
// opens the audio channel
SourceDataLine dataLine = null;
try {
dataLine = (SourceDataLine) AudioSystem.getLine(info);
dataLine.open(audioFormat, this.EXTERNAL_BUFFER_SIZE);
} catch (LineUnavailableException e1) {
throw new PlayWaveException(e1);
}
// Starts the music :P
dataLine.start();
int readBytes = 0;
byte[] audioBuffer = new byte[this.EXTERNAL_BUFFER_SIZE];
try {
while (readBytes != -1) {
readBytes = audioInputStream.read(audioBuffer, 0,
audioBuffer.length);
if (readBytes >= 0){
dataLine.write(audioBuffer, 0, readBytes);
}
}
} catch (IOException e1) {
throw new PlayWaveException(e1);
} finally {
// plays what's left and and closes the audioChannel
dataLine.drain();
dataLine.close();
}
}
}
class PlayWaveException extends Exception {
public PlayWaveException(String message) {
super(message);
}
public PlayWaveException(Throwable cause) {
super(cause);
}
public PlayWaveException(String message, Throwable cause) {
super(message, cause);
}
}
- 03-27-2011, 06:32 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Please use code tags to maintain indentation and give more readability to your code.
- 03-27-2011, 06:37 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
I have used a simple code to play audio.
Java Code:import sun.audio.*; class JavaAudioPlaySoundExample { public JavaAudioPlaySoundExample() { try{ // open the sound file as a Java input stream String gongFile = "ujjal.wav"; InputStream in = new FileInputStream(gongFile); // create an audiostream from the inputstream AudioStream audioStream = new AudioStream(in); // play the audio clip with the audioplayer class AudioPlayer.player.start(audioStream); } catch(Exception e){} } }
- 03-27-2011, 06:52 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
@Ujjal Dhar
I have neve used sun.package;what is this? And it is giving the warning that it may be removed in future release.
- 03-27-2011, 06:59 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
FAQ - Sun Packages
Please read this before using sun.packages.
- 03-27-2011, 07:21 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Use the static Applet method newAudioClip() to obtain an AudioClip instance. This AudioClip has a play() method.
From that FAQ: "In general, writing java programs that rely on sun.* is risky: they are not portable, and are not supported." I don't reccommend using such methods.
- 03-27-2011, 07:39 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
Same from me....From that FAQ: "In general, writing java programs that rely on sun.* is risky: they are not portable, and are not supported." I don't reccommend using such methods.
It is good.Use the static Applet method newAudioClip() to obtain an AudioClip instance. This AudioClip has a play() method.
Similar Threads
-
play a wav file repeatedly
By oontvoo in forum New To JavaReplies: 6Last Post: 11-28-2010, 01:15 AM -
help to play file.wav in java
By jperson in forum New To JavaReplies: 2Last Post: 03-26-2010, 03:10 AM -
Play avi file in JMF
By ramkumarm in forum AWT / SwingReplies: 0Last Post: 04-15-2009, 01:49 PM -
play vedio file and flash file in swing
By rajasekharreddym in forum AWT / SwingReplies: 1Last Post: 07-30-2008, 08:22 PM -
how can I play an mp3 file in java?
By iliana in forum NetworkingReplies: 1Last Post: 05-30-2008, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks