Play Video file when accelerometer is triggered code error:

All your problems with Audio, Video and Images.

Play Video file when accelerometer is triggered code error:

Postby jack » Fri Oct 16, 2009 9:28 pm

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;
}
}
jack
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 16, 2009 9:09 pm

Top

Re: Play Video file when accelerometer is triggered code err

Postby jack » Fri Oct 16, 2009 9:56 pm

UPDATE from myself about this:

I RESOLVED IT BY MYSELF BY REMOVING "requestWindowFeature(Window.FEATURE_NO_TITLE); " line.

Now I have other queries too related to the same thing.
1. If i want to play my video always in Landscape mode, how to achieve this?
2. If i want to play a video file continuously(play video in looping) even after stopped one cycle, how to achieve it?
3. if i want to remove built-in media player overlay controls such as 'Play', 'Rewind', 'Forward' buttons from the media player which i'm using to play a video file from sdcard on VideoView, how to achieve it?

I tried many ways, couldn't be able to resolve the above issues.

Could some one if have experience on this resolving it, please share your suggestions with me as i'm new to this development?
jack
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 16, 2009 9:09 pm

Postby Beros_ » Fri Jan 29, 2010 4:51 pm

Hi!

I noticed that if you are doing some code on sensor event, that this can generate a lot of CPU usage. try to create separate thread to do stuff every 0.1 sec for example, but in accelerometer function only update variable of accelerometer sensor. Try to set sensor delay to NORMAL or FASTEST.

I had that problem while loading images where I loaded image depending of where phone was located...


MY PROBLEM:

I would like to locate video player to some time in video. This works but when I want to pause it, it fails. And if I rewind it with pause, it doesn't refresh a screen.

Second problem is that while capturing photos, sometimes photo is in green tones. Does anybody know why?
Beros_
Freshman
Freshman
 
Posts: 7
Joined: Fri Jan 29, 2010 4:29 pm
Location: Croatia

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 18 guests