2016-06-21 60 views
1

我正在编写一些代码,它将在运行时自动启动midi序列的播放,并且用户可以随时按下某个键暂停。他们主要的事件处理工作就好了,但是,我发现了一个很奇怪的错误,其中有暂停序:暂停Java定序器复位速度

public void pause() { 
    // if the sequencer is playing, pause its playback 
    if (this.isPlaying) { 
     this.sequencer.stop(); 
    } else { // otherwise "restart" the music 
     this.sequencer.start(); 
    } 

    this.isPlaying = !this.isPlaying; 
} 

重置定序器的速度。歌曲/音序器以120000 MPQ开始播放(从我的输入加载)并重置为500000 MPQ。有谁知道为什么会发生这种情况?谢谢。

+0

停止不会暂停。为什么你的实现中的代码只能由作者知道。 –

回答

0

原来,调用start()会将音序器的速度重置为500000 mpq的默认值。对于任何有同样问题的人,这里的解决方案如下:

public void pause() { 
    // if the sequencer is playing, pause its playback 
    if (this.isPlaying) { 
     this.sequencer.stop(); 
    } else { // otherwise "restart" the music 
     // store the tempo before it gets reset 
     long tempo = this.sequencer.getTempoInMPQ(); 

     // restart the sequencer 
     this.sequencer.start(); 

     // set/fix the tempo 
     this.sequencer.setTempoInMPQ(tempo); 
    } 

    this.isPlaying = !this.isPlaying; 
}