Search code examples
androidvideogoogle-cardboardrajawalirajawalivr

How to play/pause video in phone using magnetic button of google-cardboard?


I am working on demo of RajawaliVR and I want to play/pause video using magnetic button of google-cardboard.So, how I can achieve this please help.


Solution

  • you can use my helper classes its a service to listen button and some actions are posted via interface class to main activity. Here is the code

    /**
     * Created by volkanugur
     *
     *    WORK WITH CARDBOARD VERSION 1
     *    =============================
     *               MAGNET
     *    ==========  N-S  ============
     */
    
    
        public class CardBoardButtonHelper extends Service implements SensorEventListener {
    
            private SensorManager sensorManager = null;
            private float __SENSOR_OLD_POSITION__ = -1;
            long timestamp = 0;
            long timestamp_ee = 0;
            Singleton __ = Singleton.getInstance();;
    
    
            @Nullable
            @Override
            public IBinder onBind(Intent intent) {
                return null;
            }
    
            @Override
            public void onCreate() {
                super.onCreate();
                initsensor();
            }
    
    
            private void initsensor() {
    
                sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    
                // Register magnetic sensor
                sensorManager.registerListener(this,
                        sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                        SensorManager.SENSOR_DELAY_UI);
    
    
            }
    
    
            @Override
            public void onSensorChanged(SensorEvent event) {
    
                synchronized (this) {
    
                    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
    
                        float X = event.values[0];
    
                        Log.e("Sens",X+"");
    
                        if (__SENSOR_OLD_POSITION__!=-1) {
                            if (Math.abs(X - __SENSOR_OLD_POSITION__) > 50) {
                                if (Math.abs(timestamp_ee-timestamp)>1300) {
                                    Vibrator v = (Vibrator) __.get__MAIN__().getSystemService(Context.VIBRATOR_SERVICE);
                                    v.vibrate(500);
                                    ((CardButtonTrigger) (__.get__MAIN__())).CardButton_Triggered();
                                    timestamp_ee = System.currentTimeMillis();
                                }
                                timestamp = System.currentTimeMillis();
                            }
                        }
                        __SENSOR_OLD_POSITION__ = X;
                    }
    
                }
            }
    
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
            }
    
    
        }