Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:当录制到动态对象时,应用程序失去了表面_Android_Surfaceview_Android Mediarecorder_Android Windowmanager - Fatal编程技术网

Android:当录制到动态对象时,应用程序失去了表面

Android:当录制到动态对象时,应用程序失去了表面,android,surfaceview,android-mediarecorder,android-windowmanager,Android,Surfaceview,Android Mediarecorder,Android Windowmanager,我有点困难。当从后台服务启动视图时,我试图显示摄像头提要。我得到一个运行时错误 04-19 15:00:31.872 31125-31442/csce483.vbox E/Recorder: starting prepare 04-19 15:00:31.872 31125-31442/csce483.vbox E/MediaRecorderJNI: Application lost the surface 04-19 15:00:31.872 31125-31442/csce483.vbox

我有点困难。当从后台服务启动视图时,我试图显示摄像头提要。我得到一个运行时错误

04-19 15:00:31.872 31125-31442/csce483.vbox E/Recorder: starting prepare
04-19 15:00:31.872 31125-31442/csce483.vbox E/MediaRecorderJNI: Application lost the surface
04-19 15:00:31.872 31125-31442/csce483.vbox E/Recorder: starting record
04-19 15:00:31.872 31125-31442/csce483.vbox E/MediaRecorder: start called in an invalid state: 4
当我使用动态创建的视图时。静态视图与窗口管理器不兼容。
在onCreate()中:

然后在我的接收器中:

public static class MyReceiver extends BroadcastReceiver {

        MainActivity yourMain = null;

        void setMainActivityHandler(MainActivity main){
            yourMain = main;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getAction();
            if(message.equalsIgnoreCase("csce483.vbox.start")){
                if(!yourMain.recording) {
                    Log.e("accel", "received a craSH!");
                    yourMain.mSurfaceView.setVisibility(View.VISIBLE);
                    yourMain.wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
                    WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
                    yourMain.wm.addView( yourMain.mSurfaceView, windowManagerParams);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            yourMain.startRecording(); //record w/ new camera API
                        }
                    }).start();
                }
            }
            else if(message.equalsIgnoreCase("csce483.vbox.stop")){
                Log.e("accel", "recieved stop signal!");
                yourMain.stopRecording();

            }
        }
    }
启动记录器:

    public void startRecording() {
    //all file code is TEMPORARY until server is working
    String timestamp = new SimpleDateFormat("MMddyyyy_HHmmss").format(new Date());
    videoFile = new File(Environment.getExternalStorageDirectory(), "/Pictures/"+timestamp+".mp4");
    Log.e("file", "Saving to: " + videoFile.getAbsolutePath());

    if (!videoFile.exists()) {
        File parent = videoFile.getParentFile();
        if (parent != null) {
            try {
                videoFile.createNewFile();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    cameraTriggered = true;

    if(!cameraPresent()) return;

    if (mRecorder == null) {
        mRecorder = new MediaRecorder();
    }

    mCamera.unlock();
    mRecorder.setCamera(mCamera);

    mRecorder.setPreviewDisplay(mHolder.getSurface());
    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

    mRecorder.setPreviewDisplay(mHolder.getSurface());

    if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        mRecorder.setOrientationHint(90);
    }
    else {
        mRecorder.setOrientationHint(0);
    }

    mRecorder.setOutputFile(videoFile.getAbsolutePath());

    try {
        Log.e("Recorder","starting prepare");
        mRecorder.prepare();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    try {
        Log.e("Recorder","starting record");
        mRecorder.start();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
谢谢

    public void startRecording() {
    //all file code is TEMPORARY until server is working
    String timestamp = new SimpleDateFormat("MMddyyyy_HHmmss").format(new Date());
    videoFile = new File(Environment.getExternalStorageDirectory(), "/Pictures/"+timestamp+".mp4");
    Log.e("file", "Saving to: " + videoFile.getAbsolutePath());

    if (!videoFile.exists()) {
        File parent = videoFile.getParentFile();
        if (parent != null) {
            try {
                videoFile.createNewFile();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    cameraTriggered = true;

    if(!cameraPresent()) return;

    if (mRecorder == null) {
        mRecorder = new MediaRecorder();
    }

    mCamera.unlock();
    mRecorder.setCamera(mCamera);

    mRecorder.setPreviewDisplay(mHolder.getSurface());
    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

    mRecorder.setPreviewDisplay(mHolder.getSurface());

    if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        mRecorder.setOrientationHint(90);
    }
    else {
        mRecorder.setOrientationHint(0);
    }

    mRecorder.setOutputFile(videoFile.getAbsolutePath());

    try {
        Log.e("Recorder","starting prepare");
        mRecorder.prepare();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    try {
        Log.e("Recorder","starting record");
        mRecorder.start();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}