I've managed to get Apwidgets working on my sketch but I have come across some problems. My sketch is fairly simple. Wherever you touch on the Android phone, a ring appears that starts small, then grows and grdually fades to disappear. I wanted to create an instrument with a visual by adding pitch along the y axis.
I started simple, just using 2 samples ie. the left side of the screen plays "note0", the right, "note1". So here's where my problem arose:
When I touch the screen the sample plays, but until the sample is finished, the visual freezes. I can't figure out why. Any ideas? I hope this is something I have coded incorrectly. I have added my code below for you to see.
Secondly, is it possible to play multiple samples at one time with Apwidgets? I'm not sure if you can, in which case, I am going to have to seek an alternative way to create my instrument. Any suggestions would great.
I hope someone can help, Here's my code:
import apwidgets.*;
APMediaPlayer player;
ArrayList ringarray;
int nte;
int transp;
void setup() {
size(480, 800);
orientation(PORTRAIT);
ringarray = new ArrayList(); // Create an empty ArrayList
player = new APMediaPlayer(this); //create new APMediaPlayer
player.setMediaFile("note0.mp3"); //set the file (files are in data folder)
player.setMediaFile("note1.mp3"); //set the file (files are in data folder)
player.setLooping(false); //restart playback end reached
player.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0
}
void mousePressed() {
// A new ring object is added to the ArrayList, by default to the end
ringarray.add(new Ring(mouseX, mouseY));
if (nte == 0) {
player.setMediaFile("note0.mp3");
}
else if (nte == 1) {
player.setMediaFile("note1.mp3");
}
}
void draw() {
background(135, 206, 250);
for (int i = 0; i < ringarray.size(); i = i + 1) {
Ring ringo = (Ring) ringarray.get(i); // pull the ring out of the "hat"
ringo.move(); // tell the ring to move
ringo.display(); // draw the ring
if (ringo.finished()) {
// Items can be deleted with remove()
ringarray.remove(i); // if ring has exceeded its size, the boolean variable finished is set to true
}
}
}
public void onDestroy() {
super.onDestroy(); //call onDestroy on super class
if(player!=null) { //must be checked because or else crash when return from landscape mode
player.release(); //release the player
}
}
// The Class
class Ring {
float xpos, ypos, speed;
float ringsize, transparent;
boolean finished;