-
Notifications
You must be signed in to change notification settings - Fork 105
How to work with SMF
Kaoru Shoji edited this page Jul 23, 2014
·
1 revision
The javax.sound.midi
porting has a UsbMidiSequencer
class, this class can read and write the SMF(Standard MIDI File).
To use the MidiSystem, the application must call MidiSystem.initialize(Context)
and MidiSystem.terminate()
method.
Call MidiSystem.initialize(Context)
method at onCreate
in the Activity or Fragment.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MidiSystem.initialize(this);
}
Call MidiSystem.terminate()
method at onDestroy
in the Activity or Fragment.
@Override
protected void onDestroy() {
super.onDestroy();
MidiSystem.terminate();
}
- Obtain
Sequencer
fromMidiSystem.getSequencer()
. - Open
Sequencer
withSequencer.open()
method, and set aSequence
from SMF file.- The asset file in app, or normal file path can be read.
- Start the sequencer with calling
Sequencer.start()
method, then the sequencer thread starts playing the SMF data.
Sequencer sequencer = null;
public void onPlayButtonClick() {
try {
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(getAssets().open("example.mid"));
sequencer.start();
} catch (MidiUnavailableException e) {
e.printStackTrace();
} catch (InvalidMidiDataException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
- Just call
Suquencer.stop()
method. -
Sequencer.close()
can be used if the application don't need theSequencer
.- The another
Sequencer
instance can be obtained by callingMidiSystem.getSequencer()
method again.
- The another
public void onStopPlayingButtonClick() {
if (sequencer != null && sequencer.isOpen()) {
sequencer.stop();
sequencer.close(); // if the application don't need the Sequencer anymore.
}
}
- Obtain
Sequencer
fromMidiSystem.getSequencer()
. - Open
Sequencer
withSequencer.open()
method, and set a newSequence
. - Start the sequencer with calling
Sequencer.startRecording()
method, then the sequencer thread starts recording theSequence
data.
Sequencer sequencer = null;
public void onRecordButtonClick() {
try {
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(new Sequence(Sequence.PPQ, 480));
sequencer.startRecording();
} catch (MidiUnavailableException e) {
e.printStackTrace();
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
}
- Just call
Sequencer.stopRecording()
method. -
Sequencer.close()
can be used if the application don't need theSequencer
.- The another
Sequencer
instance can be obtained by callingMidiSystem.getSequencer()
method again.
- The another
public void onStopPlayingButtonClick() {
if (sequencer != null && sequencer.isOpen()) {
sequencer.stopRecording();
try {
MidiSystem.write(sequencer.getSequence(), 0,
new File(Environment.getExternalStorageDirectory().getPath(), "recording.mid"));
// Note: this requires the 'WRITE_EXTERNAL_STORAGE' permission
} catch (IOException e) {
e.printStackTrace();
}
sequencer.close(); // if the application don't need the Sequencer anymore.
}
}