Thread: Client Sound System

Page 1 of 2 12 LastLast
Results 1 to 10 of 20
  1. #1 Client Sound System 
    The King Of Beasts
    Eleclion's Avatar
    Join Date
    Nov 2006
    Posts
    633
    Thanks given
    0
    Thanks received
    0
    Rep Power
    96
    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!
    Reply With Quote  
     

  2. #2  
    Stanyer
    Guest
    Or load it from the cache, this is the retard way.
    Reply With Quote  
     

  3. #3  
    The King Of Beasts
    Eleclion's Avatar
    Join Date
    Nov 2006
    Posts
    633
    Thanks given
    0
    Thanks received
    0
    Rep Power
    96
    Quote Originally Posted by Stanyer View Post
    Or load it from the cache, this is the retard way.
    Actually, this lets your server play background music for places through your client and use sound effects.

    You can get RuneScape background music here:

    [Only registered and activated users can see links. ]

    Well, enjoy!
    Reply With Quote  
     

  4. #4  
    Registered Member
    Your Name's Avatar
    Join Date
    Oct 2007
    Age
    29
    Posts
    1,442
    Thanks given
    0
    Thanks received
    4
    Rep Power
    172
    now post how to play my own music like linkin park and other music and ill be happy
    [Only registered and activated users can see links. ]



    MoparScape name = Doodooss
    Reply With Quote  
     

  5. #5  
    The King Of Beasts
    Eleclion's Avatar
    Join Date
    Nov 2006
    Posts
    633
    Thanks given
    0
    Thanks received
    0
    Rep Power
    96
    Quote Originally Posted by Im So Hood! View Post
    now post how to play my own music like linkin park and other music and ill be happy
    Just convert the music to .mid format and play it as background music.
    Reply With Quote  
     

  6. #6  
    qwertyuiopmun
    Guest
    election u pwn
    Reply With Quote  
     

  7. #7  
    ¯̿ ̿|̿ ̿ |̶ ̶ ̶ ̶| |̶͇̿ ̶͇̿ ͇̿ Haskelle

    Join Date
    Nov 2007
    Age
    26
    Posts
    1,228
    Thanks given
    329
    Thanks received
    517
    Rep Power
    1133
    out of cache (my way ) is better. makes it much less laggy. this is like really gonna slow down your client
    Monads are just Monoids in the category of Endofunctors. What is the problem?
    Costate Comonad Coalgebra is equivalent of Java's member variable update technology for haskell. Problem?
    Reply With Quote  
     

  8. #8  
    Registered Member
    JakobWolthers's Avatar
    Join Date
    Aug 2007
    Age
    28
    Posts
    616
    Thanks given
    16
    Thanks received
    3
    Rep Power
    84
    what u mean on step 2?

    add these to your variables

    Code:
    public snd sSnd = new snd();
    public static Sequencer sequencer;
    FIXED but now i got this error when u try to run my client

    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  9. #9  
    x19
    Guest
    He means... put them with your variables.
    Reply With Quote  
     

  10. #10  
    Registered Member
    JakobWolthers's Avatar
    Join Date
    Aug 2007
    Age
    28
    Posts
    616
    Thanks given
    16
    Thanks received
    3
    Rep Power
    84
    Quote Originally Posted by x19 View Post
    He means... put them with your variables.
    yea but now i can't run my client
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Tags for this Thread

View Tag Cloud

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •