Thread: Music playing and fading out

Page 1 of 10 123 ... LastLast
Results 1 to 10 of 93
  1. #1 Music playing and fading out 
    Banned

    Join Date
    Jun 2008
    Posts
    913
    Thanks given
    30
    Thanks received
    94
    Rep Power
    0
    This tutorial will allow you to:

    • 1) Enable music in your client if you haven't already done so.
    • 2) Make music fade out when another music file is requested to play.
    • 3) Make the RuneScape theme play when you start your client, and fade out when you log into your server.


    Video: [Only registered and activated users can see links. ]

    An example of this, is when you walk into a different area on RuneScape and the music fades out and another music song starts playing - this is the same as that.

    Firstly, you will need to save this file as 'Midi.java' into your client's sign package (the sign folder in your client files).
    Code:
    package sign;
    
    import java.io.File;
    import javax.sound.midi.MidiSystem;
    import javax.sound.midi.Receiver;
    import javax.sound.midi.Sequence;
    import javax.sound.midi.Sequencer;
    import javax.sound.midi.ShortMessage;
    import javax.sound.midi.Synthesizer;
    
    /**
     * Midi file playing.
     * @author Primadude.
     */
    public class Midi {
    
            /**
             * The name of the midi file.
             */
            String midiFileName;
    
            /**
             * The dir of the midi file.
             */
            String midiSaveDir;
    
            /**
             * The Sequence object.
             */
            Sequence sequence;
    
            /**
             * The Sequencer object.
             */
            Sequencer sequencer;
    
            /**
             * The Synthesizer object.
             */
            Synthesizer synthesizer;
    
            /**
             * Gets the name of the midi file.
             * @return The midiFileName variable.
             */
            public String getMidiFileName() {
                    return midiFileName;
            }
    
            /**
             * Gets the midi file directory.
             * @return The midiSaveDir variable.
             */
            public String getMidiSaveDir() {
                    return midiSaveDir;
            }
    
            /**
             * Sets the midi file name.
             * @param midiFileName The String to set the file name to.
             */
            public void setMidiFileName(String midiFileName) {
                    this.midiFileName = midiFileName;
            }
    
            /**
             * Sets the midi directory.
             * @param midiFileName The String to set the file directory to.
             */
            public void setMidiSaveDir(String midiSaveDir) {
                    this.midiSaveDir = midiSaveDir;
            }
    
            /**
             * Sets the volume of the midi that is playing.
             * @param volume The volume of the midi sound.
             * @return True or false.
             */
            public boolean setVolume(double value) {
                    try {
                            Receiver receiver = MidiSystem.getReceiver();
                            ShortMessage volumeMessage= new ShortMessage();
    
                            for (int i = 0; i < 16; i++) {
                                    volumeMessage.setMessage(ShortMessage.CONTROL_CHANGE, i, 7, (int)(value * 127.0));
                                    receiver.send(volumeMessage, -1);
                            }
                            return true;
                    } catch (Exception e) {
                            e.printStackTrace();
                            return false;
                    }
            }
    
            /**
             * Fades the volume of the midi out.
             * Stops the current midi playing.
             */
            public void fadeOut() {
                    double volume = 0.6;
    
                    for (;;) {
                            if (((volume - 0.05) < 0) || !setVolume(volume)) {
                                    break;
                            }
                            try {
                                    Thread.sleep(150);
                            } catch (Exception exception) {
                            }
                            volume -= 0.025;
                    }
                    if (synthesizer != null) {
                            synthesizer.close();
                            synthesizer = null;
                    }
                    if (sequencer != null) {
                            if (sequencer.isOpen()) {
                                    sequencer.stop();
                            }
                            sequencer.close();
                    }
            }
    
            /**
             * Starts playing the midi.
             */
            public void startMidi() {
                    String midiDir = getMidiFileName() + getMidiSaveDir();
    
                    try {
                            if (sequencer != null) {
                                    fadeOut();
                            }
                            sequencer = null;
                            sequence = null;
                            File file = new File(midiDir);
    
                            if (file.exists()) {
                                    sequence = MidiSystem.getSequence(file);
                            }
                            sequencer = MidiSystem.getSequencer();
                            sequencer.setSequence(sequence);
                            synthesizer = MidiSystem.getSynthesizer();
                            synthesizer.open();
    
                            if (synthesizer.getDefaultSoundbank() == null) {
                                    sequencer.getTransmitter().setReceiver(MidiSystem.getReceiver());
                            } else {
                                    sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
                            }
                            sequencer.open();
                            sequencer.start();
                    } catch (Exception exception) {
                            exception.printStackTrace();
                    }
            }
    }

    And replace your SignLink.java with mine:
    Code:
    package sign;
    
    import java.applet.Applet;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.URL;
    import javax.sound.midi.Sequence;
    import javax.sound.midi.Sequencer;
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.FloatControl;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
    import javax.sound.sampled.UnsupportedAudioFileException;
    
    public final class signlink implements Runnable {
    
            public static Midi midii = new Midi();
    
            enum Position {
    
                    LEFT, RIGHT, NORMAL
            };
            private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
            private Position curPosition;
    
            public signlink() {
            }
            public static int uid;
            public static int storeid = 32;
            public static RandomAccessFile cache_dat = null;
            public static RandomAccessFile cache_idx[] = new RandomAccessFile[5];
            public static boolean sunjava;
            public static Applet mainapp = null;
            private static boolean active;
            private static int threadliveid;
            private static InetAddress socketip;
            private static int socketreq;
            private static Socket socket = null;
            private static int threadreqpri = 1;
            private static Runnable threadreq = null;
            private static String dnsreq = null;
            public static String dns = null;
            private static String urlreq = null;
            private static DataInputStream urlstream = null;
            private static int savelen;
            private static String savereq = null;
            private static byte savebuf[] = null;
            private static boolean midiplay;
            private static int midipos;
            public static String midi = null;
            public static int midivol;
            public static int midifade;
            private static boolean waveplay;
            private static int wavepos;
            public static String wave = null;
            public static int wavevol;
            public static boolean reporterror = true;
            public static String errorname = "";
    
            public final String updateDir() {
                    String s;
                    String s1;
                    File file;
                    s = "";
                    s1 = ".";
                    file = new File((new StringBuilder()).append(s1).append(s).toString());
    
                    if (file.exists() || file.mkdir()) {
                            return (new StringBuilder()).append(s1).append(s).append("/").toString();
                    }
                    return null;
            }
    
            public static final void startpriv(InetAddress inetaddress) {
                    threadliveid = (int) (Math.random() * 99999999D);
                    if (active) {
                            try {
                                    Thread.sleep(500L);
                            } catch (Exception _ex) {
                            }
                            active = false;
                    }
                    socketreq = 0;
                    threadreq = null;
                    dnsreq = null;
                    savereq = null;
                    urlreq = null;
                    socketip = inetaddress;
                    Thread thread = new Thread(new signlink());
                    thread.setDaemon(true);
                    thread.start();
                    while (!active) {
                            try {
                                    Thread.sleep(50L);
                            } catch (Exception _ex) {
                            }
                    }
            }
    
            public final void run() {
                    active = true;
                    String s = findcachedir();
                    uid = getuid(s);
                    try {
                            File file = new File(s + "main_file_cache.dat");
                            if (file.exists() && file.length() > 0x3200000L) {
                                    file.delete();
                            }
                            cache_dat = new RandomAccessFile(s + "main_file_cache.dat", "rw");
                            for (int j = 0; j < 5; j++) {
                                    cache_idx[j] = new RandomAccessFile(s + "main_file_cache.idx" + j, "rw");
                            }
    
                    } catch (Exception exception) {
                            exception.printStackTrace();
                    }
                    for (int i = threadliveid; threadliveid == i;) {
                            if (socketreq != 0) {
                                    try {
                                            socket = new Socket(socketip, socketreq);
                                    } catch (Exception _ex) {
                                            socket = null;
                                    }
                                    socketreq = 0;
                            } else if (threadreq != null) {
                                    Thread thread = new Thread(threadreq);
                                    thread.setDaemon(true);
                                    thread.start();
                                    thread.setPriority(threadreqpri);
                                    threadreq = null;
                            } else if (dnsreq != null) {
                                    try {
                                            dns = InetAddress.getByName(dnsreq).getHostName();
                                    } catch (Exception _ex) {
                                            dns = "unknown";
                                    }
                                    dnsreq = null;
                            } else if (savereq != null) {
                                    if (savebuf != null) {
                                            try {
                                                    FileOutputStream fileoutputstream = new FileOutputStream(s + savereq);
                                                    fileoutputstream.write(savebuf, 0, savelen);
                                                    fileoutputstream.close();
                                            } catch (Exception _ex) {
                                            }
                                    }
                                    if (waveplay) {
    
                                            String wave = s + savereq;
                                            waveplay = false;
                                            AudioInputStream audioInputStream = null;
                                            try {
                                                    audioInputStream = AudioSystem.getAudioInputStream(new File(wave/*soundFile*/));
                                            } catch (UnsupportedAudioFileException e1) {
                                                    e1.printStackTrace();
                                                    return;
                                            } catch (IOException e1) {
                                                    e1.printStackTrace();
                                                    return;
                                            }
    
                                            AudioFormat format = audioInputStream.getFormat();
                                            SourceDataLine auline = null;
                                            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    
                                            try {
                                                    auline = (SourceDataLine) AudioSystem.getLine(info);
                                                    auline.open(format);
                                            } catch (LineUnavailableException e) {
                                                    e.printStackTrace();
                                                    return;
                                            } catch (Exception e) {
                                                    e.printStackTrace();
                                                    return;
                                            }
    
                                            if (auline.isControlSupported(FloatControl.Type.PAN)) {
                                                    FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
                                                    if (curPosition == Position.RIGHT) {
                                                            pan.setValue(1.0f);
                                                    } else if (curPosition == Position.LEFT) {
                                                            pan.setValue(-1.0f);
                                                    }
                                            }
    
                                            auline.start();
                                            int nBytesRead = 0;
                                            byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
    
                                            try {
                                                    while (nBytesRead != -1) {
                                                            nBytesRead = audioInputStream.read(abData, 0, abData.length);
                                                            if (nBytesRead >= 0) {
                                                                    auline.write(abData, 0, nBytesRead);
                                                            }
                                                    }
                                            } catch (IOException e) {
                                                    e.printStackTrace();
                                                    return;
                                            } finally {
                                                    auline.drain();
                                                    auline.close();
                                            }
                                    }
                                    if (midiplay) {
                                            //midi = s + savereq;
                                            midii.setMidiFileName(s);
                                            midii.setMidiSaveDir(savereq);
                                            midii.startMidi();
                                            midiplay = false;
                                            /*try {
                                                    if (musicS != null) {
                                                            musicSr.stop();
                                                            musicSr.close();
                                                    }
                                                    musicS = null;
                                                    musicSr = null;
                                                    File music = new File(midi);
    
                                                    if (music.exists()) {
                                                            musicS = MidiSystem.getSequence(music);
                                                            musicSr = MidiSystem.getSequencer();
                                                            musicSr.open();
                                                            musicSr.setSequence(musicS);
                                                            musicSr.start();
                                                    }
                                            } catch (Exception ex) {
                                                    ex.printStackTrace();
                                            }
                                            midiplay = false;*/
                                    }
                                    savereq = null;
                            } else if (urlreq != null) {
                                    try {
                                            System.out.println("urlstream");
                                            urlstream = new DataInputStream((new URL(mainapp.getCodeBase(), urlreq)).openStream());
                                    } catch (Exception _ex) {
                                            urlstream = null;
                                    }
                                    urlreq = null;
                            }
                            try {
                                    Thread.sleep(50L);
                            } catch (Exception _ex) {
                            }
                    }
    
            }
            static Sequencer musicSr = null;
            static Sequence musicS = null;
    
            public static final String findcachedir() {
                    try {
                            String s = "";
                            String s1 = "cache";
                            File file = new File(s1 + s);
                            if (file.exists() || file.mkdir()) {
                                    return s + s1 + "/";
                            }
                    } catch (Exception _ex) {
                    }
                    return null;
            }
    
            private static final int getuid(String s) {
                    return (int) (Math.random() * 99999999D);
            }
    
            public static final synchronized Socket opensocket(int i)
                    throws IOException {
                    for (socketreq = i; socketreq != 0;) {
                            try {
                                    Thread.sleep(50L);
                            } catch (Exception _ex) {
                            }
                    }
    
                    if (socket == null) {
                            throw new IOException("could not open socket");
                    } else {
                            return socket;
                    }
            }
    
            public static final synchronized DataInputStream openurl(String s)
                    throws IOException {
                    for (urlreq = s; urlreq != null;) {
                            try {
                                    Thread.sleep(50L);
                            } catch (Exception _ex) {
                            }
                    }
    
                    if (urlstream == null) {
                            throw new IOException("could not open: " + s);
                    } else {
                            return urlstream;
                    }
            }
    
            public static final synchronized void dnslookup(String s) {
                    dns = s;
                    dnsreq = s;
            }
    
            public static final synchronized void startthread(Runnable runnable, int i) {
                    threadreqpri = i;
                    threadreq = runnable;
            }
    
            public static final synchronized boolean wavesave(byte abyte0[], int i) {
                    if (i > 0x1e8480) {
                            return false;
                    }
                    if (savereq != null) {
                            return false;
                    } else {
                            wavepos = (wavepos + 1) % 5;
                            savelen = i;
                            savebuf = abyte0;
                            waveplay = true;
                            savereq = "sound" + wavepos + ".wav";
                            return true;
                    }
            }
    
            public static final synchronized boolean wavereplay() {
                    if (savereq != null) {
                            return false;
                    } else {
                            savebuf = null;
                            waveplay = true;
                            savereq = "sound" + wavepos + ".wav";
                            return true;
                    }
            }
    
            public static final synchronized void midisave(byte abyte0[], int i) {
                    if (i > 0x1e8480) {
                            return;
                    }
                    if (savereq != null) {
                            return;
                    } else {
                            midipos = (midipos + 1) % 5;
                            savelen = i;
                            savebuf = abyte0;
                            midiplay = true;
                            savereq = "jingle" + midipos + ".mid";
                            return;
                    }
            }
    
            public static final void reporterror(String s) {
                    if (!reporterror) {
                            return;
                    }
                    if (!active) {
                            return;
                    }
                    System.out.println("Error: " + s);
                    try {
                            s = s.replace(':', '_');
                            s = s.replace('@', '_');
                            s = s.replace('&', '_');
                            s = s.replace('#', '_');
                            DataInputStream datainputstream = openurl("reporterror" + 317 + ".cgi?error=" + errorname + " " + s);
                            datainputstream.readLine();
                            datainputstream.close();
                            return;
                    } catch (IOException _ex) {
                            return;
                    }
            }
    }

    You now have music working in your client, and the RuneScape theme will play when you start up your client.
    This next bit will make it so when you log into your server, the RuneScape theme fades out.

    In Client.java, go to 'method79' (public final void method79). It will look something like:
    Code:
            public final void method79(int i) {
                    if (aClass15_1166 != null) {
                            return;
                    }
                    method118(3);
                    super.aClass15_13 = null;
                    aClass15_1107 = null;
                    aClass15_1108 = null;
                    aClass15_1109 = null;
                    aClass15_1110 = null;
                    aClass15_1111 = null;
                    aClass15_1112 = null;
                    aClass15_1113 = null;
                    aClass15_1114 = null;
                    aClass15_1115 = null;
                    aClass15_1166 = new Class15(479, 96, method11(0), 0);
                    aClass15_1164 = new Class15(172, 156, method11(0), 0);
                    Class30_Sub2_Sub1.method334(aBoolean1206);
                    aClass30_Sub2_Sub1_Sub2_1197.method361(0, 16083, 0);
                    aClass15_1163 = new Class15(190, 261, method11(0), 0);
    
                    aClass15_1165 = new Class15(512, 334, method11(0), 0);
    
                    Class30_Sub2_Sub1.method334(aBoolean1206);
                    aClass15_1123 = new Class15(496, 50, method11(0), 0);
                    if (i != 1) {
                            method6();
                    }
                    aClass15_1124 = new Class15(269, 37, method11(0), 0);
                    aClass15_1125 = new Class15(249, 45, method11(0), 0);
                    aBoolean1255 = true;
            }

    At the end of that method (below 'aBoolean1255 = true;'), add this:
    Code:
    SignLink.midii.fadeOut();
    The RuneScape theme will now fade out when you login to any server.


    If you have any problems, or want to ask a question, either PM me or post it here.

    Thanks.
    Reply With Quote  
     

  2. #2  
    Community Veteran

    Dust R I P's Avatar
    Join Date
    Jan 2008
    Posts
    2,556
    Thanks given
    183
    Thanks received
    198
    Rep Power
    561
    Holy crap thats hot

    in other words: Very nice

    EDIT: midii? Rofl make a better anti-leech...
    Reply With Quote  
     

  3. #3  
    Registered Member
    Samdude151's Avatar
    Join Date
    Jan 2008
    Age
    24
    Posts
    784
    Thanks given
    5
    Thanks received
    16
    Rep Power
    78
    Code:
    Compiling com.Dynasty
    .\src\com\dynasty\client.java:6964: cannot find symbol
    symbol  : variable midii
    location: class com.dynasty.signlink
    signlink.midii.fadeOut();
            ^
    .\src\com\dynasty\signlink.java:162: non-static method setMidiFileName(java.lang
    .String) cannot be referenced from a static context
                                            Midi.setMidiFileName(s);
                                                ^
    .\src\com\dynasty\signlink.java:163: setMidiSaveDir(java.lang.String) in com.dyn
    asty.Midi cannot be applied to (java.lang.String[])
                                            Midi.setMidiSaveDir(savereq);
                                                ^
    .\src\com\dynasty\signlink.java:164: non-static method startMidi() cannot be ref
    erenced from a static context
                                            Midi.startMidi();
                                                ^
    Note: Some input files use or override a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    4 errors
    thats what i get... i even changed midii to see if that was the problem
    -Removed, advertising is not allowed.
    Reply With Quote  
     

  4. #4  
    Member Market Banned Market Banned
    User Name's Avatar
    Join Date
    Oct 2007
    Posts
    65
    Thanks given
    1
    Thanks received
    3
    Rep Power
    19
    What do we declare 'midii' as? Do you think you could supply us with your music command?
    Reply With Quote  
     

  5. #5  
    Registered Member

    Join Date
    Nov 2008
    Posts
    2,180
    Thanks given
    148
    Thanks received
    99
    Rep Power
    2004
    Thx Prima, renaming now
    Reply With Quote  
     

  6. #6  
    Registered Member
    DaraX's Avatar
    Join Date
    Apr 2009
    Posts
    1,183
    Thanks given
    18
    Thanks received
    29
    Rep Power
    262
    Quote Originally Posted by samdude151 View Post
    Code:
    Compiling com.Dynasty
    .\src\com\dynasty\client.java:6964: cannot find symbol
    symbol  : variable midii
    location: class com.dynasty.signlink
    signlink.midii.fadeOut();
            ^
    .\src\com\dynasty\signlink.java:162: non-static method setMidiFileName(java.lang
    .String) cannot be referenced from a static context
                                            Midi.setMidiFileName(s);
                                                ^
    .\src\com\dynasty\signlink.java:163: setMidiSaveDir(java.lang.String) in com.dyn
    asty.Midi cannot be applied to (java.lang.String[])
                                            Midi.setMidiSaveDir(savereq);
                                                ^
    .\src\com\dynasty\signlink.java:164: non-static method startMidi() cannot be ref
    erenced from a static context
                                            Midi.startMidi();
                                                ^
    Note: Some input files use or override a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    4 errors
    thats what i get... i even changed midii to see if that was the problem
    Rofl at the idiot
    Ontopic: Very nice.
    Reply With Quote  
     

  7. #7  
    Banned

    Join Date
    Jun 2008
    Posts
    913
    Thanks given
    30
    Thanks received
    94
    Rep Power
    0
    Updated the tutorial. I forgot to say that you will need to declare this instance in the SignLink class:
    Code:
    public static Midi midii = new Midi();
    Reply With Quote  
     

  8. #8  
    Member Market Banned Market Banned
    User Name's Avatar
    Join Date
    Oct 2007
    Posts
    65
    Thanks given
    1
    Thanks received
    3
    Rep Power
    19
    The client hangs when you use the fadeOut() method then stops the midi with no fade.
    Reply With Quote  
     

  9. #9  
    Registered Member
    Rupps's Avatar
    Join Date
    Oct 2008
    Age
    25
    Posts
    905
    Thanks given
    317
    Thanks received
    24
    Rep Power
    194
    For me it doesn't actually fade; the client freezes while it should be fading, but still has full volume, them shuts off all at once.
    Current Project: [Only registered and activated users can see links. ]
    Reply With Quote  
     

  10. #10  
    Community Veteran

    Dust R I P's Avatar
    Join Date
    Jan 2008
    Posts
    2,556
    Thanks given
    183
    Thanks received
    198
    Rep Power
    561
    Code:
    Exception in thread "Thread-3" java.lang.NullPointerException
            at com.dynasty.Midi.fadeOut(Midi.java:112)
            at com.dynasty.client.method79(client.java:6973)
            at com.dynasty.client.method84(client.java:7275)
            at com.dynasty.client.method140(client.java:12074)
            at com.dynasty.client.method7(client.java:2689)
            at com.dynasty.Applet_Sub1.run(Applet_Sub1.java:107)
            at com.dynasty.client.run(client.java:5602)
            at java.lang.Thread.run(Unknown Source)
    just pointing out that you should fix the tutorial
    Reply With Quote  
     

Page 1 of 10 123 ... LastLast

Thread Information
Users Browsing this Thread

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


User Tag List

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