Android的 - MediaRecorder - Apllication失去了表面 [英] Android - MediaRecorder - Apllication lost the surface

查看:565
本文介绍了Android的 - MediaRecorder - Apllication失去了表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试创建一个具有SurfaceView:

I first try to create the SurfaceView with:

SurfaceView sv = new SurfaceView(context);
// Get a surface
surfaceHolder = sv.getHolder();
// tells Android that this surface will have its data constantly
// replaced
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);        
wm.addView(sv, params);

然后尝试prepare记录:

Then try to prepare recorder:

try {
    camera.unlock();

    mediaRecorder = new MediaRecorder();

    mediaRecorder.setCamera(camera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setProfile(CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH));

    mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO,
                appName).toString());

    mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

    mediaRecorder.prepare();
} catch (IllegalStateException e) {
    releaseMediaRecorder();
    Log.d(TAG,
        "IllegalStateException preparing MediaRecorder: "
                        + e.getMessage());
    return false;
} catch (IOException e) {
    releaseMediaRecorder();
    Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());

    return false;
}

return true;

不过,我得到一个错误应用程序失去了面尽管我可以看到,preVIEW工作。这是为什么?

But I get an error "Application lost the surface" despite that I can see that preview is working. Why is that?

推荐答案

可能的原因是,表面实例将MediaRecorder情况确实后才能完全建立。

The possible reason is that Surface instance will be completely created only after MediaRecorder instance does.

的解决方案是放置SurfaceView初始化(code的问题的第一个片段)以调用onStart()中的活性。这将保证创作与表面所有相应的回调MediaRecorder初始化之前会完成。

The solution is to place the SurfaceView initialization (the first fragment of code in question) to onStart() in activity. It will guarantee that creation of Surface with all corresponding callbacks will be finished before MediaRecorder initialisation.

这篇关于Android的 - MediaRecorder - Apllication失去了表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆