問題は次のとおりだと思いますMidiSystem.getTransmitter()
... デフォルトのデバイス/送信機が利用できません。目的の MidiDevice を作成し、そこから送信機を取得してみてください。
編集:これは小さな例です。トランスミッター名とレシーバー名で CustomReceiver を作成するだけです。たとえば、から文字列を取得します
Info info[] = MidiSystem.getMidiDeviceInfo();
String firstDevice = info[0].getName();
で CustomReceiver を閉じることを忘れないでくださいclose()
。
import java.util.ArrayList;
import java.util.List;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiDevice.Info;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.Transmitter;
public class CustomReceiver implements Receiver{
private List<MidiMessage> m;
private MidiDevice receivedevice;
private MidiDevice transmitdevice;
private Receiver receiver;
private Transmitter transmitter;
public CustomReceiver(String transmittername, String receivername){
transmitdevice = getMidiDevice(transmittername, false);
receivedevice = getMidiDevice(receivername, true);
m=new ArrayList<MidiMessage>();
try{
receivedevice.open();
transmitdevice.open();
this.receiver = receivedevice.getReceiver();
this.transmitter = transmitdevice.getTransmitter();
this.transmitter.setReceiver(this);
}
catch(MidiUnavailableException e){
close();
e.printStackTrace();
}
}
private MidiDevice getMidiDevice(String s, boolean getReceiver){
Info[] infos = MidiSystem.getMidiDeviceInfo();
for(int i=0; i<infos.length;i++){
if(infos[i].getName().equals(s)){
try{
MidiDevice d = MidiSystem.getMidiDevice(infos[i]);
if(d.getMaxReceivers()!=0 && getReceiver){
System.out.println(infos[i].getName());
System.out.println(d.getMaxReceivers());
return d;
}
else if(d.getMaxTransmitters()!=0 && !getReceiver){
System.out.println(infos[i].getName());
System.out.println(d.getMaxTransmitters());
return d;
}
}
catch(MidiUnavailableException e){
e.printStackTrace();
}
}
}
return null;
}
public void close(){
if(receiver!=null)
receiver.close();
if(transmitter!=null)
transmitter.close();
if(transmitdevice!=null)
transmitdevice.close();
if(receivedevice!=null)
receivedevice.close();
}
public void send(MidiMessage msg, long timestamp){
//Messages are received here.
//TODO
//Send the message to the receiver
//receiver.send(msg, timestamp);
}
public void sendSingleMessage(MidiMessage msg, long timeStamp){
receiver.send(msg, timeStamp);
}
public void sendAll(){
for(MidiMessage mm: m)
receiver.send(mm, -1);
}
public void add(MidiMessage msg){
m.add(msg);
}
public void remove(MidiMessage msg){
m.remove(msg);
}
}