0

I have an application, where i have a video player, with videos. If i keep my finger on the screen for more than 0,4 seconds, it finishs this activity, starts the recorder activity, and automatically starts recording. My question is: can i make the second activity recognise when i take my finger off the screen, so that is would stop recording?

This is called when i keep the screen pressed:

 Intent i = new Intent(VideoPlayerActivity.this, VideoRecorderActivity.class);

                    Bundle extras = new Bundle();
                    extras.putInt("mode", VideoRecorderModes.SEND_ALL_MODE);
                    extras.putInt("index", index);
                    extras.putInt("touches", 2);
                    i.putExtras(extras);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

                    mPreview.getHolder().removeCallback(VideoPlayerActivity.this);
                    mPreview = null;                        
                    startActivity(i);
                    VideoPlayerActivity.this.finish();

This is called in my second activity, after the camera is ready:

  touches = getIntent().getExtras().getInt("touches");
    if (touches == 1) {
        mySurfaceView.setPressed(true);
        recordButton.setEnabled(false);
        okButton.setEnabled(false);

        recordButton.setImageResource(R.drawable.shutter_pressed);

        startTime = System.currentTimeMillis();
        timer = new Timer();
        timer.schedule(new TimerDisplayTask(), 0, 500);

        startRecording();
        touches = 0;
        System.out.println("----------------here----------");
    } 

This is the SurfaceView onTouch method for the surface view from the second activity:

mySurfaceView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.i(TAG, "event : " + event.getAction() + "||is?: " + isRecording);
            Log.i(TAG, "touches:" + touches);
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //DO NOTHING
                break;
            case MotionEvent.ACTION_UP:
                if (touches == 1 && isRecording) {
                    Log.i(TAG, "touch up");
                    timer.cancel();
                    timer.purge();
                    if (isRecording) {
                        mySurfaceView.setEnabled(false);
                        Log.i("VideoRecorder blabla", "touche enabled record, ok ,surface: " + recordButton.isEnabled() + "| " + okButton.isEnabled() + "|" + mySurfaceView.isEnabled());
                        stopRecording();
                        recordButton.setImageResource(R.drawable.shutter);
                        touches = 0;
                    }
                } 
                break;
            }
            return false;
        }
    });
rosu alin
  • 5,674
  • 11
  • 69
  • 150

2 Answers2

1

I've done a little research on this answer as I've also ran into this question myself.

It unfortunately appears that the onTouchEvent won't fire off until there's an ACTION_DOWN event in the activity it is watching (This is from observation and testing alone please let me know if I'm wrong). So I believe what you want to do isn't possible with the route you want to take.

I haven't tested it, but if you use the same activity and swap out different fragments I believe the onTouchEvent won't be reset so this method could be a possible solution to your problem.

Brandon Romano
  • 1,022
  • 1
  • 13
  • 21
  • I've thought about using Fragments, but the problem is that both activities have the touchEvent fixed on a SurfaceView, and when i initialize them again (changing from videoview, to recorder, it defocalizez and so, it loses the touch event) – rosu alin Apr 18 '13 at 14:06
  • Hmmph. I'm going to do a little more research. If I end up finding a good solution I'll share it. – Brandon Romano Apr 18 '13 at 19:34
0

I actually managed to do this with a Fragments, using a FragmentManager as the main activity, and implementing the onTouchListener into the Activity, and then passing the commands i need to both the fragments from this activity;

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (Constants.gestureEnabled) {
        switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            Log.i(TAG, "current fragment: " + currentFragment);
            if (currentFragment.toString().contains("VideoPlayerFragment")) {
                if (currentFragment instanceof VideoPlayerFragment) {
                    ((VideoPlayerFragment) currentFragment).onActionDown(event.getX());
                }
            } else if (currentFragment.toString().contains("VideoRecorderFragment")) {
                if (currentFragment instanceof VideoRecorderFragment) {
                    ((VideoRecorderFragment) currentFragment).onActionDown();
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            if (currentFragment.toString().contains("VideoPlayerFragment")) {
                if (currentFragment instanceof VideoPlayerFragment) {
                    ((VideoPlayerFragment) currentFragment).onActionUp();
                }
            } else if (currentFragment.toString().contains("VideoRecorderFragment")) {
                if (currentFragment instanceof VideoRecorderFragment) {
                    ((VideoRecorderFragment) currentFragment).onActionUp();
                }
            }
            break;
        default:
            break;
        }
    }
    return gDetector.onTouchEvent(event);
}
rosu alin
  • 5,674
  • 11
  • 69
  • 150