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);
}
}
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);
}
}