From the last time, you can make a sound by shaking it from side to side! ... that's it m (_ _) m Before I completed it, I thought it would be easy to make a sound by shaking it from side to side, but it's terribly annoying ... For some reason, there is no sound even if I do not shake it, or if I think that there is no sound even if I do not shake it, there is no sound even if I shake it, it causes multiple reactions, I devised various things, and finally shake it left and right Now makes a sound! Shown below.
MainActivity.java
public class MainActivity extends AppCompatActivity implements SensorEventListener {
ImageView Image1, Image2, Image3, Image4;
int i,j, Delay;
float X_Data, Y_Data, Z_Data;
private Timer timer1;
private CountUpTimerTask timerTask1;
private Handler handler1 = new Handler();
private Runnable runnable;
private final Handler handler = new Handler();
private SensorManager sensorManager;
private TextView X_Data_TextView , Y_Data_TextView , Z_Data_TextView;
SoundPool soundPool;
int mp3_1,mp3_x;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
X_Data_TextView = findViewById(R.id.X_Data_Text);
Y_Data_TextView = findViewById(R.id.Y_Data_Text);
Z_Data_TextView = findViewById(R.id.Z_Data_Text);
StartCyclicHandler();
Image1 = findViewById(R.id.image1);
Image2 = findViewById(R.id.image2);
Image3 = findViewById(R.id.image3);
Image4 = findViewById(R.id.image4);
Button startButton = findViewById(R.id.strat);
Button endButton = findViewById(R.id.End);
EditText et = findViewById(R.id.BpM);
Image1.setImageResource(R.drawable.en);
Image2.setImageResource(R.drawable.en);
Image3.setImageResource(R.drawable.en);
Image4.setImageResource(R.drawable.en);
//Allow only numbers to be entered in EditText
et.setInputType(InputType.TYPE_CLASS_NUMBER);
//Convert input value to int type
String bpm = ((EditText) findViewById(R.id.BpM)).getText().toString();
int BPM = Integer.parseInt((bpm));
//The one you need to add sound effects
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
} else {
AudioAttributes attr = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
soundPool = new SoundPool.Builder()
.setAudioAttributes(attr)
.setMaxStreams(5)
.build();
}
mp3_1 = soundPool.load(this, R.raw.pop, 1);
mp3_x = soundPool.load(this,R.raw.feed1,1 );
//STARTButton
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
i = 0;
String bpm = ((EditText) findViewById(R.id.BpM)).getText().toString();
int BPM = Integer.parseInt((bpm));
Delay = 60000 / BPM;
if (null != timer1) {
timer1.cancel();
timer1 = null;
}
Image1.setImageResource(R.drawable.en);
Image2.setImageResource(R.drawable.en);
Image3.setImageResource(R.drawable.en);
Image4.setImageResource(R.drawable.en);
timer1 = new Timer();
timerTask1 = new CountUpTimerTask();
timer1.schedule(timerTask1, 0, Delay);
}
});
//ENDButton
endButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (null != timer1) {
timer1.cancel();
timer1 = null;
}
Image1.setImageResource(R.drawable.en);
Image2.setImageResource(R.drawable.en);
Image3.setImageResource(R.drawable.en);
Image4.setImageResource(R.drawable.en);
}
});
}
class CountUpTimerTask extends TimerTask {
@Override
public void run() {
handler1.post(new Runnable() {
@Override
public void run() {
if (i == 0) {
Image1.setImageResource(R.drawable.eng);
Image4.setImageResource(R.drawable.en);
soundPool.play(mp3_1, 2, 2, 0, 0, 1f);
i++;
} else if (i == 1) {
Image1.setImageResource(R.drawable.en);
Image2.setImageResource(R.drawable.eng);
soundPool.play(mp3_1, 2, 2, 0, 0, 1f);
i++;
} else if (i == 2) {
Image2.setImageResource(R.drawable.en);
Image3.setImageResource(R.drawable.eng);
soundPool.play(mp3_1, 2, 2, 0, 0, 1f);
i++;
} else {
Image3.setImageResource(R.drawable.en);
Image4.setImageResource(R.drawable.eng);
soundPool.play(mp3_1, 2, 2, 0, 0, 1f);
i = 0;
}
}
});
}
}
@Override protected void onResume() {
super.onResume();
//Event Listener registration
Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override protected void onPause() {
super.onPause();
//Event Listener unregistered
sensorManager.unregisterListener(this);
StoptCyclicHandler();
}
@Override public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
X_Data = (500+(event.values[0]*25));
Y_Data = (500+(event.values[1]*25));
Z_Data = (500+(event.values[2]*25));
}
}
protected void StartCyclicHandler(){
runnable = new Runnable() {
@Override
public void run() {
X_Data_TextView.setText(String.format("%.3f",X_Data));
Y_Data_TextView.setText(String.format("%.3f",Y_Data));
Z_Data_TextView.setText(String.format("%.3f",Z_Data));
if(X_Data>800){
soundPool.play(mp3_x, 2, 2, 0, 0, 1f);
}
else if (X_Data<150){
soundPool.play(mp3_x, 2, 2, 0, 0, 1f);
}
}
handler.postDelayed(this,200); //200ms delay
}
};
handler.post(runnable);
}
protected void StoptCyclicHandler(){
handler.removeCallbacks(runnable);
}
@Override public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
What I added this time is the part that handles the accelerometer and the part that makes a sound when shaken left and right (that's right). The point that I devised in the part that swings from side to side is to add a delay. By adding a delay, the sounds are no longer duplicated.
Recommended Posts