史上第一个声音播放程序

2016-11-27  本文已影响0人  狂舞曲少WA几个好不
package book.java;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

public class MideSound {

    public MideSound() {
        // TODO Auto-generated constructor stub
    }

    public void play() {
        //获得播放器并打开
        try {
        Sequencer player = MidiSystem.getSequencer();
        player.open();
        
        Sequence seq = new Sequence(Sequence.SMPTE_30DROP, 4);
        
        Track track = seq.createTrack();

        
        ShortMessage aMessage = new ShortMessage();
        aMessage.setMessage(144, 2, 24, 100);
        MidiEvent noteOn = new MidiEvent(aMessage, 1);
        track.add(noteOn);
        
        ShortMessage bMessage = new ShortMessage();
        bMessage.setMessage(128, 1, 84, 100);
        MidiEvent noteOff = new MidiEvent(bMessage, 2222222);
        track.add(noteOff);
        
        player.setSequence(seq);
        player.start();
        } catch(Exception ex) {
            ex.printStackTrace();
        } 
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 构建播放器对象
        MideSound mini = new MideSound();
        mini.play();
    }

}
上一篇下一篇

猜你喜欢

热点阅读