error in video recorder android
<p>I am developing video recorder</p>

<p>My code is </p>

<pre><code>package com.app.UI;

import java.io.File;
import java.io.IOException;

import com.app.utils.Logger;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class main extends Activity implements SurfaceHolder.Callback{
    /** Called when the activity is first created. */
    private static final String TAG = "CAMERA_TUTORIAL";

    Button btnStart, btnStop, btnPlay;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private Camera camera;
    private boolean previewRunning;

    private MediaRecorder mediaRecorder;
    private final int maxDurationInMs = 20000;
    private final long maxFileSizeInBytes = 500000;
    private final int videoFramesPerSecond = 20;

    public static String pathname = "/sdcard/recorder.amr";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        btnStart = (Button)findViewById(R.id.btnStart);
        btnStop = (Button)findViewById(R.id.btnStop);
        btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startRecording();
            }
        });

        btnStop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                stopRecording();
            }
        });
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        Logger.debug("surface change");
        if (previewRunning){
            camera.stopPreview();
        }
        Camera.Parameters p = camera.getParameters();
        camera.setParameters(p);

        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
            previewRunning = true;
        }
        catch (IOException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
        }

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Logger.debug("surface created");
        camera = Camera.open();
        if (camera != null){
            Camera.Parameters params = camera.getParameters();
            camera.setParameters(params);
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Logger.debug("surface destroy");
        camera.stopPreview();
        previewRunning = false;
        camera.release();
    }

    public boolean startRecording(){
        try {
            //camera.unlock();

            mediaRecorder = new MediaRecorder();

            mediaRecorder.setCamera(camera);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

            mediaRecorder.setMaxDuration(maxDurationInMs);

            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File (sdCard.getAbsolutePath() + "/My Recording");
            dir.mkdirs();
            File tempFile = new File(dir, "myvideo.mp4");

            mediaRecorder.setOutputFile(tempFile.getAbsolutePath());

            //mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
            //mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());

            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

            mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

            //mediaRecorder.setMaxFileSize(maxFileSizeInBytes);

            mediaRecorder.prepare();
            mediaRecorder.start();

            return true;
        } catch (IllegalStateException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    public void stopRecording(){
        try{
            mediaRecorder.stop();
            mediaRecorder.release();
        }
        catch (IllegalStateException e) {
            // TODO: handle exception
            Logger.debug(""+e.getMessage());
        }
        //camera.lock();
    }

}
</code></pre>

<p>and in manifest file I have write </p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.app.UI"
      android:versionCode="1"
      android:versionName="1.0"&gt;
    &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt;
        &lt;activity android:name=".main"
                  android:label="@string/app_name"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

    &lt;/application&gt;
    &lt;uses-sdk android:minSdkVersion="3" /&gt;
    &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
    &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
    &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
    &lt;uses-feature android:name="android.hardware.camera"/&gt;
    &lt;uses-feature android:name="android.hardware.camera.autofocus"/&gt;
&lt;/manifest&gt; 
</code></pre>

<p>now my problem is when I click on start button it show me error like</p>

<pre><code>04-25 17:47:03.817: WARN/System.err(23842): java.io.IOException: prepare failed.
04-25 17:47:03.817: WARN/System.err(23842):     at android.media.MediaRecorder._prepare(Native Method)
04-25 17:47:03.817: WARN/System.err(23842):     at android.media.MediaRecorder.prepare(MediaRecorder.java:458)
04-25 17:47:03.817: WARN/System.err(23842):     at com.app.UI.main.startRecording(main.java:143)
04-25 17:47:03.827: WARN/System.err(23842):     at com.app.UI.main$1.onClick(main.java:55)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.View.performClick(View.java:2344)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.View.onTouchEvent(View.java:4133)
04-25 17:47:03.827: WARN/System.err(23842):     at android.widget.TextView.onTouchEvent(TextView.java:6504)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.View.dispatchTouchEvent(View.java:3672)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.827: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.837: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.837: WARN/System.err(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:47:03.837: WARN/System.err(23842):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
04-25 17:47:03.837: WARN/System.err(23842):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
04-25 17:47:03.837: WARN/System.err(23842):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
04-25 17:47:03.837: WARN/System.err(23842):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
04-25 17:47:03.847: WARN/System.err(23842):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
04-25 17:47:03.847: WARN/System.err(23842):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 17:47:03.847: WARN/System.err(23842):     at android.os.Looper.loop(Looper.java:123)
04-25 17:47:03.847: WARN/System.err(23842):     at android.app.ActivityThread.main(ActivityThread.java:4203)
04-25 17:47:03.847: WARN/System.err(23842):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 17:47:03.847: WARN/System.err(23842):     at java.lang.reflect.Method.invoke(Method.java:521)
04-25 17:47:03.847: WARN/System.err(23842):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-25 17:47:03.847: WARN/System.err(23842):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
04-25 17:47:03.857: WARN/System.err(23842):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>and when I click on stop button it shows error like</p>

<pre><code>04-25 17:48:03.827: ERROR/AndroidRuntime(23842): Uncaught handler: thread main exiting due to uncaught exception
04-25 17:48:03.837: ERROR/AndroidRuntime(23842): java.lang.IllegalStateException
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.media.MediaRecorder.stop(Native Method)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.app.UI.main.stopRecording(main.java:159)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.app.UI.main$2.onClick(main.java:64)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.View.performClick(View.java:2344)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.View.onTouchEvent(View.java:4133)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.widget.TextView.onTouchEvent(TextView.java:6504)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.View.dispatchTouchEvent(View.java:3672)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.os.Looper.loop(Looper.java:123)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at android.app.ActivityThread.main(ActivityThread.java:4203)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at java.lang.reflect.Method.invoke(Method.java:521)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
04-25 17:48:03.837: ERROR/AndroidRuntime(23842):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>What I have to do, i reffered many site but my problem can not solve,
please friend help me</p>

<p>I have test on 1.6b device and 2.2 emulater</p>
