Leap Motion在更新功能中播放声音(openframework)

Leap motion play sound in update function (openframework)

我用leap motion弹奏乐器音符,我用palmPosition触发声音播放,但这不是我想要的,palmPosition处于更新功能,声音会根据帧率播放又一遍。太快了,我想要跳跃动作,就像鼠标向上和向下鼠标一样...当 palmPosition 改变时播放声音。 这是我的代码,

    void ofApp::update(){
        ...
    if (hands[i].palmPosition().z < 50) {
                    isTouch = true;
                        if ((hands[i].palmPosition().x > -240)&& (hands[i].palmPosition().x < -160)) {
                                pianoE.setVolume(1);
                                pianoE.play();
                        }

                        if ((hands[i].palmPosition().x >-120)&& (hands[i].palmPosition().x <-60)) {
                            pianoE.setVolume(1);
                            pianoE.play();
                        }

                        if ((hands[i].palmPosition().x >-20)&& (hands[i].palmPosition().x < 20)) {
                            pianoC.setVolume(1);
                            pianoC.play();
                        }

                        if ((hands[i].palmPosition().x > 90)&& (hands[i].palmPosition().x < 120)) {
                            pianoC.setVolume(1);
                            pianoC.play();
                        }

                        if ((hands[i].palmPosition().x >180)&& (hands[i].palmPosition().x < 240)) {
                            pianoC.setVolume(1);
                            pianoC.play();
                        }
                    }else{
                    isTouch = false;

                }
...
    }

您检查声音是否 isPlaying() 并且仅在它尚未这样做时才播放它。

例如

if (hands[i].palmPosition().z < 50) {
                    isTouch = true;
                        if ((hands[i].palmPosition().x > -240)&& (hands[i].palmPosition().x < -160)) {
                                pianoE.setVolume(1);
                                if(!pianoE.isPlaying()) pianoE.play();
                        }

                        if ((hands[i].palmPosition().x >-120)&& (hands[i].palmPosition().x <-60)) {
                            pianoE.setVolume(1);
                            if(!pianoE.isPlaying()) pianoE.play();
                        }

                        if ((hands[i].palmPosition().x >-20)&& (hands[i].palmPosition().x < 20)) {
                            pianoC.setVolume(1);
                            if(!pianoC.isPlaying()) pianoC.play();
                        }

                        if ((hands[i].palmPosition().x > 90)&& (hands[i].palmPosition().x < 120)) {
                            pianoC.setVolume(1);
                            if(!pianoC.isPlaying()) pianoC.play();
                        }

                        if ((hands[i].palmPosition().x >180)&& (hands[i].palmPosition().x < 240)) {
                            pianoC.setVolume(1);
                            if(!pianoC.isPlaying()) pianoC.play();
                        }
                    }else{
                    isTouch = false;

                }