Example usage for android.view SurfaceHolder addCallback

List of usage examples for android.view SurfaceHolder addCallback

Introduction

In this page you can find the example usage for android.view SurfaceHolder addCallback.

Prototype

public void addCallback(Callback callback);

Source Link

Document

Add a Callback interface for this holder.

Usage

From source file:org.digitalcampus.oppia.utils.mediaplayer.VideoPlayerActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_player);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    videoSurface = (SurfaceView) findViewById(R.id.videoSurface);
    SurfaceHolder videoHolder = videoSurface.getHolder();
    videoHolder.addCallback(this);

    player = new MediaPlayer();
    controller = new VideoControllerView(this);

    Bundle bundle = this.getIntent().getExtras();
    if (bundle != null) {
        mediaFileName = (String) bundle.getSerializable(MEDIA_TAG);
        activity = (Activity) bundle.getSerializable(Activity.TAG);
        course = (Course) bundle.getSerializable(Course.TAG);
    } else {/*from  w  w  w .  ja v  a 2  s .  co m*/
        this.finish();
    }

    try {
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource(this, Uri.parse(FileUtils.getMediaPath(this) + mediaFileName));
        player.setOnPreparedListener(this);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.artoolkit.ar.samples.nftSimple.CameraSurface.java

@SuppressWarnings("deprecation")
public CameraSurface(Context context) {
    super(context);
    Log.i(TAG, "CameraSurface(): ctor called");
    Activity activityRef = (Activity) context;

    try {//from  w  w  w.j a v a  2s.com
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activityRef,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activityRef.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activityRef.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CameraSurface(): must ask user for camera access permission");
                activityRef.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        nftSimpleActivity.REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CameraSurface(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.
}

From source file:org.artoolkit.ar.utils.calib_optical.CameraSurface.java

@SuppressWarnings("deprecation")
public CameraSurface(Context context) {
    super(context);

    Log.i(TAG, "CameraSurface(): ctor called");
    Activity activityRef = (Activity) context;

    try {//from   ww  w. j  a v a2s  .  c om
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activityRef,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activityRef.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activityRef.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CameraSurface(): must ask user for camera access permission");
                activityRef.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        calib_optical_Activity.REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CameraSurface(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.
}

From source file:com.msohm.blackberry.samples.bdvideoplayback.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Initialize BlackBerry Dynamics.
    GDAndroid.getInstance().activityInit(this);
    setContentView(R.layout.activity_main);

    copyButton = (Button) findViewById(R.id.copyButton);

    //The copy button is used to copy an existing video file you have into the secure
    //BlackBerry Dynamics file system.  This only needs to be run once.
    copyButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //Ensure we have permission to read the file being copied.
            int permissionCheck = ContextCompat.checkSelfPermission(v.getContext(),
                    Manifest.permission.READ_EXTERNAL_STORAGE);

            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                //Permission isn't set.  Request it.
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
            } else {
                copyFile();/*w  ww .ja  v a2  s  .  c o m*/
            }
        }
    });

    playButton = (Button) findViewById(R.id.playButton);

    //Initializes the MediaPlayer.
    playButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                if (!isPaused) {
                    BDMediaDataSource source = new BDMediaDataSource(FILENAME);
                    mp.setDataSource(source);
                    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mp.start();
                        }
                    });
                    mp.prepareAsync();
                } else {
                    mp.start();
                }
                isPaused = false;
            } catch (IOException ioex) {
            }

        }
    });

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    SurfaceHolder holder = surfaceView.getHolder();
    holder.addCallback(this);
    mp = new MediaPlayer();
    isPaused = false;
}

From source file:com.kupay.decoder.DecoderActivity.java

public void startCamera() {

    //(SurfaceView) getView().findViewById(R.id.preview_view);
    RelativeLayout rl = (RelativeLayout) getView().findViewById(R.id.fitCamera);
    surfaceView = new SurfaceView(getActivity());
    SurfaceHolder surfaceHolder = surfaceView.getHolder();

    Log.v("app", "Cargando camara");
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    if (rl != null && surfaceView != null)
        rl.addView(surfaceView);/*w w  w. j  a  v  a2 s.c  o m*/

}

From source file:org.artoolkit.ar.base.camera.CaptureCameraPreview.java

/**
 * Constructor takes a {@link CameraEventListener} which will be called on
 * to handle camera related events.//w  w w. j ava2 s. c om
 *
 * @param cel CameraEventListener to use. Can be null.
 */
@SuppressWarnings("deprecation")
public CaptureCameraPreview(Activity activity, CameraEventListener cel) {
    super(activity);

    Log.i(TAG, "CaptureCameraPreview(): ctor called");
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activity,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activity.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activity.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CaptureCameraPreview(): must ask user for camera access permission");
                activity.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CaptureCameraPreview(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.

    setCameraEventListener(cel);
}

From source file:com.TwentyCodes.android.IOIOTruck.CameraFragment.java

/**
 * (non-Javadoc)//from   w  w w  . ja v a 2s.c  om
 * @see android.support.v4.app.Fragment#onViewCreated(android.view.View, android.os.Bundle)
 * @author ricky barrette
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.camera_fragment, container, false);

    /*
     * setup augmented preview overlay
     */
    FrameLayout fl = (FrameLayout) view.findViewById(R.id.camera_frame);
    mPreviewOverlay = new PreviewOverlay(this.getActivity());
    fl.addView(mPreviewOverlay);

    /*
     * setup camera preview      
     */
    mPreview = (SurfaceView) view.findViewById(R.id.cameraSurface);
    SurfaceHolder cameraHolder = mPreview.getHolder();
    cameraHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    cameraHolder.addCallback(new SurfaceHolder.Callback() {
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
    });

    return view;
}

From source file:com.abhi.barcode.fragment.BarCodeFragment.java

@Override
public void onResume() {
    super.onResume();
    if (runCamera && hasSurface) {
        startCameraCampure();/*  www. ja  v  a2 s . co  m*/
    } else if (runCamera) {
        SurfaceView surfaceView = (SurfaceView) getView().findViewById(R.id.cameraView);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(BarCodeFragment.this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
}

From source file:com.abhi.barcode.fragment.BarCodeFragment.java

public void startCameraCampure() {
    cameraManager = new CameraManager(getActivity().getApplicationContext());
    viewfinderView.setCameraManager(cameraManager);
    handler = null;/*from  w w  w  .j  av  a 2  s  .c o  m*/
    resetStatusView();
    runCamera = true;
    SurfaceView surfaceView = (SurfaceView) getView().findViewById(R.id.cameraView);
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    hasSurface = true;
    if (hasSurface) {
        initCamera(surfaceHolder, viewfinderView);
    } else {
        surfaceHolder.addCallback(BarCodeFragment.this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    decodeFormats = null;
    characterSet = null;
    cameraActive = true;
}

From source file:com.jwetherell.QRComm575.DecoderActivity.java

@Override
protected void onResume() {
    super.onResume();
    Log.v(TAG, "onResume()");

    // CameraManager must be initialized here, not in onCreate().
    if (cameraManager == null)
        cameraManager = new CameraManager(getApplication());

    if (viewfinderView == null) {

        viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
        viewfinderView.setCameraManager(cameraManager);
    }//from  ww w.ja va  2s.  c o m

    showScanner();

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    if (hasSurface) {
        // The activity was paused but not stopped, so the surface still
        // exists. Therefore
        // surfaceCreated() won't be called, so init the camera here.

        initCamera(surfaceHolder);
    } else {
        // Install the callback and wait for surfaceCreated() to init the
        // camera.
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
}