Purpose: To play sounds in your client.
Difficulty: 3
Client Base: DragonXScape Client (my client)
Server Base: TestScape Source
Assumed Knowledge: Copying & Pasting, Adding Subroutines, Adding Variables, Adding Imports, Finding Lines Of Text, Adding Lines Of Text
Client Classes Modified: client.java
Server Classes Modified: client.java
Credits: 100% Me (Eleclion).
Now for the client part.
1. Create a file called snd.java and put this in it:
Code:
import java.util.zip.CRC32;
import sign.signlink;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.sound.midi.*;
import javax.swing.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import sign.signlink;
import java.lang.reflect.Method;
import java.util.Arrays;
import javax.swing.JOptionPane;
import java.io.*;
public class snd implements Runnable {
String sFile = "";
Boolean play = false;
private final int EXTERNAL_BUFFER_SIZE = 128000;
public void run() {
while(true) {
if (play == true) {
play = false;
playAudio(sFile);
}
}
}
public void playAudio(String pathAudio) {
String strFilename = pathAudio;
File soundFile = new File(strFilename);
/*
We have to read in the sound file.
*/
AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
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);
}
/*
From the AudioInputStream, i.e. from the sound file,
we fetch information about the format of the
audio data.
These information include the sampling frequency,
the number of
channels and the size of the samples.
These information
are needed to ask Java Sound for a suitable output line
for this audio file.
*/
AudioFormat audioFormat = audioInputStream.getFormat();
/*
Asking for a line is a rather tricky thing.
We have to construct an Info object that specifies
the desired properties for the line.
First, we have to say which kind of line we want. The
possibilities are: SourceDataLine (for playback), Clip
(for repeated playback) and TargetDataLine (for
recording).
Here, we want to do normal playback, so we ask for
a SourceDataLine.
Then, we have to pass an AudioFormat object, so that
the Line knows which format the data passed to it
will have.
Furthermore, we can give Java Sound a hint about how
big the internal buffer for the line should be. This
isn't used here, signaling that we
don't care about the exact size. Java Sound will use
some default value for the buffer size.
*/
SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
try
{
line = (SourceDataLine) AudioSystem.getLine(info);
/*
The line is there, but it is not yet ready to
receive audio data. We have to open the line.
*/
line.open(audioFormat);
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
/*
Still not enough. The line now can receive data,
but will not pass them on to the audio output device
(which means to your sound card). This has to be
activated.
*/
line.start();
/*
Ok, finally the line is prepared. Now comes the real
job: we have to write data to the line. We do this
in a loop. First, we read data from the
AudioInputStream to a buffer. Then, we write from
this buffer to the Line. This is done until the end
of the file is reached, which is detected by a
return value of -1 from the read method of the
AudioInputStream.
*/
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}
/*
Wait until all data are played.
This is only necessary because of the bug noted below.
(If we do not wait, we would interrupt the playback by
prematurely closing the line and exiting the VM.)
Thanks to Margie Fitch for bringing me on the right
path to this solution.
*/
line.drain();
/*
All data are played. We can close the shop.
*/
line.close();
}
}
Open up client.java
2. Add this to your variables:
Code:
public snd sSnd = new snd();
public static Sequencer sequencer;
3. Add this to public final void run():
Code:
(new Thread(sSnd)).start();
4. Put all the music you want in your client in a folder called "music". Make your background music in .mid format, and make your sound effects in .wav format.
5. Find:
Code:
if(s.endsWith(":duelreq:"))
Before it, for each background music you want, add:
Code:
if(s.endsWith(":ID:")) {
try {
sequencer.stop();
File midiFile = new File("music/Name.mid");
sequencer = MidiSystem.getSequencer();
sequencer.setSequence(MidiSystem.getSequence(midiFile));
sequencer.setLoopCount(-1);
sequencer.open();
sequencer.start();
} catch (Exception ex) { }
}
Replace ID with what you want the music ID to be.
Replace Name with the name of the background music file without the extention, say if the file was called "mymusic.mid", you would replace Name with "mymusic".
Before those, for each sound effect you want, add:
Code:
if(s.endsWith(":ID:")) {
try {
sSnd.sFile = "music/Name.wav";
sSnd.play = true;
} catch (Exception ex) { }
}
Replace ID with what you want the music ID to be.
Replace Name with the name of the sound effect file without the extention, say if the file was called "myeffect.wav", you would replace Name with "myeffect".
NOTE: No IDs can be the same, even if one is a background music ID and one is a sound effect ID.
6. Add this to public static final void main(String args[]):
Code:
try {
File midiFile = new File("music/Name.mid");
sequencer = MidiSystem.getSequencer();
sequencer.setSequence(MidiSystem.getSequence(midiFile));
sequencer.setLoopCount(-1);
sequencer.open();
sequencer.start();
} catch (Exception ex) { }
return;
}
Replace Name with the name of the background music file that you want to play for the client's title screen without the extention, say if the file was called "mytitle.mid", you would replace Name with "mytitle".
NOTE: The title screen music must be in .mid format for this to work.
7. Make sure that you have all of these imports:
Code:
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.io.*;
import java.math.BigInteger;
import java.net.*;
import java.util.zip.CRC32;
import sign.signlink;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.sound.midi.*;
import javax.swing.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import sign.signlink;
import java.lang.reflect.Method;
import java.util.Arrays;
import javax.swing.JOptionPane;
Now for the server part.
Open up client.java
8. Add this to your subroutines:
Code:
public void playSoundEleclion(String soundID) {
sendMessage(":"+soundID+":");
}
9. Add this line of text wherever you want to play a sound effect or background music:
Code:
playSoundEleclion("ID");
Replace ID with the ID of the sound effect or background music you want to play.
Well, enjoy!