0

在我的家庭活动中单击按钮后,我试图自动在 android 设备上录制视频。

它是这样发生的:我在家庭活动中有一个按钮(激活)。单击它时,将启动新的活动视频捕获。在那个活动中,我实现了视频捕获。它在 OnClick 侦听器实现中运行良好。

但我想要的是:当活动开始时,会显示 5 秒倒计时 - 它可以在倒计时结束后开始录制,并且应该开始新的倒计时 15 秒,在此录制应该停止并进行一些处理之后. - 它不起作用。

我得到什么错误:我在 startRec() 方法的行 recorder.start() 上得到一个 nullpointerexception。

我还检查了 Log Cat,我发现就在 recorder.start() 被调用之前,我的 Recorder 是按照 Log cat 准备的。但突然它的引用变为 null。我该如何解决这个问题。如果这不是实施我的解决方案的正确方法,那可能是一种替代方法。我不希望用户单击开始和停止录制。

这是我的活动代码:

public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback {

    public static final String LOGTAG = "VIDEOCAPTURE";

    private MediaRecorder recorder;
    private SurfaceHolder holder;
    private CamcorderProfile camcorderProfile;
    private Camera camera;  

    boolean recording = false;
    boolean usecamera = true;
    boolean previewRunning = false;

    private TextView countdown;
    private TextView videotimer;
    private TextView emailnotifier;

    CountDownTimer videotimeremaining;
    CountDownTimer initialCountDown;

    String file_path;

    private SharedPreferences my_prefs;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
        my_prefs = PreferenceManager.getDefaultSharedPreferences(this);     
        setContentView(R.layout.activity_video);
        countdown = (TextView)findViewById(R.id.countdown);
        videotimer = (TextView)findViewById(R.id.video_timer);
        emailnotifier = (TextView) findViewById(R.id.emailtextview);
        int CD = 4; // Count Down
        SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
        holder = cameraView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        initialCountDown =  new CountDownTimer(4000, 1000) {

             public void onTick(long millisUntilFinished) {
                 countdown.setText("" + millisUntilFinished / 1000);
             }

             public void onFinish() {
                countdown.setVisibility(View.GONE);
                videotimeremaining.start();
                changeRecordingStatus();
             }
          }.start();

         videotimeremaining = new CountDownTimer(15000, 1000) {

                 public void onTick(long millisUntilFinished) {
                     videotimer.setText("00:" + millisUntilFinished / 1000);
                 }

                 public void onFinish() {
                     videotimer.setVisibility(View.GONE);
                     changeRecordingStatus();

                 }
              };    

        cameraView.setClickable(false);

        //cameraView.setOnClickListener(this);
    }



    private void changeRecordingStatus(){
        if(!recording)
            startRec();
        else
            stopRec();
    }

    public void prepareRecorder() {
        recorder = new MediaRecorder();
        recorder.setPreviewDisplay(holder.getSurface());

        if (usecamera) {

                camera.unlock();
                recorder.setCamera(camera);
            Log.i(LOGTAG, "recorder is set");

        }

        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

        recorder.setProfile(camcorderProfile);

        File dir = new File("/sdcard/PanicButton/");
        if(!dir.exists()){
            dir.mkdir();
        }

        // This is all very sloppy
        if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.THREE_GPP) {
            try {
                    File newFile = File.createTempFile("/PanicButton/", ".3gp", Environment.getExternalStorageDirectory());
                    recorder.setOutputFile(newFile.getAbsolutePath());
                    file_path = newFile.getAbsolutePath().toString();
            }catch (IOException e) {
                    Log.v(LOGTAG,"Couldn't create file");
                    e.printStackTrace();
                    finish();
            }

        }else {
             try {

                 File newFile = File.createTempFile("/PanicButton/", ".mp4", Environment.getExternalStorageDirectory());
                    recorder.setOutputFile(newFile.getAbsolutePath());
                    file_path = newFile.getAbsolutePath().toString();
             }catch (Exception e) {
                    Log.v(LOGTAG,"Couldn't create file");
                    e.printStackTrace();
                    finish();
             }

        }
            recorder.setMaxDuration(15000); // 15 seconds
            //recorder.setMaxFileSize(5000000); // Approximately 5 megabytes

        try {
                recorder.prepare();
                Log.i(LOGTAG, "Recorder Prepared");
        }catch (IllegalStateException e) {
                e.printStackTrace();
                finish();
        } catch (IOException e) {
                e.printStackTrace();
                finish();
        }
    }


    public void startRec(){
        recording = true;
        recorder.start();
        Log.v(LOGTAG, "Recording Started");

    }

    public void stopRec(){
        if (recording) {
            recorder.stop();
                if (usecamera) {
                            try {
                                    camera.reconnect();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                }   
                // recorder.release();
            recording = false;
            Log.v(LOGTAG, "Recording Stopped");
            //Toast.makeText(this, "Video Is Ready To Be Sent", Toast.LENGTH_SHORT).show();
            //Testing emails
            if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = 
                        new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                }



            // Let's prepareRecorder so we can record again
            prepareRecorder();

        }
    }   


    public void surfaceCreated(SurfaceHolder holder) {
        Log.v(LOGTAG, "surfaceCreated");

        if (usecamera) {
            camera = Camera.open();
            Log.i(LOGTAG, "Camera is set");
            try {
                    camera.setPreviewDisplay(holder);
                    camera.startPreview();
                    previewRunning = true;
                    Log.i(LOGTAG, "Camera preview is running");
            }
            catch (IOException e) {
                    Log.e(LOGTAG,e.getMessage());
                    e.printStackTrace();
            }   
        }   

    }


    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.v(LOGTAG, "surfaceChanged");

        if (!recording && usecamera) {
        if (previewRunning){
            camera.stopPreview();
            Log.i(LOGTAG, "Camera preview stopped");
        }

        try {
            Camera.Parameters p = camera.getParameters();

            p.setPreviewSize(camcorderProfile.videoFrameWidth, camcorderProfile.videoFrameHeight);
            p.setPreviewFrameRate(camcorderProfile.videoFrameRate);

            camera.setParameters(p);

            camera.setPreviewDisplay(holder);
            camera.startPreview();

            previewRunning = true;
            Log.i(LOGTAG, "Camera preview running with parameteres set");
        }
        catch (IOException e) {
            Log.e(LOGTAG,e.getMessage());
            e.printStackTrace();
        }   

            prepareRecorder();  
        }
    }


    public void surfaceDestroyed(SurfaceHolder holder) {
            Log.v(LOGTAG, "surfaceDestroyed");
            if (recording) {
                    recorder.stop();
                    recording = false;
            }
                    recorder.release();
            if (usecamera) {
                previewRunning = false;
                //camera.lock();
                camera.release();
            }
            finish();
    }


}
4

1 回答 1

0

搬进recorder = new MediaRecorder();onCreate()

于 2013-06-15T13:09:50.700 回答