I'm working with the proximity sensor to record a sound, when I step the finger on the sensor it starts to record, but the problem is knowing how to do when removing the finger stops recording and moves to another activity. I can play the sound). What I want to do is comment on "onSensorChanged".
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
public class Classe1 extends ActionBarActivity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mProximity;
private MediaRecorder gravador;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classe1);
gravador = new MediaRecorder();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public final void onSensorChanged(SensorEvent event) {
if(event.values[0] > 3 ){
}
else{
startRecording();
}
// stopRecording();
//Intent intent=new Intent(this, Reproduzir.class);
//startActivity(intent);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
super.onStop();
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.unregisterListener(this);
}
private void startRecording() {
gravador = new MediaRecorder();
gravador.setAudioSource(MediaRecorder.AudioSource.MIC);
gravador.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
gravador.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test2.3gp";
gravador.setOutputFile(filename);
try {
gravador.prepare();
gravador.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getBaseContext(), "A gravar....", Toast.LENGTH_SHORT).show();
}
private void stopRecording() {
gravador.stop();
gravador.release();
Toast.makeText(getBaseContext(), "Gravação concluída!!", Toast.LENGTH_SHORT).show();
}
public void close(View view) {
Intent botaofechar= new Intent(this, MainActivity.class);
startActivity(botaofechar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.micro);
getSupportActionBar().setDisplayUseLogoEnabled(true);
return true;
}
}