0

我正在开发 RajawaliVR 的演示,我想使用 google-cardboard 的磁性按钮播放/暂停视频。所以,我该如何实现这一点,请帮忙。

4

1 回答 1

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

        }


    }
于 2016-04-09T09:21:24.433 回答