Exit onPreviewFrame and notify user
<p>I am working the <code>onPreviewFrame(byte[] data, Camera camera)</code> on Android. Within the onPreviewFrame I do some image processing. At a point inside the onPreviewFrame I want to stop the preview (I know the point by an if statement) and play a sound - perhaps the phone ringtome. I think you can not play a sound in the preview. </p>

<p>How do I exit the onPreviewFrame and where do I add the code for playing the sound? </p>

<p>Is it on Surface Destroyed?</p>

<p>Here is my code:</p>

<pre><code>public class MyCameraPreview extends Activity {    
private Preview mPreview;
     public TextView results;



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

        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Create our Preview view and set it as the content of our activity.
        mPreview = new Preview(this);
        setContentView(mPreview);

   }
}


class Preview extends SurfaceView implements SurfaceHolder.Callback, PreviewCallback {   

    SurfaceHolder mHolder;   
    Camera mCamera;   
    public TextView results;
    public TextView txt;
    private Parameters parameters;   
    //this variable stores the camera preview size    
    private Size previewSize;   
    //this array stores the pixels as hexadecimal pairs    
    private int[] pixels;   
    public int[] argb8888;

    Preview(Context context) {   
        super(context);   
        // Install a SurfaceHolder.Callback so we get notified when the   
        // underlying surface is created and destroyed.   
        mHolder = getHolder();   
        mHolder.addCallback(this);   
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);   
   }   

    public void surfaceCreated(SurfaceHolder holder) {   
        // The Surface has been created, acquire the camera and tell it where   
        // to draw.   
        mCamera = Camera.open();   
        try {   
           mCamera.setPreviewDisplay(holder);   

           //sets the camera callback to be the one defined in this class   
           mCamera.setPreviewCallback(this);

           parameters = mCamera.getParameters();   
           parameters.setZoom(parameters.getMaxZoom());
           mCamera.setParameters(parameters);
           parameters = mCamera.getParameters();
           previewSize = parameters.getPreviewSize();   
           pixels = new int[previewSize.width * previewSize.height]; 


        } catch (IOException exception) {   
            mCamera.release();   
            mCamera = null;   
            // TODO: add more exception handling logic here   
        }   
    }   

    public void surfaceDestroyed(SurfaceHolder holder) {   
        // Surface will be destroyed when we return, so stop the preview.   
        // Because the CameraDevice object is not a shared resource, it's very   
        // important to release it when the activity is paused.   

        mCamera.stopPreview();   
        mCamera.release();   
        mCamera = null;



    }   

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {   
        // Now that the size is known, set up the camera parameters and begin   
        // the preview.   
        parameters.setPreviewSize(w, h);   
        //set the camera's settings   
        mCamera.setParameters(parameters);   
        mCamera.startPreview();


    }   

    @Override  
    public void onPreviewFrame(byte[] data, Camera camera) {   


Do some image processing.


If condition == true {

Exit the preview and then play the ringtone and exit the application.

}   

    }
</code></pre>
