Results 1 to 3 of 3
  1. #1
    ytrewqc is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Thumbs up Making and adding music to a java application

    Hello, I am making a small java game and I want to add some music into it.
    I dont want to download any music though, I want to make it myself.
    What is the easiest way to do that?

    I have managed to add a midi file that i found on the internet, the problem is that i don't know how to make midi files.

    Does anyone know a free software that lets you make music and export it as a midi file?

    I know how to make mp3 files but I can't find a simple way to play it in java without downloading stuff.

    So if someone knows how to easily play a mp3 file in java, or knows a good free music software that export midi, then please reply.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default Re: Making and adding music to a java application

    Playing the file is pretty straightforward, I'm not sure about software to create a MIDI. Here is an example of how to play an existing MIDI:

    Java Code:
    try {
            Sequence sequence = MidiSystem.getSequence(new File("/PATH/TO/FILE"));
            Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.open();
            sequencer.setSequence(sequence);    
            sequencer.start();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } catch (MidiUnavailableException e) {
        } catch (InvalidMidiDataException e) {
        }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Making and adding music to a java application

    Quote Originally Posted by ytrewqc View Post
    Does anyone know a free software that lets you make music and export it as a midi file?
    Yup, the JDK. But you need to make use of the undocumented com.sun.media.sound.StandardMidiFileWriter, which *may* be renamed/relocated/removed in any future release.

    I've never used it for anything useful, but here's a small experimental code I wrote just to see for myself that it works.
    Java Code:
    import com.sun.media.sound.StandardMidiFileWriter;
    import java.io.File;
    import java.io.IOException;
    import javax.sound.midi.InvalidMidiDataException;
    import javax.sound.midi.MidiEvent;
    import javax.sound.midi.MidiSystem;
    import javax.sound.midi.Sequence;
    import javax.sound.midi.ShortMessage;
    import javax.sound.midi.Track;
     
    public class MidiOctaveFile {
     
       public static void main(String[] args) {
          StandardMidiFileWriter writer = new StandardMidiFileWriter();
          try {
             Sequence sequence = new Sequence(Sequence.PPQ, 8);
             Track track = sequence.createTrack();
             ShortMessage shortMessage;
             MidiEvent event;
             int[] offsets = {0, 2, 4, 5, 7, 9, 11, 12, 12};
             for (int i = 0; i < 9; i++) {
                shortMessage = new ShortMessage();
                shortMessage.setMessage(ShortMessage.NOTE_ON, 0, 60 + offsets[i],
                        127);
                event = new MidiEvent(shortMessage, i * 10);
                track.add(event);
             }
             int[] allowedTypes = MidiSystem.getMidiFileTypes(sequence);
             writer.write(sequence, allowedTypes[0],
                     new File("e:/java/midi.mid"));
          } catch (IOException ex) {
             ex.printStackTrace();
          } catch (InvalidMidiDataException ex) {
             ex.printStackTrace();
          }
       }
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need help with adding Background music to my game.
    By Ballistixz in forum New To Java
    Replies: 2
    Last Post: 05-15-2013, 09:58 AM
  2. Replies: 2
    Last Post: 10-01-2011, 02:08 AM
  3. Web application to manage music
    By Thomasheen in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 12:20 AM
  4. adding music to a applet.
    By ramsrocker in forum New To Java
    Replies: 1
    Last Post: 02-14-2009, 04:31 AM
  5. Making Java Application Executable
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-23-2008, 12:59 PM

Tags for this Thread

Posting Permissions

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