Thanks for this example. It helped me a lot to get sound out of my project (sorry for my bad english).
For short sounds (10 sec.) i found out its prefer to use Anroids SoundPool Class.
It is easier to code and sounds better if you want to play several sounds on top of each other (maybe in gameapp or soundboard like your example).
Than your code look like this:
// !!!modified!!! -> original code by Gord Payne - with help from 'kfrajer'
// on the discourse.processing.org/c/processing-android forum
import android.media.SoundPool;
import android.media.AudioManager;
import android.content.res.AssetFileDescriptor;
import android.content.Context;
import android.app.Activity;
SoundPool sp; //
Context context; // these two objects determine the file access path to the sound files
Activity act;
AssetFileDescriptor afd1, afd2, afd3, afd4; // 4 file path descriptors
int s1, s2, s3, s4; // IDs for the sounds to handle in the soundpool
void setup() {
fullScreen();
orientation(PORTRAIT);
act = this.getActivity();
context = act.getApplicationContext();
// first param. set how many sounds to play at same time
// Android wants to use a builder, but the old constructor also works
sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
// the volume buttons contol the stream which is currently being played, otherwise the ringtonevolume.
// this call bind the Volumebuttons on the activity.
act.setVolumeControlStream(AudioManager.STREAM_MUSIC);
try {
// the easest way to load resourses in android is the R. class but i did not find out how to use this.
// So i take the AssetFileDiscriptor like you.
// all your sound files are in the 'data' folder in your project folder
afd1 = context.getAssets().openFd("fogleg09.mp3");
afd2 = context.getAssets().openFd("bd04.mp3");
afd3 = context.getAssets().openFd("bugs11.mp3");
afd4 = context.getAssets().openFd("daffy10.mp3");
// my stuff to test it
//afd1 = context.getAssets().openFd("flag_v2.ogg");
//afd2 = context.getAssets().openFd("klick.ogg");
//afd3 = context.getAssets().openFd("pling.ogg");
//afd4 = context.getAssets().openFd("plopp.ogg");
s1 = sp.load(afd1, 1);
s2 = sp.load(afd2, 1);
s3 = sp.load(afd3, 1);
s4 = sp.load(afd4, 1);
}
catch(IOException e) {
}
// draw some buttons to mousePress on screen
fill(200, 0, 0); // red
rect(100, 100, 50, 50);
fill(0, 200, 0);// green
rect(100, 200, 50, 50);
fill(0, 0, 200);// blue
rect(200, 100, 50, 50);
fill(200, 0, 200);// magenta
rect(200, 200, 50, 50);
fill(100, 100, 100);// exitbutton, i want to test same other stuff on this opportunity
rect(100, 300, 50, 50);
};
void draw() {
}
void mousePressed() { // check which screen zone you picked and play that sound
if ((mouseX>100)&&(mouseX<150)&&(mouseY>100)&&(mouseY<150)) {
//play the sound; params: volume r, volume l, loop (-1) or not(0), ?, up or down the samplerate
sp.play(s1, 1.0, 1.0, 0, 0, 1);
}
if ((mouseX>100)&&(mouseX<150)&&(mouseY>200)&&(mouseY<250)) {
sp.play(s2, 1.0, 1.0, 0, 0, 1);
}
if ((mouseX>200)&&(mouseX<250)&&(mouseY>100)&&(mouseY<150)) {
sp.play(s3, 1.0, 1.0, 0, 0, 1);
}
if ((mouseX>200)&&(mouseX<250)&&(mouseY>200)&&(mouseY<250)) {
sp.play(s4, 1.0, 1.0, 0, 0, 1);
}
if ((mouseX>100)&&(mouseX<150)&&(mouseY>300)&&(mouseY<350)) {
act.finish();
}
}
// housekeeping
void onPause() {
super.onPause();
if(sp != null){
sp.stop(s1);
sp.stop(s2);
sp.stop(s3);
sp.stop(s4);
sp.release();
sp = null;
}
}
void onStop() {
super.onStop();
if(sp != null){
sp.release();
sp = null;
}
}
// !!!modified!!! -> original code by Gord Payne - with help from 'kfrajer'
// on the discourse.processing.org/c/processing-android forum
import android.media.SoundPool;
import android.media.AudioManager;
import android.content.res.AssetFileDescriptor;
import android.content.Context;
import android.app.Activity;
SoundPool sp; //
Context context; // these two objects determine the file access path to the sound files
Activity act;
AssetFileDescriptor afd1, afd2, afd3, afd4; // 4 file path descriptors
int s1, s2, s3, s4; // IDs for the sounds to handle in the soundpool
void setup() {
fullScreen();
orientation(PORTRAIT);
act = this.getActivity();
context = act.getApplicationContext();
// first param. set how many sounds to play at same time
// Android wants to use a builder, but the old constructor also works
sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
// the volume buttons contol the stream which is currently being played, otherwise the ringtonevolume.
// this call bind the Volumebuttons on the activity.
act.setVolumeControlStream(AudioManager.STREAM_MUSIC);
try {
// the easest way to load resourses in android is the R. class but i did not find out how to use this.
// So i take the AssetFileDiscriptor like you.
// all your sound files are in the 'data' folder in your project folder
afd1 = context.getAssets().openFd("fogleg09.mp3");
afd2 = context.getAssets().openFd("bd04.mp3");
afd3 = context.getAssets().openFd("bugs11.mp3");
afd4 = context.getAssets().openFd("daffy10.mp3");
// my stuff to test it
//afd1 = context.getAssets().openFd("flag_v2.ogg");
//afd2 = context.getAssets().openFd("klick.ogg");
//afd3 = context.getAssets().openFd("pling.ogg");
//afd4 = context.getAssets().openFd("plopp.ogg");
s1 = sp.load(afd1, 1);
s2 = sp.load(afd2, 1);
s3 = sp.load(afd3, 1);
s4 = sp.load(afd4, 1);
}
catch(IOException e) {
}
// draw some buttons to mousePress on screen
fill(200, 0, 0); // red
rect(100, 100, 50, 50);
fill(0, 200, 0);// green
rect(100, 200, 50, 50);
fill(0, 0, 200);// blue
rect(200, 100, 50, 50);
fill(200, 0, 200);// magenta
rect(200, 200, 50, 50);
fill(100, 100, 100);// exitbutton, i want to test same other stuff on this opportunity
rect(100, 300, 50, 50);
};
void draw() {
}
void mousePressed() { // check which screen zone you picked and play that sound
if ((mouseX>100)&&(mouseX<150)&&(mouseY>100)&&(mouseY<150)) {
//play the sound; params: volume r, volume l, loop (-1) or not(0), ?, up or down the samplerate
sp.play(s1, 1.0, 1.0, 0, 0, 1);
}
if ((mouseX>100)&&(mouseX<150)&&(mouseY>200)&&(mouseY<250)) {
sp.play(s2, 1.0, 1.0, 0, 0, 1);
}
if ((mouseX>200)&&(mouseX<250)&&(mouseY>100)&&(mouseY<150)) {
sp.play(s3, 1.0, 1.0, 0, 0, 1);
}
if ((mouseX>200)&&(mouseX<250)&&(mouseY>200)&&(mouseY<250)) {
sp.play(s4, 1.0, 1.0, 0, 0, 1);
}
if ((mouseX>100)&&(mouseX<150)&&(mouseY>300)&&(mouseY<350)) {
act.finish();
}
}
// housekeeping
void onPause() {
super.onPause();
if(sp != null){
sp.stop(s1);
sp.stop(s2);
sp.stop(s3);
sp.stop(s4);
sp.release();
sp = null;
}
}
void onStop() {
super.onStop();
if(sp != null){
sp.release();
sp = null;
}
}