最近想找个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);
    }


}

posted on 2005-06-02 23:50  steeven  阅读(1665)  评论(1编辑  收藏  举报