Results 1 to 7 of 7
  1. #1
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default javax.sound play midi

    I want to make a javax.sound applet/application.

    But now I can only open and play an audio (.au) file.
    now I want to open and play a .mid file.

    I've read it's possible to do that with a sequence(r), but I don't know how I need to use it.

    Does someone have a simple example for open and play a .mid file?

    Thanks,
    Dennis

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yeas that's the way, Sequence will done the job. Open the stream before start it.

    Java Code:
                Sequencer sequencer = MidiSystem.getSequencer();
                sequencer.setSequence(MidiSystem.getSequence(midi_file));
                sequencer.open();
                sequencer.start();

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    When I do that I get an error. (my IDE is "EclipseSDK")

    This is my source-code:
    Java Code:
    import java.io.File;
    import java.io.IOException;
    import javax.sound.midi.InvalidMidiDataException;
    import javax.sound.midi.MidiSystem;
    import javax.sound.midi.MidiUnavailableException;
    import javax.sound.midi.Sequencer;
    
    
    public class sound3 extends java.applet.Applet
    {
    	private static final long serialVersionUID = 1L;
    
    	public sound3()
    	{
    		Thread soundThread = new Thread(soundRun);
    		soundThread.start();
    	}
    	
    	Runnable soundRun = new Runnable(){
    			public void run()
    			{
    				try{
    					Sequencer sequencer = MidiSystem.getSequencer();
    					sequencer.setSequence(MidiSystem.getSequence(new File("TLSTN.MID")));
    					sequencer.open();
    					sequencer.start();
    				}catch(InvalidMidiDataException ex){
    					ex.printStackTrace();
    				}catch(IOException ex){
    					ex.printStackTrace();
    				}catch(MidiUnavailableException ex){
    					ex.printStackTrace();
    				}
    			}
    	};
    }
    And this is the error that I recieve:
    Java Code:
    javax.sound.midi.InvalidMidiDataException: could not get sequence from file
    	at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:854)
    	at sound3$1.run(sound3.java:23)
    	at java.lang.Thread.run(Thread.java:619)

  5. #5
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    I think it's already solved, my .mid file isn't a good MIDI file.
    when I tried it with a .mid file from internet it's working.

    Thanks! ;)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Regarding your question. It's better to validate with a endless loop. It's much easier way to work on. Something like this.

    Java Code:
    while(true) {
                    if(sequencer.isRunning()) {
                        try {
                            Thread.sleep(1000);
                        } 
                         catch(Exception ex) {
                            // Relevant process
                        }
                    }
                }

Similar Threads

  1. Create and play midi notes
    By tor in forum New To Java
    Replies: 0
    Last Post: 04-26-2010, 01:18 AM
  2. Trouble using midi sound package
    By isum12b in forum Eclipse
    Replies: 1
    Last Post: 04-05-2009, 11:11 AM
  3. Make sound play in a java application
    By lenny in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2007, 12:45 PM
  4. how to play MIDI in java application?
    By oregon in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 03:26 AM
  5. Play sound and Video in a Mobile Phone
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 2
    Last Post: 05-23-2007, 11:52 AM

Posting Permissions

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