How to use GImageButton1 to turn my audio file on/off with loops?

edited April 2017 in Library Questions

This is my GUI BUTTON: I want to use it to play/pause my audio file:

public void imgButton1_click1(GImageButton source, GEvent event) { //_CODE_:imgButton1:842456:
  println("imgButton1 - GImageButton >> GEvent." + event + " @ " + millis());
  imgButton1 = source;
  if (playing == true) 
    {
    myplayer.play();
    }
  else 
    {
    myplayer.pause();
  }
} //_CODE_:imgButton1:842456:

This is where it is in my code:

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

//declarations
Minim minim; // audio 
AudioPlayer myplayer; // audio output device
boolean playing = false;

void setup() 
{
  createGUI();
  minim = new Minim(this); // instantiate the audio class
  myplayer = minim.loadFile("subbeat.wav");

  myplayer.play();
  myplayer.pause();
}

Answers

  • In the button event handler you have

      if (playing == true) {
        myplayer.play();
      }
      else {
        myplayer.pause();
      }
    

    which doesn't make sense, think about it, then try this

      if (myplayer.isPlaying()) {
        myplayer.pause();
      }
      else  {
        myplayer.play();
      }
    
  • edited April 2017

    Ah thank you very much! I still can't get the player to work though,1 I have successfully imported all my libraries.

    My declarations are 
        Minim minim;
        AudioPlayer myplayer;
        float gain = 0.0;
        float tempo = 0.0;
    
    In setup I have:
        minim = new Minim(this); // instantiate the audio class
        myplayer = minim.loadFile("subbeat.wav");
    
  • Answer ✓

    Please edit your post, select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    You can explore previous posts working with the minim library: https://forum.processing.org/two/search?Search=minim or explore the examples that come with the library.

    Kf

  • The audio file was corrupt. Quarks answer above was right. Thanks.

Sign In or Register to comment.