# Processing.sound SoundFile in Android

**URL:** <https://discourse.processing.org/t/processing-sound-soundfile-in-android/36625>\
**Category:** Processing for Android\
**Created:** [April 27, 2022, 2:20pm UTC](https://discourse.processing.org/t/processing-sound-soundfile-in-android/36625 "2022-04-27T14:20:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JamesLEE](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@JamesLEE](https://discourse.processing.org/u/JamesLEE)\
**Post date:** [April 27, 2022, 2:20pm UTC](https://discourse.processing.org/t/processing-sound-soundfile-in-android/36625/1 "2022-04-27T14:20:18Z")

</div>

Hello Guys  
I am very new both processing and android.  
I would use an example “beat detection” using processing.sound in android.  
I want to vibrate my phone when beat is detected as shown below code.  
Can anyone help me to fix below my code?  
Any example codes will be greateful!!  
Thank you in advance for your fruitful advise and any help.  
James.

```auto
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;

import apwidgets.APMediaPlayer;
import processing.core.PApplet;
import processing.sound.FFT;
import processing.sound.SoundFile;
import processing.sound.Waveform;

public class Sketch extends PApplet {
    Activity act;
    Context mC;
    APMediaPlayer player;

    Vibrator vibrator;
    int noOfSamples = 1024;

    SoundFile sample;
    Waveform waveform;
    FFT fft;
    int bands = 256;
    double detectionValue = 0.22;

    public void settings() {
        size(getScreenWidth(), getScreenHeight());
    }

    public void setup(){
        //initPlay();
        background(0);
        //background(255);

        // Load and play a soundfile and loop it.
        //https://discourse.processing.org/t/a-sound-play-exemplar-please/416
        //https://discourse.processing.org/t/a-sound-play-exemplar-please/416/5
        sample = new SoundFile(this,"Remix Bass Boosted.wav");
// player = new APMediaPlayer(this);
// player.setMediaFile("assets/Remix Bass Boosted.wav");
// sample = new SoundFile(player);
        sample.loop();

        // Create the Waveform analyzer and connect the playing soundfile to it.
        waveform = new Waveform(this, noOfSamples);
        waveform.input(sample);

        fft = new FFT(this, bands);
        fft.input(sample);

        //re-draw
        frameRate(60);
    }

    public void draw(){
        strokeWeight(2);
        noFill();
        waveform.analyze();

       // Detect beat
        boolean beatDetected = false;

        int detectionCount = 0;

        for (int i = 0; i < noOfSamples / 4; i++){
            float posValue;

            if (waveform.data[i] < 0 ) posValue = -waveform.data[i];
            else posValue = waveform.data[i];

            if( posValue > detectionValue ){
                detectionCount++;
            }
        }

       if (detectionCount > 86){
            beatDetected = true;
        }

        if (beatDetected)
        {
// vibrator = (Vibrator) getActivity.getSystemService(Context.VIBRATOR_SERVICE);
// vibrator.vibrate(100);
            if (Build.VERSION.SDK_INT >= 26){
                vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
            } else {
                vibrator.vibrate(200);
            }
            textSize(48);
            text("BEAT", 100,100);
        }
        stroke(1,1,1);
    }

    public static int getScreenWidth(){
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }
    public static int getScreenHeight(){
        return Resources.getSystem().getDisplayMetrics().heightPixels;
    }
}

```

It compiled without error but it just shut down with log from logcat as shown belows;

```auto
2022-04-27 22:56:31.338 31652-31712/com.example.tutorial E/AndroidRuntime: FATAL EXCEPTION: Animation Thread
    Process: com.example.tutorial, PID: 31652
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/jsyn/engine/SynthesisEngine;
        at processing.sound.Engine.<init>(Unknown Source:13)
        at processing.sound.Engine.getEngine(Unknown Source:6)
        at processing.sound.SoundObject.<init>(Unknown Source:10)
        at processing.sound.AudioSample.<init>(Unknown Source:0)
        at processing.sound.SoundFile.<init>(Unknown Source:0)
        at processing.sound.SoundFile.<init>(Unknown Source:1)
        at com.example.tutorial.Sketch.setup(Sketch.java:51)
        at processing.core.PApplet.handleDraw(PApplet.java:1878)
        at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:478)
        at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:518)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.jsyn.engine.SynthesisEngine" on path: DexPathList[[zip file "/data/app/com.example.tutorial-gFKqvlxGiMNBkGSk9ttFqg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.tutorial-gFKqvlxGiMNBkGSk9ttFqg==/lib/arm64, /system/lib64, /system/vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at processing.sound.Engine.<init>(Unknown Source:13) 
        at processing.sound.Engine.getEngine(Unknown Source:6) 
        at processing.sound.SoundObject.<init>(Unknown Source:10) 
        at processing.sound.AudioSample.<init>(Unknown Source:0) 
        at processing.sound.SoundFile.<init>(Unknown Source:0) 
        at processing.sound.SoundFile.<init>(Unknown Source:1) 
        at com.example.tutorial.Sketch.setup(Sketch.java:51) 
        at processing.core.PApplet.handleDraw(PApplet.java:1878) 
        at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:478) 
        at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:518) 
2022-04-27 22:56:31.353 31652-31712/com.example.tutorial I/Process: Sending signal. PID: 31652 SIG: 9

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 1, 2022, 11:30pm UTC](https://discourse.processing.org/t/processing-sound-soundfile-in-android/36625/2 "2022-05-01T23:30:43Z")

</div>

Hi

Here is how to vibrate with mouse hope it’s help

```auto
import android.app.Activity;
import android.content.Context;
import android.os.Vibrator;

Activity det;

void setup() {
  fullScreen();
  background(255, 0, 0);
  det = this.getActivity();
}

void draw() {
  background(0, 255, 0);
}

void mouseReleased() {
  Vibrator vibrer = (Vibrator) det.getSystemService(Context.VIBRATOR_SERVICE);
  vibrer.vibrate(300);
}

```
