i want to record a video (with audio) from this app

but as soon as i press start recording it gives "E/MediaRecorder: start 
failed: -19"

i referred to this link ---> 
http://stackoverflow.com/questions/10496969/android-mediarecorder-start-failed-19
 
 and according to them i set the supported videoSize but again i failed 


and i have set the method calls according to the state diagram given on --->
"**developer.android.com/reference/android/media/MediaRecorder.html**" 
website

and i have set audio and video parameters as given on ---> 
"**developer.android.com/guide/appendix/media-formats.html**" website

but nothing seems to work :( 


i am testing this on my **phone Mi Redmi 2 running MIUI**

this is how UI looks like  ---> [Cam UI][1] 

  [1]: http://i.stack.imgur.com/z7hW2.jpg


this is my code below:-


   

    public class AndroidCamera extends Activity implements 
SurfaceHolder.Callback {
    TextView testView;

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    MediaRecorder mRecorder;
    private boolean mCaptureFrame = false;
    private boolean recording = false;
    File audiofile = null;
    private final String tag = "VideoServer";

    Button start, stop, startrec, stoprec;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        start = (Button) findViewById(R.id.startcamerapreview);
        start.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                start_camera();
            }
        });

        stop = (Button) findViewById(R.id.stopcamerapreview);
        stop.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                stop_camera();
            }
        });

        startrec = (Button) findViewById(R.id.startrecording);
        startrec.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                startRecording();
            }
        });

        stoprec = (Button) findViewById(R.id.stoprecording);
        stoprec.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                stopRecording();
            }
        });

        surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


    }

    private void start_camera() {
        try {
            camera = Camera.open();
            setCameraDisplayOrientation(this, 
Camera.CameraInfo.CAMERA_FACING_BACK, camera);
        } catch (RuntimeException e) {
            Log.e(tag, "init_camera: " + e);
            return;
        }
        try {
            camera.setPreviewDisplay(surfaceHolder);
            Camera.Parameters parameters = camera.getParameters();
            parameters.setPreviewSize(1280, 720);
            surfaceView.requestLayout();
            if (parameters.getSupportedFocusModes().contains(
                    Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
                
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
            }
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
            camera.setParameters(parameters);
            camera.startPreview();
            camera.autoFocus(null);
        } catch (Exception e) {
            Log.e(tag, "init_camera: " + e);
            return;
        }
    }

    private void stop_camera() {
        camera.stopPreview();
        camera.release();
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int 
arg3) {
        // TODO Auto-generated method stub
    }

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

    }

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

    public static void setCameraDisplayOrientation(Activity activity, int 
cameraId, android.hardware.Camera camera) {
        android.hardware.Camera.CameraInfo info = new 
android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        int rotation = 
activity.getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;  // compensate the mirror
        } else {  // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }
        camera.setDisplayOrientation(result);
    }

    public void startRecording() {
        Log.e("", "Begin StartRecording");
        Toast.makeText(this, "Recording....", Toast.LENGTH_SHORT).show();
        mCaptureFrame = true;
        if (mRecorder != null) {
            mRecorder.stop();
            mRecorder.release();
        }
        mRecorder = new MediaRecorder();
        mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setAudioChannels(1);
        mRecorder.setAudioSamplingRate(24);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mRecorder.setVideoEncodingBitRate(56);
        mRecorder.setAudioEncodingBitRate(24);
        final List<Camera.Size> mSupportedVideoSizes = 
getSupportedVideoSizes(camera);
        for (Camera.Size str : mSupportedVideoSizes)
            Log.e(tag, "mSupportedVideoSizes "+str.width + ":" + str.height 
+ " ... "
                    + ((float) str.width / str.height));

        File dir = Environment.getExternalStorageDirectory();
        try {
            audiofile = File.createTempFile("sound", ".mp4", dir);
        } catch (IOException e) {
            Log.e(tag, "external storage access error");
            return;
        }
        mRecorder.setOutputFile(audiofile.getAbsolutePath());
        mRecorder.setVideoSize(176, 144);
        mRecorder.setVideoFrameRate(12);
        mRecorder.setMaxDuration(20000);
        mRecorder.setPreviewDisplay(surfaceHolder.getSurface());

        try {
            mRecorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mRecorder.start();

        } catch (Throwable t) {
            t.printStackTrace();
            Log.w(tag, t);

            recording = true;
        }

    }

    public void stopRecording() {
        Log.e("", "Begin StopChange");
        Toast.makeText(this, "Recording STOPPED .. ", 
Toast.LENGTH_SHORT).show();


        if (mRecorder != null)
            try {
                mRecorder.stop();
                Toast.makeText(this, "Recording STOPPED .. ", 
Toast.LENGTH_SHORT).show();
            } catch (RuntimeException e) {
                e.printStackTrace();
            } finally {
                mRecorder.release();

                mRecorder = null;
                recording = false;
            }
    }
    public List<Camera.Size> getSupportedVideoSizes(Camera camera) {
        if (camera.getParameters().getSupportedVideoSizes() != null) {
            return camera.getParameters().getSupportedVideoSizes();
        } else {
            // Video sizes may be null, which indicates that all the 
supported
            // preview sizes are supported for video recording.
            return camera.getParameters().getSupportedPreviewSizes();
        }
    }


}
      

here is the Android Manifest fie:-

   

    <?xml version="1.0" encoding="utf-8"?>
       <manifest xmlns:android="http://schemas.android.com/apk/res/android";
        package="pkg.android.chintan.khetiya.cp"
        android:versionCode="1"
        android:versionName="1.0" >

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.STORAGE" />



    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="pkg.android.chintan.khetiya.cp.AndroidCamera"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-sdk android:minSdkVersion="4" />

</manifest>



Logcat :--->

    E/MediaRecorder: start failed: -19
    02-24 13:40:32.070 14892-14892/pkg.android.chintan.khetiya.cp 
W/System.err: java.lang.RuntimeException: start failed.
    02-24 13:40:32.070 14892-14892/pkg.android.chintan.khetiya.cp 
W/System.err:     at android.media.MediaRecorder.start(Native Method)
    02-24 13:40:32.070 14892-14892/pkg.android.chintan.khetiya.cp 
W/System.err:     at 
pkg.android.chintan.khetiya.cp.AndroidCamera.startRecording(AndroidCamera.java:240)
    02-24 13:40:32.070 14892-14892/pkg.android.chintan.khetiya.cp 
W/System.err:     at 
pkg.android.chintan.khetiya.cp.AndroidCamera$3.onClick(AndroidCamera.java:65)
    02-24 13:40:32.070 14892-14892/pkg.android.chintan.khetiya.cp 
W/System.err:     at android.view.View.performClick(View.java:4802)
    02-24 13:40:32.070 14892-14892/pkg.android.chintan.khetiya.cp 
W/System.err:     at android.view.View$PerformClick.run(View.java:20102)
    02-24 13:40:32.070 14892-14892/pkg.android.chintan.khetiya.cp 
W/System.err:     at android.os.Handler.handleCallback(Handler.java:810)
    02-24 13:40:32.070 14892-14892/pkg.android.chintan.khetiya.cp 
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
    02-24 13:40:32.070 14892-14892/pkg.android.chintan.khetiya.cp 
W/System.err:     at android.os.Looper.loop(Looper.java:189)


THESE are the supported video sizes from logcat :--->

    E/VideoServer: mSupportedVideoSizes 176:144 ... 1.2222222
    E/VideoServer: mSupportedVideoSizes 320:240 ... 1.3333334
    E/VideoServer: mSupportedVideoSizes 352:288 ... 1.2222222
    E/VideoServer: mSupportedVideoSizes 480:320 ... 1.5
    E/VideoServer: mSupportedVideoSizes 480:368 ... 1.3043479
    E/VideoServer: mSupportedVideoSizes 640:480 ... 1.3333334
    E/VideoServer: mSupportedVideoSizes 720:480 ... 1.5
    E/VideoServer: mSupportedVideoSizes 800:480 ... 1.6666666
    E/VideoServer: mSupportedVideoSizes 800:600 ... 1.3333334
    E/VideoServer: mSupportedVideoSizes 864:480 ... 1.8
    E/VideoServer: mSupportedVideoSizes 960:540 ... 1.7777778
    E/VideoServer: mSupportedVideoSizes 1280:720 ... 1.7777778
    E/VideoServer: mSupportedVideoSizes 1280:768 ... 1.6666666
    E/VideoServer: mSupportedVideoSizes 1600:1200 ... 1.3333334
    E/VideoServer: mSupportedVideoSizes 1920:1088 ... 1.7647059


> first i press the  **1) start preview button** 
>      then **2) start recording button**
>      then after making vid **3) stop recording**
>       finally **4) stop preview**


please please tell me where am i wrong ... 
any help will be appreciated thanks in advance !

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/08a91231-1f3f-49be-b1f5-1e4ee1308d80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to