如何使用google-cardboard的磁性按钮在手机中播放/暂停视频?

时间:2016-03-26 05:35:21

标签: android video google-cardboard rajawali rajawalivr

我正在研究RajawaliVR的演示,我想使用google-cardboard的磁性按钮播放/暂停视频。所以,我如何才能实现这一目标,请帮忙。

1 个答案:

答案 0 :(得分:1)

您可以使用我的帮助程序类作为监听按钮的服务,并通过接口类将一些操作发布到主活动。这是代码

/**
 * 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) {

        }


    }