7

谁能指出我正在做的错误?我正在尝试录制一个短视频,但不知何故它找到了崩溃的方法。

源代码:

public class Start_recording extends Activity implements SurfaceHolder.Callback
{
    MediaRecorder recorder;
    SurfaceHolder surfaceHolder;
    SurfaceView myVideoView;
    Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        camera = Camera.open();
        camera.unlock();
        recorder = new MediaRecorder();
        setContentView(R.layout.activity_main_rst);
        myVideoView = (SurfaceView)findViewById(R.id.surface_camera);
        surfaceHolder = myVideoView.getHolder();
        surfaceHolder.addCallback(this);
        initMediaRecorder();
        boolean exists = (new File(android.os.Environment.getExternalStorageDirectory() + "/Record/")).exists();

        if (!exists)
        {
            new File(android.os.Environment.getExternalStorageDirectory() + "/Record/").mkdirs();
        }

        try
        {
            recorder.prepare();  // This is the line of error.
            recorder.start();
            Thread.sleep(36000);
            recorder.stop();
            setupActionBar();
        }
        catch(Exception e)
        {
            Log.v("Error is there : ",e.toString());
            e.printStackTrace();
        }
    }

    public void initMediaRecorder()
    {
        try
        {


            recorder.setPreviewDisplay(surfaceHolder.getSurface());
            recorder.setCamera(camera);
            recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(android.os.Environment.getExternalStorageDirectory()+"/Record/test.3gp");
            recorder.setMaxDuration(300000);
        }
        catch(Exception f)
        {
            Log.v("Exception here : ",f.toString());
            f.printStackTrace();
        }
    }
}

错误 :

E/MediaRecorderJNI(1575): Application lost the surface
V/Error is there :(1575): java.io.IOException: invalid preview surface
W/System.err(1575): java.io.IOException: invalid preview surface
W/System.err(1575):     at android.media.MediaRecorder._prepare(Native Method)
W/System.err(1575):     at android.media.MediaRecorder.prepare(MediaRecorder.java:666)
W/System.err(1575):     at com.example.project.Start_recording.onCreate(Start_recording.java:51)

我已经在 Manifest 文件中正确添加了权限,并且还设置了 setPreviewDisplay()。但我得到了无效的预览表面。请纠正我。

4

1 回答 1

0

必须SurfaceHolder.Callback在真正创建表面后才能开始录制。

于 2015-03-10T12:03:30.893 回答