Start playing media player when recording video in android

I am trying to record a video in android using a media recorder. It works fine from 4.0 and up when I try to record a video in the 2.3.4 media player, giving rise to an unsuccessful exception -12.

Here is my media recorder code.

            mrec.setCamera(camera);
        mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
        mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mrec.setVideoSize(getMaxSupportedVideoSize().width,
            getMaxSupportedVideoSize().height);

        mrec.setOutputFile(path + filename);
        mrec.setMaxDuration(30000);
        mrec.setPreviewDisplay(surfaceHolder.getSurface());

        mrec.prepare();
        mrec.start();

      

The runtime exception I am getting is

01-21 12:58:16.459: E/MediaRecorder(2461): start failed: -12
01-21 12:58:16.469: W/System.err(2461): java.lang.RuntimeException: start failed.
01-21 12:58:16.469: W/System.err(2461):     at android.media.MediaRecorder.native_start(Native Method)
01-21 12:58:16.469: W/System.err(2461):     at android.media.MediaRecorder.start(MediaRecorder.java:603)
01-21 12:58:16.469: W/System.err(2461):     at com.roseenvy.ui.activities.VideoRecordActivity.startRecording(VideoRecordActivity.java:235)
01-21 12:58:16.469: W/System.err(2461):     at com.roseenvy.ui.activities.VideoRecordActivity.onClick(VideoRecordActivity.java:125)
01-21 12:58:16.469: W/System.err(2461):     at android.view.View.performClick(View.java:2485)
01-21 12:58:16.469: W/System.err(2461):     at android.view.View$PerformClick.run(View.java:9080)
01-21 12:58:16.469: W/System.err(2461):     at android.os.Handler.handleCallback(Handler.java:587)
01-21 12:58:16.469: W/System.err(2461):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 12:58:16.469: W/System.err(2461):     at android.os.Looper.loop(Looper.java:130)
01-21 12:58:16.469: W/System.err(2461):     at android.app.ActivityThread.main(ActivityThread.java:3737)
01-21 12:58:16.469: W/System.err(2461):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 12:58:16.469: W/System.err(2461):     at java.lang.reflect.Method.invoke(Method.java:507)
01-21 12:58:16.469: W/System.err(2461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
01-21 12:58:16.469: W/System.err(2461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
01-21 12:58:16.469: W/System.err(2461):     at dalvik.system.NativeStart.main(Native Method)

      

Please help me how can I solve this.

Thank you in advance

+3


source to share


2 answers


  The below code worked for me.      
public Size getMaxSupportedVideoSize() {
    int maximum = 0;
    int position = -1;
    for (int i = 0; i < sizes.size(); i++) {
        if (sizes.get(i).width > maximum) {
            maximum = sizes.get(i).width; // new maximum
            position = i;
        }
    }
    if (position == -1) {
        return null;
    }

    return sizes.get(position);

       }

        mrec = new MediaRecorder();
camera.unlock();
mrec.setCamera(camera);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
Size maxSupportedVideoSize = getMaxSupportedVideoSize();
if (maxSupportedVideoSize == null) {
    mrec.setVideoSize(480, 640);
} else {
    mrec.setVideoSize(maxSupportedVideoSize.width,
            maxSupportedVideoSize.height);
    System.out.println(maxSupportedVideoSize.width + " width");
    System.out.println(maxSupportedVideoSize.height + " height");
}
mrec.setOutputFile(path + filename);
mrec.setMaxDuration(30000);
mrec.setPreviewDisplay(surfaceHolder.getSurface());

      



+2


source


Just comment this line and then try it.

mrec.setVideoSize(getMaxSupportedVideoSize().width,
            getMaxSupportedVideoSize().height);

      

and change this line:



mrec.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

      

Submit your link on how to capture video using MediaRecorder.

Hope it helps :)

0


source







All Articles