Results 1 to 2 of 2
  1. #1
    misternopps is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Java won't play midi sequences. How can I get this to work?

    Hi,

    I am running java on my mac OS X 10.4
    I cannot create and play midi notes.
    No error is thrown, but no sound comes from my speakers.
    Is the problem in my code, or with my mac?
    Thanks,
    Jack

    code:

    Java Code:
    import javax.sound.midi.*;
    public class MiniMusicCmdLine {
    static {
    System.out.println("Initialising...");
    }
    public static void main(String[] args) {
    MiniMusicCmdLine mini = new MiniMusicCmdLine();
    String errorMsg = "Error - Main requires two intigers for input.";
    try {
    int instrument = 1;
    int note = 12;
    mini.play(instrument, note);
    }
    catch(Exception ex){
    System.out.println(errorMsg);
    }
    System.out.println("End of programme");//wrap up
    }
    
    public void play(int instrument, int note){
    try {
    System.out.println("entering play()");
    System.out.println("Initialising variables...");
    Sequencer player = MidiSystem.getSequencer();
    player.open();
    Sequence seq = new  Sequence(Sequence.PPQ, 4);
    Track track = seq.createTrack();
    MidiEvent event = null;
    ShortMessage first = new ShortMessage();
    first.setMessage(192, 1, instrument, 1000);
    MidiEvent changeInstrument = new MidiEvent(first, 1);
    track.add(changeInstrument);
    
    ShortMessage a = new ShortMessage();
    a.setMessage(144, 1, note, 100);
    MidiEvent noteOn = new MidiEvent(a, 1);
    track.add(noteOn);
    
    ShortMessage b = new ShortMessage();
    b.setMessage(128, 1, note, 100);
    MidiEvent noteOff = new MidiEvent(b, 1);
    track.add(noteOff);
    player.setSequence(seq);
    System.out.println("Our control flow has reached open() and start().");
    player.open();
    player.start();
    System.out.println("Now calling open(), start() five times...");
    try {
    for (int i = 1 ;i < 6; i++)
    {
    player.open();
    player.start();
    System.out.print(i + " ");
    }
    System.out.println("\n");
    }
    catch(Exception ex)
    {
    System.out.println("Error Caught while looping.  Throwing to play()");
    throw(ex);
    }
    System.out.println("Calling stop() and  close() on our midi sequence");
    player.stop();
    player.close();
    System.out.println("Exiting main()...");
    }
    catch(Exception ex) 
    {
    System.out.println("Error caught in play...");
    ex.printStackTrace();
    }
    }
    }
    Last edited by Fubarable; 05-25-2011 at 03:15 AM. Reason: code tags added

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

    Default

    I don't know whether this applies to a Mac, but a soundbank isn't included in the Windows installation of the JRE, only with the JDK.

    On Windows, the solution is to copy the file soundbank.gm from <JDK folder>\jre\lib\audio to <JRE folder>\lib\audio. Or download a soundbank (*.gm) and place it in the lib/audio folder.

    db

Similar Threads

  1. javax.sound play midi
    By Dennis in forum Advanced Java
    Replies: 6
    Last Post: 06-08-2010, 04:59 PM
  2. Create and play midi notes
    By tor in forum New To Java
    Replies: 0
    Last Post: 04-26-2010, 12:18 AM
  3. for loops and escape sequences
    By orangefest in forum New To Java
    Replies: 1
    Last Post: 04-19-2009, 08:55 PM
  4. how to play MIDI in java application?
    By oregon in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 02:26 AM
  5. Oracle sequences
    By orchid in forum JDBC
    Replies: 5
    Last Post: 04-19-2007, 03:47 PM

Posting Permissions

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