Accessing sound in Android

I am working on a control application using Android as compiled from a Processing sketch. I need to use the Android sound device, just to produce an alarm buzz sound. Any idea how to access this device?
Thanks

Hi this works with java mode I did not try it with android mode

int tick = 10;
    for (int i = 0; i < tick; ++i) {
     println("Beep : " + i);
      // Ring the bell again using the Toolkit 
      java.awt.Toolkit.getDefaultToolkit().beep();
      try {
        Thread.sleep(1011); // introduce delay
      } catch (InterruptedException e) {
      }
    }

With APDE didn’t work but may works with processing android mode try it yourself

https://www.codercrunch.com/question/2123074635/how-generate-beep-java

Hello @MPROCD

The SimplePlayback example worked on my Android Phone:

:)

1 Like

Thank you for the idea to use the sound library. I managed to work with one of the examples.

1 Like

Thank you. Unfortunately, does not work with Android.

The following code works on my Android phone using Processing 3.5.4. You will need to create a folder named ‘data’ in your project folder and place your mp.3 alarm file inside the data folder.

/*
 * Android Audio Player example, copied from https://forum.processing.org/two/discussion/12819/how-do-i-get-sound-to-play-on-the-phone
 * Some audio formats seem not to be recognized (maybe some MP3-codecs as well), put the audio file into the data folder.
 */

import android.media.MediaPlayer;
import android.content.res.AssetFileDescriptor;
import android.content.Context;
import android.app.Activity;

MediaPlayer mp;
Context context; 
Activity act;
AssetFileDescriptor afd;

color BLUE = color(64,124,188);
color LTGRAY = color(185,180,180);
color YELLOW = color(245,250,13);
color RED = color(255,0,0);
color BLACK = color(0,0,0);
color WHITE = color(255,255,255);
 
Button _quit;
Button _playBtn;
 
class Button {
 float x, y, w, h;
 String title;
 color btnColor;
 color txtColor;
 
 // Constructor
 Button(int xpos, int ypos, float wt, float ht, String titleStr, color background, color foreground) {
   x = xpos;
   y = ypos;
   w = wt;
   h = ht;
   title = titleStr;
   btnColor = background;
   txtColor = foreground;
 }
 
 void display(){
   fill(btnColor); // button color
   noStroke();
   rect( x, y, w, h, 15); // rounded rect
   fill(txtColor); // text color
   textSize(42);
   text(title, x + 15, y + 15, w, h);
 }
}

void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  background(BLUE);
  _quit = new Button( width - 300, 60, 200, 80, "Quit", LTGRAY, BLACK);
  _playBtn = new Button( 300, 180, 300, 80, "Play Sound", YELLOW, BLACK);
  
  act = this.getActivity();
  context = act.getApplicationContext();
  try {
    mp = new MediaPlayer();
    afd = context.getAssets().openFd("alarm.mp3");//which is in the data folder
    println("Successfully loaded audio file");
    mp.setDataSource(afd.getFileDescriptor());
    mp.prepare();
  } 
  catch(IOException e) {
    println("file did not load");
  }
}

void draw() {
   _quit.display();
  _playBtn.display();
}

public void pause() {
  super.pause();
  if (mp !=null) {
    mp.release();
    mp = null;
  }
};

public void stop() {
  super.stop();
  if (mp !=null) {
    mp.release();
    mp = null;
  }
}

void mousePressed(){
 if((mouseX >= _quit.x) && (mouseX <= _quit.x + _quit.w) && (mouseY >= _quit.y) && (mouseY <= _quit.y + _quit.h)){
  exit();
 }
 
 if((mouseX >= _playBtn.x) && (mouseX <= _playBtn.x + _playBtn.w) && (mouseY >= _playBtn.y) && (mouseY <= _playBtn.y + _playBtn.h)){
   mp.start();
  println("You hit the _playBtn.");
 }
}
1 Like

Thank you for trying to help. Meantime I could use one of the examples from the sound library.

1 Like