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 8946603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:33:47+00:00 2026-06-15T12:33:47+00:00

public class FulfillVideoTaskActivity extends Activity implements SurfaceHolder.Callback, OnInfoListener, OnErrorListener{ private Button initBtn = null;

  • 0
public class FulfillVideoTaskActivity extends Activity implements SurfaceHolder.Callback, OnInfoListener, OnErrorListener{

private Button initBtn = null;
private Button startBtn = null;
private Button stopBtn = null;
private Button playBtn = null;
private Button stopPlayBtn = null;
// save Button should be implemented
private TextView recordingMsg = null;
private VideoView videoView = null;
private SurfaceHolder holder = null;
private Camera camera = null;
private static final String TAG ="RecordVideo";
private MediaRecorder recorder = null;
private String outputFileName;



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

    // get references to UI elements
    initBtn = (Button) findViewById(R.id.initBtn);
    startBtn = (Button) findViewById(R.id.startBtn);
    stopBtn = (Button) findViewById(R.id.stopBtn);
    playBtn = (Button) findViewById(R.id.playBtn);
    stopPlayBtn = (Button) findViewById(R.id.stopPlayBtn);
    recordingMsg = (TextView) findViewById(R.id.recording);
    videoView = (VideoView)this.findViewById(R.id.videoView);


}

public void buttonTapped(View view) {
    switch(view.getId()) {
    case R.id.initBtn:
        initRecorder();
        break;
    case R.id.startBtn:
        beginRecording();
        break;
    case R.id.stopBtn:
        stopRecording();
        break;
    case R.id.playBtn:
        playRecording();
        break;
    case R.id.stopPlayBtn:
        stopPlayback();
        break;
    }
}
private void stopPlayback() {
    videoView.stopPlayback();

}

private void playRecording() {
    MediaController mc = new MediaController(this);
    videoView.setMediaController(mc);
    videoView.setVideoPath(outputFileName);
    videoView.start();
    stopPlayBtn.setEnabled(true);

}

private void stopRecording() {
    if(recorder != null) {
        recorder.setOnErrorListener(null);
        recorder.setOnInfoListener(null);
        try {
            recorder.stop();

        }
        catch(IllegalStateException e) {
            //this can happen if the recorder has already stopped.
            Log.e(TAG, "Got IllegalStateException in stopRecording");

        }
        releaseRecorder();
        recordingMsg.setText("");
        releaseCamera();
        startBtn.setEnabled(false);
        stopBtn.setEnabled(false);
        playBtn.setEnabled(true);

    }


}


private void releaseCamera() {
    if(camera != null) {
        try {
            camera.reconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }
        camera.release();
        camera = null;

    }

}

private void releaseRecorder() {
    if(recorder != null) {
        recorder.release();
        recorder = null;
    }

}

private void beginRecording() {
    recorder.setOnInfoListener(this);
    recorder.setOnErrorListener(this);
    recorder.start();
    recordingMsg.setText("RECORDING");
    startBtn.setEnabled(false);
    stopBtn.setEnabled(true);

}

// Initialize the recorder
private void initRecorder() {
    if(recorder != null) return;

    // The place where the video will be saved.
    outputFileName = Environment.getExternalStorageDirectory() + "/videooutput.mp4";

    File outFile = new File(outputFileName);
    //if File already exists, we delete it 
    if(outFile.exists())
        outFile.delete();

    try{
        camera.stopPreview();
        camera.unlock();
        recorder = new MediaRecorder();
        recorder.setCamera(camera);

        recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setVideoSize(280, 200);
        recorder.setVideoFrameRate(15);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setMaxDuration(10000); // limit to 10 seconds
        recorder.setPreviewDisplay(holder.getSurface());
        recorder.setOutputFile(outputFileName); // setting our output file to our FileName

        recorder.prepare();
        Log.v(TAG, "MediaRecorder initialized");
        initBtn.setEnabled(false);
        startBtn.setEnabled(true);

    }
    //error checking 
    catch(Exception e) {
        Log.v(TAG, "MediaRecorder failed to initialize");
        e.printStackTrace();

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_fulfill_video_task, menu);
    return true;
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub

}

public void surfaceCreated(SurfaceHolder holder) {
    Log.v(TAG, "in sufaceCreated");

    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();

    }catch (IOException e) {
        Log.v(TAG, "Could not start the preview");
        e.printStackTrace();

    }
    initBtn.setEnabled(true);

}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

public void onError(MediaRecorder mr, int what, int extra) {
    Log.e(TAG, "got a recording error");
    stopRecording();
    Toast.makeText(this, "Recording error has occurred. Stopping the recording", Toast.LENGTH_SHORT).show();

}

public void onInfo(MediaRecorder mr, int what, int extra) {
    Log.i(TAG, "got a recording event");
    if(what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
        Log.i(TAG, "...max duration reached");
        stopRecording();
        Toast.makeText(this, "Recording limit has been reached. Stopping the recording", Toast.LENGTH_SHORT).show();

    }


}

// disable the buttons until the camera is initialized
protected void onResume() {
    Log.v(TAG, "in onResume");
    super.onResume();
    initBtn.setEnabled(false);
    startBtn.setEnabled(false);
    stopBtn.setEnabled(false);
    playBtn.setEnabled(false);
    stopPlayBtn.setEnabled(false);
    if(!initCamera())
        finish();

}

// initializes the camera 

private boolean initCamera() {
    try {
        camera = Camera.open();
        Camera.Parameters camParams = camera.getParameters();
        camera.lock();
        holder = videoView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }
    catch(RuntimeException re) {
        Log.v(TAG, "Could not initialize the Camera");
        re.printStackTrace();
        return false;
    }
    return true;
}

}

Hi, I’m trying to record a video on android right now, when I run my code (the whole code above), the camera can’t be initialized. I guess I have an error in the following part.

private boolean initCamera() {
    try {
        camera = Camera.open();
        Camera.Parameters camParams = camera.getParameters();
        camera.lock();
        holder = videoView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }
    catch(RuntimeException re) {
        Log.v(TAG, "Could not initialize the Camera");
        re.printStackTrace();
        return false;
    }
    return true;
}

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); this line in this code gets multiple markers, saying

  • The method setType(int) from the type SurfaceHolder is deprecated
  • The field SURFACE_TYPE_PUSH_BUFFERS is deprecated

Does anyone know the reason why I’m getting 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-15T12:33:50+00:00Added an answer on June 15, 2026 at 12:33 pm

    Try this code

    first

       setRecorder() ;
       SurfaceView  videoShootSurfaceView = (SurfaceView) findViewById(R.id.shootVideosurfaceView_VSD);
    
    
        SurfaceHolder videoSurfaceHolder = videoShootSurfaceView.getHolder();
        videoSurfaceHolder.addCallback(this);
        videoSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    

    set the recorder type

        public void setRecorder() {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        // recorder.setVideoSize(640, 480);
        recorder.setVideoSize(320, 240);
        // recorder.setVideoSize(480, 320);
        // recorder.setVideoSize(176, 144);
        recorder.setVideoFrameRate(15);
        // recorder.setMaxDuration(3600000);
        recorder.setMaxDuration(300000);
        recorder.setOutputFile("/sdcard/videocapture_example.mp4");
    }
    

    override this methhod.

        @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
        recorder.setPreviewDisplay(holder.getSurface());
        if (recorder != null) {
            try {
                recorder.prepare();
            } catch (IllegalStateException e) {
                Log.e("IllegalStateException", e.toString());
            } catch (IOException e) {
                Log.e("IOException", e.toString());
            }
        }
    }
    

    To start the recording

        public void startRecording() {
        setRecorder();
        recorder.setPreviewDisplay(recoderTempHolder.getSurface());
        if (recorder != null) {
            try {
                recorder.prepare();
                recorder.start();
            } catch (IllegalStateException e) {
                Log.e("IllegalStateException", e.toString());
            } catch (IOException e) {
                Log.e("IOException", e.toString());
            }
        }
    
    }
    

    To stop the recording

      public void stopRecording() {
    
        recorder.stop();
        // recorder.reset();
        recorder.release();
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class TwitterActivity extends Activity { private Twitter twitter; private OAuthProvider provider; private CommonsHttpOAuthConsumer
public class XPBN extends Activity{ private Map _map; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
public class SuperUser extends User implements Serializable{ private static final long serialVersionUID = 1L;
public class MainActivity extends Activity { Button b; //FrameLayout fl; @Override protected void onCreate(Bundle
public class AycanClass extends AsyncTask<String, Void, String> implements IAppointments { Activity activity; public AycanClass(Activity
public class tryAnimActivity extends Activity { /** * The thread to process splash screen
public class ImportContactsActivity extends Activity { /** Called when the activity is first created.
public class Challenge implements Comparable<Challenge>, Serializable { private static final long serialVersionUID = 6970603871560357536L;
public class PackageTabActivity extends ListActivity{ HashMap<String,Object> hm ; ArrayList<HashMap<String,Object>> applistwithicon ; private static final
public class MyActivity extends Activity { /** Called when the activity is first created.

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.