源码:http://yuncode.net/code/c_5052c1fddaa6538

 

 

 

 

 

 
  
  1. 01 import javax.swing.*;    
  2.  
  3. 02 import javax.sound.midi.*;    
  4.  
  5. 03 import java.awt.GridLayout;    
  6.  
  7. 04 import java.io.File;    
  8.  
  9. 05      
  10.  
  11. 06 public class MidiPlayer extends JFrame {    
  12.  
  13. 07     @SuppressWarnings("deprecation")    
  14.  
  15. 08     MidiPlayer(String song) {    
  16.  
  17. 09         super(song);    
  18.  
  19. 10         setSize(300150);    
  20.  
  21. 11         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  22.  
  23. 12         MidiPanel midi = new MidiPanel(song);    
  24.  
  25. 13         JPanel pane = new JPanel();    
  26.  
  27. 14         pane.add(midi);    
  28.  
  29. 15         setContentPane(pane);    
  30.  
  31. 16         show();    
  32.  
  33. 17     }    
  34.  
  35. 18      
  36.  
  37. 19     public static void main(String[] arguments) {    
  38.  
  39. 20      
  40.  
  41. 21         MidiPlayer pm = new MidiPlayer("c:\\1.midi"); // midi文件    
  42.  
  43. 22      
  44.  
  45. 23     }    
  46.  
  47. 24 }    
  48.  
  49. 25      
  50.  
  51. 26 class MidiPanel extends JPanel implements Runnable {    
  52.  
  53. 27     Thread runner;    
  54.  
  55. 28     JProgressBar progress = new JProgressBar();    
  56.  
  57. 29     Sequence currentSound;// 音序    
  58.  
  59. 30     Sequencer player;// 默认音序器    
  60.  
  61. 31     String songFile;// 歌曲    
  62.  
  63. 32      
  64.  
  65. 33     MidiPanel(String song) {    
  66.  
  67. 34         super();    
  68.  
  69. 35         songFile = song;    
  70.  
  71. 36         JLabel label = new JLabel("Playing file...");    
  72.  
  73. 37         setLayout(new GridLayout(21));    
  74.  
  75. 38         add(label);    
  76.  
  77. 39         add(progress);    
  78.  
  79. 40         if (runner == null) {    
  80.  
  81. 41             runner = new Thread(this);    
  82.  
  83. 42             runner.start();    
  84.  
  85. 43         }    
  86.  
  87. 44     }    
  88.  
  89. 45      
  90.  
  91. 46     public void run() {    
  92.  
  93. 47      
  94.  
  95. 48         try {    
  96.  
  97. 49      
  98.  
  99. 50             System.out.println(songFile);    
  100.  
  101. 51             File file = new File(songFile);    
  102.  
  103. 52      
  104.  
  105. 53             currentSound = MidiSystem.getSequence(file);// 获取音序文件    
  106.  
  107. 54             player = MidiSystem.getSequencer();// 获取音序器    
  108.  
  109. 55             player.open();    
  110.  
  111. 56             player.setSequence(currentSound);// 设置音序器播放指定音乐文件    
  112.  
  113. 57      
  114.  
  115. 58             progress.setMinimum(0);    
  116.  
  117. 59             progress.setMaximum((int) player.getMicrosecondLength());// 设置最大位歌曲时间    
  118.  
  119. 60      
  120.  
  121. 61             player.start();    
  122.  
  123. 62             while (player.isRunning()) {    
  124.  
  125. 63                 progress.setValue((int) player.getMicrosecondPosition());// 设置播放文件显示当前播放进度    
  126.  
  127. 64                 try {    
  128.  
  129. 65                     Thread.sleep(1000);    
  130.  
  131. 66                 } catch (Exception e) {    
  132.  
  133. 67                     // TODO: handle exception    
  134.  
  135. 68                 }    
  136.  
  137. 69             }    
  138.  
  139. 70             player.close();    
  140.  
  141. 71         } catch (Exception e) {    
  142.  
  143. 72             // TODO: handle exception    
  144.  
  145. 73         }    
  146.  
  147. 74     }    
  148.  
  149. 75      
  150.  
  151. 76 }