如何使用自定义媒体记录器录制?

时间:2011-10-22 16:33:09

标签: android video preview mediarecorder flashlight

我正在为Android 2.1 - 2.3手机创建一个应用程序,我需要知道如何录制视频并在录制视频时实现闪光灯。我在下面的各个网站上看到了以下示例代码,它适用于2.1但更高的内容,预览屏幕仍然很暗。

    boolean recording=false;
    Camera camera = null;
    Parameters parameters;
    SurfaceView cameraView = (SurfaceView) findViewById(R.id.surface_view);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    holder.getSurface();

    private void prepareRecorder() 
{

    Log.d("BROADCAST", "Camera not null");
    recorder.setPreviewDisplay(holder.getSurface());
     try{

        recorder.prepare();


 //implementing flash button below but fails as soon as recording begins when turned on

      Button flash = (Button) findViewById (R.id.flash);
       flash.setOnClickListener(new Button.OnClickListener(){
      @Override
      public void onClick(View arg0) {
      // TODO Auto-generated method stub
      if(camera == null){
      camera = Camera.open();
      parameters = camera.getParameters();
      parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
       camera.setParameters(parameters);

       }else{
           parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
           camera.setParameters(parameters);
           camera.release();
           camera = null;
            }

        }});


    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }


}



public void surfaceCreated(SurfaceHolder holder) 
   {
    prepareRecorder();
    Log.v(TAG,"surfaceCreated");
   }


public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {}

public void surfaceDestroyed(SurfaceHolder holder) 
    {

    recorder.release();

    }

当我点击下面的这个按钮时,录制开始,但在此之前,屏幕是黑色的,不显示预览。

private Button.OnClickListener myButtonOnClickListener = new Button.OnClickListener (){
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
          if (recording) {

                recorder.stop();
             recorder.release();// stops the recording process
             startActivity(new Intent(""));

      finish();
      } 
      else {
                recorder.start();
                counter.start();
                recording = true;

            }
    }

};

我可能接近答案,但我只是需要一些指导,或许我在某处犯了错误?任何帮助都足够了。提前致谢

1 个答案:

答案 0 :(得分:0)

在prepareRecorder()方法上,在调用prepare();

之前添加此行
 recorder.setCamera(camera);