问题
Hi i'm using javax midi lib with Android and it works very well handling midi messages, but when i open and try to play a midi file it does not perform any sound. I've verified all this steps:
- No exception handling the midi file.
- Previously i've got play the midi file with winamp and with a DAW.
- I've placed soundbank.gm to directory C:\Program Files\Java\jdk1.7.0_45\lib\audio and C:\Program Files (x86)\Java\jre1.8.0_66\lib\audio
- I've got play it successfully with MediaPlayer class
My code:
private Sequencer playMidiFile() { try { Sequencer mSeqr = MidiSystem.getSequencer(); InputStream in = getAssets().open(MIDI_FILE); Sequence mSeq = MidiSystem.getSequence(in); mSeqr.open(); mSeqr.setSequence(mSeq); mSeqr.start(); //it is supposed this execution will perform the play of the file return mSeqr; } catch (MidiUnavailableException e) { e.printStackTrace(); } catch (InvalidMidiDataException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
What should i do to ear something? Thx!!
回答1:
From the java side, you have to connect to a synthesizer so you do the following -if your sequencer is tied up or you cant get the default you should check all others available in your system. Check the tutorials at oracle for further info
static void play(Sequence sequence)
throws MidiUnavailableException, InvalidMidiDataException, IOException
{
Sequencer sequencer=MidiSystem.getSequencer( );
sequencer.open();
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
sequencer.getTransmitter().setReceiver(synthesizer.getReceiver( ));
sequencer.setSequence(sequence);
sequencer.addMetaEventListener(new MetaEventListener( ) {
public void meta(MetaMessage m) {
if (m.getType( ) == END_OF_TRACK) {
System.out.println("end of track");
return;
}
}
});
sequencer.start( );
}
来源:https://stackoverflow.com/questions/34570453/android-javax-midi-sequencer-not-playing-mid-file