Problem Playing an Audio File/Clip
in
Android Processing
•
3 years ago
I'm attempting to work with Android's SoundPool() in Processing. The two separate files below are in the same Processing project. I can either use the Processing IDE itself or ant to compile and install them to my phone but soundPool.play() doesn't seem to work properly either way - no sounds are played and it returns a zero, which indicates a failure. I followed this example:
http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html. I've played with MediaPlayer() as well but still no sound is played.
I've searched but couldn't find a solution for soundPool.play() returning a zero. Has anyone had any success with either soundPool() or MediaPlayer() in Processing for Android?
I have yet to try the Pd libraries for Android but that is next on the list.
Much thanks,
Gary
Here is my file soundPool.java (exported from a .pde)
- package processing.android.test.soundpool;
- import processing.core.*;
- import processing.xml.*;
- import android.content.pm.ActivityInfo;
- import android.os.Handler;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Message;
- import android.os.Process;
- import android.content.Context;
- import android.media.SoundPool;
- import android.media.MediaPlayer;
- import android.media.AudioManager;
- import android.media.AudioFormat;
- import android.view.MotionEvent;
- import android.view.KeyEvent;
- import android.graphics.Bitmap;
- import java.io.*;
- import java.util.*;
- public class soundPool extends PApplet {
- // Declarations
- int counter = 0;
- int SoundInt = 0;
- SoundPoolSoundManager s;
- public void setup(){
- background(255);
- // Volume controls will be used for audio, not ringer volume.
- setVolumeControlStream(AudioManager.STREAM_MUSIC);
- s = new SoundPoolSoundManager(this);
- s.init(); // Initialize soundPool before using it.
- }
- public void draw(){
- counter = counter + 1; // Increment counter
- print("Time = " + counter + "s"); // Print some info so we know we're running
- delay(1000); // Wait 1s.
- if (counter == 6){ // Imposes 6s delay.
- s.playSound(1);
- print("playsound");
- }
- }
- public int sketchWidth() { return 480; }
- public int sketchHeight() { return 800; }
- public String sketchRenderer() { return A3D; }
- }
Here is my SoundPoolManager.java file:
- package processing.android.test.soundpool;
- import android.content.pm.ActivityInfo;
- import android.os.Handler;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Message;
- import android.os.Process;
- import android.content.Context;
- import android.media.SoundPool;
- import android.media.MediaPlayer;
- import android.media.AudioManager;
- import android.media.AudioFormat;
- import processing.core.*;
- import processing.xml.*;
- import java.io.*;
- import java.util.*;
- import java.text.*;
- import java.lang.Object;
- import java.util.ArrayList; // May not be needed
- import java.util.List;
- //public class SoundPoolSoundManager implements SoundManager{ // Erro: SoundManager not found
- public class SoundPoolSoundManager{ // Can I declare class without SoundManager?
- // Following http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html
- // Class Declarations:
- private SoundPool soundPool;
- private HashMap<Integer, Integer> soundPoolMap; // Use a HashMap to organize image files
- private Context context;
- private int soundId;
- public SoundPoolSoundManager(Context context){
- this.context = context;
- }
- // Initialize soundPool:
- public void init() {
- if (soundPool != null) {
- soundPool.release();
- soundPool = null;
- }
- // Initialize soundPool:
- // max streams, stream type, source quality
- soundPool = new SoundPool(24, AudioManager.STREAM_MUSIC, 0);
- soundPoolMap = new HashMap<Integer, Integer>();
- // Commented this out since I load the resource below during playSound.
- //soundPoolMap.put(1, soundPool.load(this.context, R.raw.applewav, 1));
- }
- public void playSound(int sound) {
- int soundInt = 1; // Set this to non-zero for now.
- if (soundPool != null){ // Check if soundPool is alive.
- AudioManager mgr = (AudioManager)
- context.getSystemService(Context.AUDIO_SERVICE);
- // See http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html
- float streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
- streamVolume = streamVolume / mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
- //soundId = soundPoolMap.get(sound);
- soundId = soundPool.load(this.context, R.raw.applewav, 1);
- // soundID, left volume, right volume, priority, loop, playback rate
- soundInt = soundPool.play(soundId, streamVolume, streamVolume, 1, 0, 1f);
- }
- }
- }
1