Flip to pause feature for android music player

83 views Asked by At

Can you help to implement flip to pause feature in Android music player? Looking for the positive reply. For more ref: Youtube video of flip to pause feature

1

There are 1 answers

0
Rahul Singh Chandrabhan On BEST ANSWER

You can detect flipping like this:

Activity Code:

public class AndroidDetectFlipping extends Activity {

    SensorManager sensorManager;
    Sensor accelerometerSensor;
    boolean accelerometerPresent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
        if(sensorList.size() > 0){
            accelerometerPresent = true;
            accelerometerSensor = sensorList.get(0);
        }
        else{
            accelerometerPresent = false;
            face.setText("No accelerometer present!");
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if(accelerometerPresent){
            sensorManager.registerListener(accelerometerListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

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

    private SensorEventListener accelerometerListener = new SensorEventListener(){

        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSensorChanged(SensorEvent arg0) {
            // TODO Auto-generated method stub
            float z_value = arg0.values[2];
            if (z_value >= 0){
                // Current condition is "FACE UP" \\
                // do your stuff according to this\\
            }
            else{
                // Current condition is "FACE DOWN" \\
                // do your stuff according to this\\
            }
        }};

}