开发者

Head First Design Patterns - Combined Pattern

I'm reading the chapter 12 on the Combined Pattern in Head First Design Patterns. On page 541,the sample DJView,it cant't run correctly in my computer.when i press the 'start', the program only sounds once rather than circularly . i'm not sure whether because of the environment of my system. but if i add one line code in the method meta of the class BeatModel,it works.like:

  public void meta(MetaMessage message) {
        if (message.getType() == 47) {
            beatEvent();
            sequencer.setMicrosecondPosition(0);  //add this line
            sequencer.start();
            setBPM(getBPM());
        }
    }

can anyone tell me why? i'm so confused,is something wrong with the code given by t开发者_高级运维he book or some other reason? help me . Thanks in advance!!

So sorry,the code is long so i could not put all here,you could download from the offical website,here is the link http://www.headfirstlabs.com/books/hfdp/HeadFirstDesignPatterns_code102507.zip

you can find the sample in this folder '\HeadFirstDesignPatterns_code102507\HF_DP\src\headfirst\combined\djview'.

run the class DJTestDrive.java

Look forward to your help.


EDIT #2: For completeness' sake, the author of Head First Design Patterns Elisabeth Freeman herself has made a note of the fact that the code in her book has only been tested with Java 1.4. She has promised to take our feedback into account.


EDIT: There seems to be a bug with the Sequencer.setTempoInBPM during play. Your approach with setting the microsecond position to 0 is the right approach -- it basically rewinds the sequencer as soon as it ends (i.e. message type = 47).


Unfortunately, the sample code seems incorrect. There are several problems:

  1. The sequencer is not initialized to play in a loop
  2. The meta() method resets the BPM and renotifies all listeners, but does not reset the sequencer to its original position, which you did to get it to work. However, this method does not need to do anything so long as the sequencer is set to play in a loop.
  3. The off() method sets the BPM to 0, which will fast forwards the sequencer to the end of all loops -- which means next time you start the player, it will start from the end and will play nothing.

These change should do the trick:

#1 In method BeatModel.buildTrackAndStart, add sequencer.setLoopCount as follows:

 public void buildTrackAndStart() {
    // ...
    try {
        sequencer.setSequence(sequence);
        sequencer.setLoopCount(Integer.MAX_VALUE); // play forever
    } catch(Exception e) {
    // ...
} 

#2 Remove all statements from method BeatModel.meta(MetaMessage):

public void meta(MetaMessage message) {
}

#3 Remove setBPM(0) from method BeatModel.off():

public void off() {
    // -- remove this -- setBPM(0);
    sequencer.stop();
}


On Java 8 just adding the

sequencer.setMicrosecondPosition(0);

to the BeatModel.meta() makes it work just fine!

Greetings to all, hebgeenbrug


I meet the similar error when reading. in my computer the processBar doesn`t update.

1.First I add "sequencer.setMicrosecondPosition(0);" in "public void meta(MetaMessage message)" ;

2.Second I delete "sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);" in "public void setUpMidi()"; it continuous sound but "meta(MetaMessage message)" will not be notice,so ProcessBar not update.

3.Then it works

may be my experience can help others.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜