建立Swing Timer以每NN毫秒检查和更新刻度位置.
那么,MidiSystem中没有任何内置定时器?
当然有.但重点是所有GUI更新都应该在EDT上进行.通过从Swing调用它们Timer,它们就是.此外,MIDI计时器用于MIDI API /系统,让它做它不受干扰的事情,并在从Swing检查时报告相关信息Timer.
此外,考虑到UI组件的性质,请查看JProgressBar此GUI右上角的a.

我将Java Sound WIKI上看到的源代码改编成了这种方法的SSCCE.

import javax.sound.midi.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.URL;
class PlayMidi {
public static void main(String[] args) throws Exception {
URL url = new URL("http://pscode.org/media/EverLove.mid");
Sequence sequence = MidiSystem.getSequence(url);
final Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
Runnable r = new Runnable() {
public void run() {
final JProgressBar progress = new JProgressBar(0,(int)sequencer.getMicrosecondLength());
ActionListener updateListener = new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
progress.setValue((int)sequencer.getMicrosecondPosition());
}
};
Timer timer = new Timer(40,updateListener);
sequencer.start();
timer.start();
JOptionPane.showMessageDialog(null, progress);
sequencer.close();
timer.stop();
}
};
SwingUtilities.invokeLater(r);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |