Hi All,
MyTask: I want play a video file in media player or videoview when the device is being shaken(accelerometer triggers).
1. I'm able to get notification whenever accelerometer is triggered on the device using the following code separately.
2. And i'm able to play my video file from sdcard using the following code separately.
I'm now integrating both to play my video file when the accelerometer has triggered(whether device moved or shaked or etc). But when it was working both the functionality separately, is not working when i integrated both code to work as per my MyTask. After integrating the code, i tested by shaking my device to observe starting video, but it throws error as "The application has stopped unexpectedly. Please try again'.
Can some one please look at my code below and suggest how do improve it and resolve it based on your experince and view?
Integrated Code for both Accelerometer access and Playing Video when device is triggered accelerometer: [NOTE: The same code when i executed separately, worked fine for both cases(Accelerometer access and Playing Video)]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// start motion detection
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
boolean accelSupported = sensorMgr.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);
if (!accelSupported) {
// on accelerometer on this device
sensorMgr.unregisterListener( this,
SensorManager.SENSOR_ACCELEROMETER);
}
}
protected void onPause() {
if (sensorMgr != null) {
sensorMgr.unregisterListener(this,
SensorManager.SENSOR_ACCELEROMETER);
sensorMgr = null;
}
super.onPause();
}
public void onAccuracyChanged(int arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(int sensor, float[] values) {
Log.d("sensor", "onSensorChanged: " + sensor);
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
x = values[SensorManager.DATA_X];
y = values[SensorManager.DATA_Y];
z = values[SensorManager.DATA_Z];
float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;
// WHICH MEANS ACCELEROMETER HAS TRIGGERED ....
if (speed > SHAKE_THRESHOLD && enagaged==false) {
enagaged=true;
Log.d("sensor", "shake detected w/ speed: " + speed);
// PLAY VIDEO FILE FROM SDCARD .....
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
myVideo=(VideoView)findViewById(R.id.VideoView01);
myVideo.setVideoPath(filename); // filename = "file:///sdcard/myvideo.3gp"
mc=new MediaController(this);
mc.setMediaPlayer(myVideo);
myVideo.setMediaController(mc);
myVideo.requestFocus();
}
last_x = x;
last_y = y;
last_z = z;
}
}


