如何使用自定义媒体录音机进行录音?

发布于 2024-12-11 06:26:16 字数 2465 浏览 1 评论 0原文

我正在为 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;

            }
    }

};

我可能已经接近答案了,但我只需要一些指导,也许我在某个地方出了问题?任何帮助就足够了。提前致谢

I am creating an app for android 2.1 - 2.3 phones and I need to know how to record a video and implement flash lighting when the video is being recorded. I have seen the following sample code below from various websites, and it works on 2.1 but anything higher and the preview screen remains dark.

    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();

    }

When I click on this button below, the recording starts, but before that the screen is black and doesn't show a preview.

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;

            }
    }

};

I am probably close to the answer, but I just need some guidance perhaps i am putting something wrong somewhere? Any help will suffice. Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2024-12-18 06:26:16

在你的prepareRecorder()方法上,在调用prepare()之前也添加这一行;

 recorder.setCamera(camera);

On your prepareRecorder() method add this line too before calling prepare();

 recorder.setCamera(camera);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文