Question

I am trying since one week to record a video, and after trying examples and examples, it is impossible and I always arrive to similar problems. My code is the following one:

public class VideoRecorder extends Activity{

private final static String TAG = "Playing";
private MediaRecorder recorder;
private Preview mPreview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    recorder = new MediaRecorder();

    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

    mPreview = new Preview(this,recorder);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(mPreview);

}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    menu.add(0, 0, 0, "StartRecording");   
    menu.add(0, 1, 0, "StopRecording");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) 
    {
        case 0:
            try {
                recorder.start();

            } catch (Exception e) {
                Log.w(TAG, e);
                recorder.release();
            }
            break;

        case 1:   //GoToAllNotes
            recorder.stop();
            recorder.release();
            recorder = null;
            finish();
            break;

        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}
}

class Preview extends SurfaceView implements SurfaceHolder.Callback{
    private SurfaceHolder mHolder;
    private MediaRecorder tempRecorder;

Preview(Context context, MediaRecorder recorder){
    super(context);
    tempRecorder = recorder;
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public Surface getSurface(){
    return mHolder.getSurface();
}

public void surfaceCreated(SurfaceHolder holder){
    tempRecorder.setOutputFile("/sdcard/myVideo.3gp");
    tempRecorder.setPreviewDisplay(mHolder.getSurface());
    try{
        tempRecorder.prepare();
    } catch (Exception e) {
        tempRecorder.release();
        tempRecorder = null;
    }
}

public void surfaceDestroyed(SurfaceHolder holder){
    if(tempRecorder!=null){
        tempRecorder.release();
        tempRecorder = null;
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){

}
}

This one is supose to be an easy application which is recording video, but after trying and trying, It is not working, the problem is:

Media Recorder: prepare failed -17

If anyone knows how to make it work properly....I would like a help cause I am desperate with that.

Btw, for those who are gonna tell me about adding the permissions, yes, i added them:

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

<uses-feature android:name="android.hardware.camera" android:required="true" />

I tried with other examples, and always i found one problem, if it is not with prepare -17, it is with start -0............

Thanks a lot!

Was it helpful?

Solution

You must call MediaRecorder and Camera methods in the same order as described here: http://developer.android.com/guide/topics/media/camera.html#capture-video

OTHER TIPS

You can ask the CamcorderProfile api if your device supports the requested MPEG_4_SP video format.

According to Android Supported Media Formats the MPEG_4_SP encoding is not supported til android 4.

May be using h.263 works for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top