accelormeter to play sounds when sensor detects

Quickly share your Android Code Snippets here...

accelormeter to play sounds when sensor detects

Postby developdude27 » Tue Jan 17, 2012 4:28 am

ok so i have worked out the code for accelerometer when detected to change colors. how do i program it to make sounds when motion is dected in that fashion. any help would be fantastic. my guess is possibly if i switch out the view.setBackgroundColor(Color.GREEN). to say play a sound file from raw file. or do i have to create a media player and somehow make it play on accel dection. pieceing these two together have been challenging please help!!
here is what i have...

package matt.Accelerometer.android.research;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class AccelerometerActivity extends Activity implements SensorEventListener { // added implements sensor event.
private SensorManager sensorManager;
private boolean color = true;
private View view;
private long lastupdate;

public void onCreate(Bundle savedinstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0);

super.onCreate(savedinstanceState);
setContentView(R.layout.main);
view = findViewById(R.id.textview);
view.setBackgroundColor(Color.GREEN);

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
lastupdate = System.currentTimeMillis();
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//register this class as a listener for the orientation and accelerometer sensors
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
}


@Override
protected void onPause() {
// TODO Auto-generated method stub
sensorManager.unregisterListener(this);
super.onStop();
}

public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float[] values = event.values;
//movement
float x = values[0];
float y = values[1];
float z = values[2];

float accelationSquareRoot = (x*x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
if (accelationSquareRoot >=2)
{ if (actualTime - lastupdate < 200) {
return;
}
lastupdate = actualTime;
Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT).show();
if (color) {
view.setBackgroundColor(Color.GREEN);// import color and capitalize color...note to self on correction
}else {
view.setBackgroundColor(Color.RED);
}
color = !color;
}

}
}
}
developdude27
Freshman
Freshman
 
Posts: 9
Joined: Mon Jan 09, 2012 5:14 am

Top

Re: accelormeter to play sounds when sensor detects

Postby marecek » Wed Feb 29, 2012 8:02 pm

Make a mediaPLayer and use the Play() function to play it when you change the color. But remember to load the music into the player.
marecek
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Feb 19, 2012 7:13 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 5 guests