1

My code is meant to play the specified frequency and wave form when the button is pressed and stop playing when its released however the sound does not stop when the button is released, any help would be useful:

public boolean onTouch(View v, MotionEvent event) {
    int frequency = Integer.parseInt(frequencyInput.getText().toString());
    displayFrequency.setText(String.valueOf(frequency));
    sineWave.setSine(frequency);
    squareWave.setSquareWave(frequency);
    sawWave.setSawWave(frequency);
    boolean on = (sine.isChecked());
    boolean sqOn = (square.isChecked());
    boolean sawOn = (saw.isChecked());
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (on) {
                sineWave.start();
            } else if (sqOn) {
                squareWave.start();
            } else if (sawOn) {
                sawWave.start();
            }
            break;
        case MotionEvent.ACTION_UP:
            if (!on) {
                sineWave.stop();
            }
            if (!sqOn) {
                squareWave.stop();
            }
            if (!sawOn) {
                sawWave.stop();
            }
            break;
   }
   return false;
}}
u32i64
  • 2,384
  • 3
  • 22
  • 36
Jackington
  • 127
  • 10
  • 2
    I may be wrong but where do you get the (!on) its FALSE so the stop(); is never called and so on they need to be true for the if statement to work. In the MotionEvent.ACTION_UP: – Roger Belk Mar 06 '17 at 14:56
  • you need to return true whenever you handle any touch event. soon after returning false in action down yoir touch listener expires – Mohammed Atif Mar 06 '17 at 14:58
  • You're right, thanks so much i've spent an unjustifiable amount of time working on this problem – Jackington Mar 06 '17 at 15:00
  • @RogerBelk , Thank you, I've been struggling with it for a day :) – Mamedov Dec 16 '21 at 15:14

0 Answers0