最近想找個midi做鈴聲,以前存了上千個midi, 一時間很難找到。
mediaplayer不支援每曲抽樣開頭部分,只好自己動手了。
先在資源管理員搜尋到所有midi, 然後拖mediaplayer,儲存播放清單為m3u,
然後用下面這段程式播放m3u列表了。
利用了java內建的midi播放api. 參考了灰色魔女的midi播放程式
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;
public class MidiPlay {
public MidiPlay(final File m3u, final int line) throws Exception {
final Sequencer player = MidiSystem.getSequencer();
player.open();
final LineNumberReader list = new LineNumberReader(
new InputStreamReader(new FileInputStream(m3u), "UTF-8"));
//list.setLineNumber(line);
for(int i=0;i<line;i++)
list.readLine();
new Thread() {
public void run() {
try {
int current = line;
String file = list.readLine();
while (file != null) {
try {
file = file.trim();
if (!(file.startsWith("#") || file.length() == 0)) {
File f = new File(file);
if (f.exists()) {
player.stop();
Thread.sleep(500);
player.setSequence(MidiSystem
.getSequence(f));
player.start();
System.out.println(current + ": "
+ f.getCanonicalPath());
Thread.sleep(5000);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
file = list.readLine();
current++;
}
} catch (Exception e) {
e.printStackTrace();
}
player.close();
}
}.start();
}
public static void main(String[] args) throws Exception {
new MidiPlay(new File("e:/all-midi.m3u"),170);
}
}