hello friends,
i created a program which is expected to play a MIDI song, continuously by continuous event handling. everytime the track is finished, the sequencer object will fire a event, which will be handled by a method called meta(). the problem is the event is fired so fast that the song is not being played at all from second time. the execution of the following program may explain better.
import javax.sound.midi.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Orchestra implements MetaEventListener
{
Sequencer player;// CD player
Sequence seq;// CD
Track track;// track in CD
int[] instruments = {35, 42, 46, 38, 49, 39, 50, 60, 70, 72, 64, 56, 58, 47, 67, 63};
public void buildTrackandStart()
{
try
{
//create the CD player and switch it on
player = MidiSystem.getSequencer();
player.open();
//registering for event handling
player.addMetaEventListener(this);
//create the CD
seq = new Sequence(Sequence.PPQ,4);
//create the track 'in the CD'
track = seq.createTrack();
//-------- SONG CREATION ----------------------
for(int i=0; i < 16;i++)
{
track.add(makeEvent(144,9,instruments[i],100,i));
track.add(makeEvent(128,9,instruments[i],100,i+2));
}
//-------- end of SONG CREATION ---------------
//making an event explicitly
track.add(makeEvent(192,9,1,0,15));
//put the CD in the CD player
player.setSequence(seq);
//push the play button in the CD player
player.start();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public MidiEvent makeEvent(int command,int channel,int note,int velocity,int beat_timing)
{
MidiEvent event = null;
try
{
ShortMessage a = new ShortMessage();
a.setMessage(command,channel,note,velocity);
event = new MidiEvent(a,beat_timing);
}
catch(Exception ex)
{
}
return event;
}
public void meta(MetaMessage message)
{
System.out.println("hello");
player.start()
}
}
public class testMusicVer42a
{
public static void main(
String args[])
{
Orchestra troop1 = new Orchestra();
troop1.buildTrackandStart();
}
}
when it runs, it is supposed to play the sequence continuously but it is not. what is the problem in this code? please help me friends. kindly reply at the earliest.
[ October 17, 2005: Message edited by: parthiban rajendran ]