1 Importjavax.sound.midi.*;2 3 Public classMidi {4 Public voidPlayintInstrument,intNote) {5 Try {6Sequencer player =midisystem.getsequencer ();7 Player.open ();8 9Sequence seq =NewSequence (SEQUENCE.PPQ, 4);//Divisiontype, resolutionTen OneTrack track =Seq.createtrack (); A -Shortmessage first =Newshortmessage (); -First.setmessage (192, 1, instrument, 0); theMidievent change =NewMidievent (First, 1); - Track.add (change); - -Shortmessage A =Newshortmessage (); +A.setmessage (144, 1, note, 100);//command channel data1 data2 - //144-note on 128-note off + //1-Channel A //44-Note 0-127 at //100-Sound path -Midievent Noteon =NewMidievent (A, 1);//tick-the Time-stamp for the event, in MIDI ticks - Track.add (Noteon); - -Shortmessage B =Newshortmessage (); -B.setmessage (1, note, 100);//command channel data1 data2 inMidievent Noteoff =NewMidievent (b, 16);//tick-the Time-stamp for the event, in MIDI ticks - Track.add (noteoff); to + player.setsequence (seq); - Player.start (); the //While (Player.isrunning ()) { * //try { $ //Thread.Sleep (+);Panax Notoginseng //} catch (Exception e) {} - //} the //player.close (); + A}Catch(Exception ex) { the ex.printstacktrace (); + } - } $ $ Public Static voidMain (string[] args) { - if(Args.length < 2) { -System.out.println ("Don t forget the instrument and note args"); the } - Else {Wuyi intinstrument = Integer.parseint (args[0]); the intNote = Integer.parseint (args[1]); -MIDI MIDI =NewMidi (); Wu Midi.play (instrument, note); - } About } $}
Java Learning Note _midi