I want to Play and Stop my Music with a Double Tap, but why my Code don’t Work?
import android.media.MediaPlayer;
import android.content.res.AssetFileDescriptor;
import android.content.Context;
import android.app.Activity;
import android.widget.FrameLayout;
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;
int wait = 200, lastTime = -wait;
boolean music;
Activity act;
Context mC;
MediaPlayer mp;
AssetFileDescriptor afd;
void setup() {
music = true;
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("hm.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();
}
};
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;
}
};
void mousePressed() {
if (music=true) {
mp.start();
if (lastTime + wait > millis()) {
music=false;
} else {
lastTime = millis();
};
if (music=false) {
mp.stop();
if (lastTime + wait > millis()) {
music=true;
} else {
lastTime = millis();
}
}
}
}