Can someone help me with a boolean to make sound play once from a loop?

I'm checking if a value of a place changes but I only want the sound to play once from the loop and stop and then restart again if the next value of a place is the same. I will be having more than one sound in the end but right now I'm working off one. I'm not too experienced at Processing but I have been trying to it but not succeeding. Any help is much appreciated. It's basically the last step I need to finish this

JSONObject earthJSON;
JSONArray earth;

import ddf.minim.*;

Minim minim;
AudioSample sound; 
boolean firstPlayThrough = false;

int mag;
String placecheck = " ";
String place;
int startTime;

void setup() {

 frameRate(0.1);
  size(512, 200, P3D);  
  minim = new Minim(this);
  sound = minim.loadSample( "sound.mp3");

}

void playSound(){
if (mag == 1){

  if(firstPlayThrough == false){
          sound.trigger();}

}

}   

void checkValue(){   
placecheck = place;

playSound();

}


void draw() { 

  earthJSON = loadJSONObject("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson");

  earth = earthJSON.getJSONArray("features");
  //println(earth.size());


    mag = earth.getJSONObject(0).getJSONObject("properties").getInt("mag");
    place = earth.getJSONObject(0).getJSONObject("properties").getString("place");

    println("_________________________");
    println("mag: " + mag);   
    println("place: " + place);

    if (place != placecheck){
 if(sound.isPlaying() == false){
   sound.pause();
   if(firstPlayThrough == false) firstPlayThrough =  true;}
    }


 if(firstPlayThrough == true){

        sound.pause();//pause the current playback wherever it is
        sound.cue(0);//set it at the start
        sound.trigger();//play        
        }

}



void stop()
{
minim.stop();

}

Answers

  • edited April 2014
    void draw() {
    
      earthJSON = loadJSONObject("http://" + "earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson");
      ...
    

    PLEASE DON'T DO THIS. draw() runs up to 60 times a second by default. requesting that web page constantly like this is a terrible thing to do. you need to limit it.

    http://earthquake.usgs.gov/earthquakes/feed/v1.0/quakeml.php says that the data is only updated every 5 minutes so you only need to check it that frequently.

  • How do I do that?

  • An example from this thread below:
    http://forum.processing.org/two/discussion/1891/adding-intervals-for-fetching-rss-data

    /** 
     * Timed XML Fetcher (v2.12)
     * by  alless0 (2013/Dec)
     * mod GoToLoop
     * 
     * forum.processing.org/two/discussion/1891/
     * adding-intervals-for-fetching-rss-data
     */
    
    static final int MAX_ROWS = 9, INTERVAL = 5*60*1000; // 5 minutes
    static final int DIM = 48, GAP = 65;
    static final color BG = 0300, FG = 0;
    
    static final String URL  = "http://" + "www.hamshahrionline.ir/rss";
    static final String DIR  = "channel/item/title";
    static final String NAME = "news-", EXT = ".png";
    
    static PGraphics pg;
    static boolean isActive = true;
    
    void setup() {
      size(1280, 720);
      frameRate(60);
      noLoop();
    
      pg = createGraphics(width, height);
    
      pg.beginDraw();
      pg.smooth(4);
      pg.fill(FG);
      pg.textFont(createFont("Nazanin", DIM));
      pg.textAlign(RIGHT, TOP);
      pg.endDraw();
    
      thread("xmlFetchTimer");
    }
    
    void draw() {
      background(pg);
      //pg.save(dataPath(NAME + nf(frameCount, 3) + EXT));
      saveFrame(dataPath(NAME + "###" + EXT));
    }
    
    void xmlGetAndRender() {
      final XML[] elems = loadXML(URL).getChildren(DIR);
      final int len = min(elems.length, MAX_ROWS);
      final int w = width - 5;
    
      pg.background(BG);
      for (int i = 0; i != len; pg.text(elems[i++].getContent(), w, GAP*i));
      pg.endDraw();
    }
    
    void xmlFetchTimer() {
      delay(1000);
      frameCount = 1;
    
      while (isActive) {
        xmlGetAndRender();
        redraw();
        delay(INTERVAL);
      }
    }
    
  • Don't make duplicate threads. I answered in the other thread, so you are diluting information and wasting efforts.

Sign In or Register to comment.