Drop_of_Rain
asked on
Look over modified audio class
Hello objects and CEHJ. I modified this class to get rid of the command line arg. It compiles and excepts a String strFilename , would you look it over to see if i got it right. How will I call the class from another?
Do i need to worry about this in the class?
e.printStackTrace();
System.exit(1);
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioF ormat;
import javax.sound.sampled.AudioI nputStream ;
import javax.sound.sampled.AudioS ystem;
import javax.sound.sampled.DataLi ne;
import javax.sound.sampled.LineUn availableE xception;
import javax.sound.sampled.Source DataLine;
public class AudioPlayer
{
private static final int EXTERNAL_BUFFER_SIZE = 128000;
private static String strFilename;
public AudioPlayer (String strFilename)
{
File soundFile = new File(strFilename);
// We have to read in the sound file.
AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputS tream(soun dFile);
}
catch (Exception e)
{
/*
In case of an exception, we dump the exception
including the stack trace to the console output.
Then, we exit the program.
*/
e.printStackTrace();
System.exit(1);
}
AudioFormat audioFormat = audioInputStream.getFormat ();
// Asking for a SourceDataLine (for playback)
SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLi ne.class, audioFormat);
try
{
line = (SourceDataLine) AudioSystem.getLine(info);
// The line is there, but it is not yet ready
line.open(audioFormat);
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
//get audio output to device activated
line.start();
// to write data to the line. We do this
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE] ;
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abDa ta, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}
//Wait until all data are played.
line.drain();
//All data are played. We can close the shop.
line.close();
//There is a bug in the jdk1.3/1.4. we exit
System.exit(0);
}
}
Do i need to worry about this in the class?
e.printStackTrace();
System.exit(1);
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioF
import javax.sound.sampled.AudioI
import javax.sound.sampled.AudioS
import javax.sound.sampled.DataLi
import javax.sound.sampled.LineUn
import javax.sound.sampled.Source
public class AudioPlayer
{
private static final int EXTERNAL_BUFFER_SIZE = 128000;
private static String strFilename;
public AudioPlayer (String strFilename)
{
File soundFile = new File(strFilename);
// We have to read in the sound file.
AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputS
}
catch (Exception e)
{
/*
In case of an exception, we dump the exception
including the stack trace to the console output.
Then, we exit the program.
*/
e.printStackTrace();
System.exit(1);
}
AudioFormat audioFormat = audioInputStream.getFormat
// Asking for a SourceDataLine (for playback)
SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLi
try
{
line = (SourceDataLine) AudioSystem.getLine(info);
// The line is there, but it is not yet ready
line.open(audioFormat);
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
//get audio output to device activated
line.start();
// to write data to the line. We do this
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abDa
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}
//Wait until all data are played.
line.drain();
//All data are played. We can close the shop.
line.close();
//There is a bug in the jdk1.3/1.4. we exit
System.exit(0);
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER