A sound play exemplar please?

Hi Gord

You need to learn how to format your code in the forum. It is easy. Enclose your code using three back ticks like this:

```java
[<Your Code>]
```

You can change java for any other language (C, python, etc.)

****EDIT
Related to your last post. I believe apwidgets is not being supported so I am not sure if it will work. I have also tested your code. You can see it at the end of this post. I just edited this post to talk about your two examples above.

Back to your question. This link provides some code: Using Android MediaPlayer to play sound. - Processing 2.x and 3.x Forum

This next code is tested using P3.37 and Android Mode v4.0.1 which is from the previous link, slightly modified and tested on Samsung Galaxy S8 running on Oreo(Android version 8.0 / API 26). Make sure your mp3 file is in the data folder.

import android.media.MediaPlayer;
import  android.content.res.Resources;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.content.Context;
import android.app.Activity;
import android.os.Bundle;
 
MediaPlayer snd = new MediaPlayer();
//AssetManager assets = this.getAssets();
AssetFileDescriptor fd;
Context context;
Button bPlay;
Activity act;
 
void setup()
{
  colorMode(HSB);
  size(800, 800);
  act = this.getActivity();
  context = act.getApplicationContext();
  try {
 
    fd = context.getAssets().openFd("test-sound.mp3");
    snd.setDataSource(fd.getFileDescriptor(),fd.getStartOffset(), fd.getLength());
  }
  catch (IllegalArgumentException e) {
    e.printStackTrace();
  }
  catch (IllegalStateException e) {
    e.printStackTrace();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
 
  bPlay = new Button(width/2, height/2, 500, color(0, 125, 255));
}
 
void draw()
{
  background(210);
  bPlay.draw();
  textSize(30);
  textAlign(CENTER);
  fill(0, 21, 75);
  text("Press for replacement", width/2, height/2);
}
 
void mousePressed()
{
  if (bPlay.buttonCheck() && !snd.isPlaying()) {
    bPlay.c = color(0, 125, 220);
    try {
      snd.prepare();
      snd.start();
      //fd = context.getAssets().openFd("Ni-"+(int)random(1, 22)+".mp3");
      //snd.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
 
    }
    catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
    catch (IllegalStateException e) {
      e.printStackTrace();
    } 
    catch (IOException e) {
      e.printStackTrace();
    }
    //fd = context.getAssets().openFd("Ni-"+(int)random(1, 22)+".mp3");
  }
}
 
void mouseReleased()
{
  bPlay.c = color(0, 125, 255);
 
}
 

 
 
////////////
 
class Button
{
  int x, y, w, h, r;
  color c;
 
  Button(int x, int y, int w, int h, color c) //rectangular buttons
  {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.c = c;
  }
  Button(int x, int y, int r, color c) //circular buttons
  {
    this.x = x;
    this.y = y;
    this.r = r;
    this.c = c;
  }
 
  void draw()
  {
    ellipseMode(CENTER);
    noStroke();
    fill(c);
    rect(x, y, w, h);
    ellipse(x, y, r, r);
  }
 
  boolean buttonCheck()
  {
    boolean result = false;
    float disX = x - mouseX;
    float disY = y - mouseY;
    if (sqrt(sq(disX) + sq(disY)) < r/2) result = true;
    return result;
  }
}



//===========================================================================
// ANDROID ACTIVITY LIFECYCLE'S FUNCTIONS:


//  @@@@@@@@@@@@
@Override 
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
}

//  @@@@@@@@@@@@
@Override 
  public void onStart() {
  super.onStart();
}

//  @@@@@@@@@@@@
void onPause(){
  super.onPause();
  if(snd!=null){
 
    snd.release();
    snd= null;
  }
}

//  @@@@@@@@@@@@
void onStop(){
  super.onStop();
  if(snd!=null){
 
    snd.release();
    snd= null;
  }
 
}
 
//  @@@@@@@@@@@@ 
void onDestroy(){
   super.onDestroy();
  if(snd!=null){
 
    snd.release();
    snd= null;
  }
}

I have also tested the following code which is in the same link above. It uses ketai and the cassette library, which you need to installed via the Contribution manager. Sound files need to be place in a folder named sound which is placed inside the data folder.

Kf

import ketai.sensors.*;
import android.os.Bundle;

import android.os.Environment;
import cassette.audiofiles.SoundFile; 


import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;


int globalProxCtr=0;

int seln=1;
int CM_STD_WAIT=100;
int holdCtr=0;//CM_STD_WAIT;


SoundFile music; 
Boolean isPlaying=false;
int fileN;

// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


void settings() {
  fullScreen();
}


void setup() {  

  selectionUpdate(1);    

  orientation(LANDSCAPE);  
  stroke(0, 255, 0);
  strokeWeight(2);
  noFill();
  textAlign(CENTER, CENTER);
  textSize(36);
  smooth();
  noStroke();
  frameRate(30);

  rectMode(CORNER);
  ellipseMode(CENTER);
}

// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


void draw() { 
  background(92);

  if (isPlaying) {
    fill(255, 20, 20);
    //noStroke();
    ellipse(width/2, height/2, width*0.3, height*0.3);
  }

  if (holdCtr>0) {    
    fill(#FFFF00);
    //stroke(0, 255, 0);
    rect(width/2 -100, 0.15*height -15, 100*2, 15*2);
    fill(250, 220, 25);    
    text("Playing a random sound " + fileN + "\nThis message will disappear in "+ int(holdCtr/30), width/2, 0.05*height); 
    holdCtr--;
  }
}


// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


public void mousePressed() {

  if (isPlaying) {
    music.stop();  
    isPlaying=false;
    holdCtr=0;
  } else {    
    selectionUpdate(int(random(4))+1);
    //music.loop();
    music.play();
    isPlaying=true;

    holdCtr=CM_STD_WAIT;
  }
} 


// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


void selectionUpdate(int s) {

  String cm_audiofile="sound/default.mp3";  //Default case 
  fileN=s;

  switch(s) {

  case 1:
    cm_audiofile="sound/snd01.amr";
    break;

  case 2:
    cm_audiofile="sound/snd02.amr"; 
    break;

  case 3:
    cm_audiofile="sound/snd03.amr";     
    break;

  case 4:
    cm_audiofile="sound/button-3.mp3";
    break;
  }

  music = new SoundFile(this, cm_audiofile);
}  

Your example above, slightly changed and tested.

import android.media.MediaPlayer;
import android.content.res.AssetFileDescriptor;
import android.content.Context;
import android.app.Activity;
import android.widget.FrameLayout;
//import android.app.Fragment;
import android.os.Bundle;

import android.os.Environment;
import android.graphics.Color;
import android.widget.Toast;
import android.os.Looper;
import android.view.WindowManager;
import android.os.Bundle;
import android.view.ViewParent;
import android.view.ViewGroup;
import android.view.View;
import android.widget.RelativeLayout;
import android.view.LayoutInflater;
import android.R.string;

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

void setup() {
  initPlay();
}

void initPlay(){
  act = this.getActivity();
  mC = act.getApplicationContext();
  try {
    mp = new MediaPlayer();
    //afd = mC.getAssets().openFd("test-sound.mp3");//which is in the data folder
    afd = mC.getAssets().openFd("button-3.mp3");
    println("Successfully loaded audio file");
    mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
    //delay(900);
    mp.prepare();
  }
  catch (IllegalArgumentException e) {
    e.printStackTrace();
  }
  catch (IllegalStateException e) {
    e.printStackTrace();
  } 
  catch(IOException e) {
    println("File did not load");
    e.printStackTrace();
  }
  
  mp.start();
};

void draw() {
}

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

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