Skip to content

playsound #1521

Description

@elenaprogamer69-code

import processing.sound.*;
import java.io.File;
SoundFile[] sounds;
SoundFile sound;
PFont font;

void setup() {
//size(40, 20);
fullScreen();
File folder = new File(dataPath(""));
File[] files = folder.listFiles();
int count = 0;
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".wav")) {
count++;
}
textAlign(CENTER);
}

sounds = new SoundFile[count];
int index = 0;
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".wav")) {
sounds[index] = new SoundFile(this, file.getName());
index++;
}
}

font = loadFont("JIStickerbushCaps-48.vlw");
}

void draw() {
if (sound != null && sound.isPlaying()) {
background(255, 0, 0);
textFont(font);
text("Sound is playing", width/2, height/2);
} else {
background(0);
textFont(font);
text("Press spacebar to play a random sound", width/2, height/2);
}
}

void keyPressed() {
if (key == ' ') {
int index = int(random(sounds.length));
sound = sounds[index];
sound.play();
}
}

NullPointerException
Could not run the sketch (Target VM failed to initialize).
For more information, read Help → Troubleshooting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions