Can someone help? Want to play a sound once from a loop

I'm constantly looping through JSON of live earthquake data to check if there's new data.

What I want to do is to play about a 20 second clip only once when there's new data. For each magnitude I will have a different sound. I've tried delaying the frameRate but it still will play the sound again as it's in the loop. Any way of fixing this?

JSONObject earthJSON;
JSONArray earth;

import ddf.minim.*;

Minim minim;
AudioSample sound;

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){

  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){
    checkValue();
    }




}

void stop()
{
minim.stop();

} 

Answers

  • Can't you store a boolean that keeps track of whether you've already played the sound? Then only play the sound when the boolean is false.

  • Thanks for the suggestion but could you give an example of what you mean? I'm not overly experienced in Processing.

  • You should not rely on the frame rate to ensure the Json data isn't read too often. You should learn to use a timer with millis().

    This technique can also be used to control the delay between two sounds.

Sign In or Register to comment.