Java program to add play MIDI music functions of the implementation method _java

Source: Internet
Author: User
JAVAIt does not have much advantage in multimedia processing, but we sometimes need some music in the program, if the music played is wavsuch as waveform audio files, and very large, so the background music is the best midi, but many of the online play MIDI tutorials are a few examples of simple words.
Without taking into account the release of resources, if the program is running for a long time, there will be more memory consumption of the situation, will eventually throw a Java.lang.OutOfMemoryError..
In MIDI playback, a class is more important, and that is MidisystemClass, is responsible for the entire MIDI playback equipment and so on management, is actually Seqencer, which is a MIDI playback setting for playing MIDI sequences. There is also another class called seqence, it is a sequence of midi, the MIDI sequence can be generated by the program itself, can also be read from the file or the URL.
Copy Code code as follows:

Package test1;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Hashtable;
Import Java.util.Map;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
Import javax.sound.midi.InvalidMidiDataException;
Import Javax.sound.midi.MidiSystem;
Import javax.sound.midi.MidiUnavailableException;
Import javax.sound.midi.Sequence;
Import Javax.sound.midi.Sequencer;
public class TEST5 implements runnable{
Private sequencer MIDI;
Private string[] names={"1.mid", "2.mid", "3.mid", "4.mid", "5.mid"};
private int i;
Private map<string,sequence> Map;
Public Test5 () {
Initmap ();
New Thread (This). Start ();
}
private void Initmap () {
try {
Map = new hashtable<string, sequence> ();
MIDI = Midisystem.getsequencer (FALSE);
Midi.open ();
for (String s:names) {
try {
Sequence S1 = midisystem.getsequence (new File (s));
Map.put (S, s1);
catch (Invalidmididataexception ex) {
Logger.getlogger (Test5.class.getName ()). log (Level.severe, NULL, ex);
catch (IOException ex) {
Logger.getlogger (Test5.class.getName ()). log (Level.severe, NULL, ex);
}
}
catch (Midiunavailableexception ex) {
Logger.getlogger (Test5.class.getName ()). log (Level.severe, NULL, ex);
}

}
private void Createplayer (String name) {
try {
Sequence se=map.get (name);
Midi.setsequence (SE);
Midi.start ();

}catch (Invalidmididataexception ex) {
Logger.getlogger (Test5.class.getName ()). log (Level.severe, NULL, ex);
}

}
public void Run () {
while (true) {
try {
System.out.println ("changed the file.") + (++i));
String name=names[(int) (Math.random () *names.length)];
Createplayer (name);
Thread.Sleep (10000);

catch (Interruptedexception ex) {
Logger.getlogger (Test5.class.getName ()). log (Level.severe, NULL, ex);
}
}
}
public static void Main (string[] args) {
New Test5 ();
}
}

Here is a very important point, that is, when the program is running, as long as a SeqencerOn it, I used to be in the program every time I played the generation of a SeqencerBecause at that time I think that I've all called it the Close ()method, can it still be opened? In fact it can also be opened again, is such a thinking that the program will eventually collapse due to memory overflow.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.