I need to record video file to mp4 formate. But, when I run and click on the record button, it throws error like FATAL EXCEPTION: main java.lang.IllegalStateException . Here is my whole code
boolean recording = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_example);
initUI();
initRecorder();
buttonStopVid.setEnabled(false);
buttonPlayVid.setEnabled(false);
buttonStartVid.setOnClickListener(this);
holder = surfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
protected void initUI(){
buttonStartVid = (Button) findViewById(R.id.buttonStartRecordVid);
buttonPlayVid = (Button) findViewById(R.id.buttonPlayVid);
buttonStopVid = (Button) findViewById(R.id.buttonStopRecordVid);
videoView = (VideoView) findViewById(R.id.videoView);
surfaceView = (SurfaceView) findViewById(R.id.surface_camera_final);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.buttonStartRecordVid){
buttonStartVid.setEnabled(false);
buttonStopVid.setEnabled(true);
surfaceView.setVisibility(View.VISIBLE);
prepareRecorder();
recording = true;
recorder.start();
}
if(v.getId() == R.id.buttonStopRecordVid){
buttonPlayVid.setEnabled(true);
buttonStartVid.setEnabled(true);
buttonStopVid.setEnabled(false);
surfaceView.setVisibility(View.GONE);
recorder.stop();
recording = false;
initRecorder();
prepareRecorder();
}
if(v.getId() == R.id.buttonPlayVid){
}
}
protected void initRecorder(){
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cp = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cp);
try{
File newFile = File.createTempFile("vid", ".mp4", Environment.getExternalStorageDirectory());
recorder.setOutputFile(newFile.getAbsolutePath());
}
catch (IOException e){
e.printStackTrace();
}
}
private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(recording){
recorder.stop();
recording = false;
}
recorder.release();
}
Any ideas of what could be causing this and how to fix it? Thanks!
Use this link http://developer.android.com/reference/android/media/MediaRecorder.html
it will provide full information that you need.