Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:40:55+00:00 2026-06-11T10:40:55+00:00

I am developing an application where I need to have constant Camera running inside

  • 0

I am developing an application where I need to have constant Camera running inside my app
So I have two things to achieve here,
1. Capture Image
2. Record Video

I am having difficulties to record video and stop and reset camera

Following is snippet of my code:-

import java.io.File;
import java.io.IOException;
import java.util.Date;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.Menu;
import android.view.SurfaceView;
import android.widget.Toast;

public class Myvideo1 extends Activity  implements SurfaceHolder.Callback {

    @Override
    protected void onDestroy() {
     //   stopRecording();
        super.onDestroy();
    }

    private SurfaceHolder surfaceHolder;
    private SurfaceView surfaceView;
    public MediaRecorder mrec = new MediaRecorder();    
    private Camera mCamera;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        surfaceView = (SurfaceView) findViewById(R.id.videoview);
   //     if(mCamera==null)
    //        mCamera = Camera.open();

        // Please maintain sequence of following code. 

        // If you change sequence it will not work.

        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        try {
            startRecording();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {

        menu.add(0, 0, 0, "Start");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
   public boolean onOptionsItemSelected(MenuItem item)

    {
        if(item.getTitle().equals("Start"))
        {
            try {

                startRecording();
            //    item.setTitle("Stop");

            } catch (Exception e) {

                String message = e.getMessage();
                Log.i(null, "Problem " + message);
                mrec.release();
            }
        }

        else
        //  if(item.getTitle().equals("Stop"))
        {
          //  mrec.stop();
         //   mrec.release();
         //   mrec = null;
          //  item.setTitle("Start");
        }

        return super.onOptionsItemSelected(item);
    }

    protected void startRecording() throws IOException
    {
        if(mCamera==null)
            mCamera = Camera.open();

         String filename;
         String path;

         path= Environment.getExternalStorageDirectory().getAbsolutePath().toString();

         Date date=new Date();
         filename="/rec"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";

         //create empty file it must use
         File file=new File(path,filename);

        mrec = new MediaRecorder(); 

        mCamera.lock();
        mCamera.unlock();

        // Please maintain sequence of following code. 

        // If you change sequence it will not work.
        mrec.setCamera(mCamera);    
        mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mrec.setAudioSource(MediaRecorder.AudioSource.MIC);     
        mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mrec.setPreviewDisplay(surfaceHolder.getSurface());
        mrec.setOutputFile(path+filename);
        mrec.setMaxDuration(10000);     //10 sec



  //  }


    mrec.setOnInfoListener(new OnInfoListener() {   
        @Override  
        public void onInfo(MediaRecorder mr, int what, int extra) {  
            if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {   
                onStop();    

                Toast.makeText(getApplicationContext(), "video of 10 sec is completed", Toast.LENGTH_SHORT).show();

            }  
        }
    }
    ); 

    }


    public void preparerec()
    {
         try {
            mrec.prepare();
            mCamera.setPreviewDisplay(this.surfaceHolder);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void startrec()
    {
         mrec.start();
    }





    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {      

        Camera.Parameters parameters = mCamera.getParameters();      
        parameters.setPreviewSize(width, height);     
        mCamera.setParameters(parameters);       
        mCamera.startPreview(); 


    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {       

       /* if (mCamera != null) {
            Parameters params = mCamera.getParameters();
            mCamera.setParameters(params);

            Log.i("Surface", "Created");
        }
        else {
            Toast.makeText(getApplicationContext(), "Camera not available!",
                    Toast.LENGTH_LONG).show();

            finish();
        }*/


         try                 {      
             //Open the Camera in preview mode            
            mCamera = Camera.open(); 
            preparerec();
            //        
             }              
         catch(Exception ioe)          
             {          
             ioe.printStackTrace(System.out);          
             } 

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera.release();       

    }


    public void onStop(){
        mrec.stop();
        mrec.release();
        mrec = null;
     //   item.setTitle("Start");
    }


}

When I StartRecording the Screen gets flickering and green on my samsung Galaxy s2,s any solution on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:40:56+00:00Added an answer on June 11, 2026 at 10:40 am

    This Solution works perfect for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an application in asp.net mvc where i need to have Chat
I have been developing the application for drawing and I need to detect double
I have been developing the application, and I need that drawingg is executed in
I am developing a geneology application, and I have a need to store dates
While developing my WordPress application i have a requirement where i need to show
I have been developing an asp.net mvc application where i need to make large
I am developing a WPF application, and I need your advice. I have to
I'm developing an OpenGL application. I need to have a model of planet earth
I'm developing an application with javascript. What I need is to have divs with
I am developing web application where users have collection of tags. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.