美文网首页
史上第一个声音播放程序

史上第一个声音播放程序

作者: 狂舞曲少WA几个好不 | 来源:发表于2016-11-27 20:14 被阅读0次
    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();
        }
    
    }
    

    相关文章

      网友评论

          本文标题:史上第一个声音播放程序

          本文链接:https://www.haomeiwen.com/subject/ovynpttx.html