【问题标题】:Android record video without audioAndroid录制没有音频的视频
【发布时间】:2012-08-12 08:42:50
【问题描述】:

Android 是否可以在没有音频流的情况下从相机录制视频?

目标:减小输出文件大小。

【问题讨论】:

    标签: android android-camera mediarecorder android-mediarecorder


    【解决方案1】:

    您可以通过从内置配置文件 (CamcorderProfile) 中复制必填字段来准备 MediaRecorder。只需忽略音频设置,您就可以开始使用了。根据您的需要编辑下面的代码,第 3 步是这里的重要部分。

    private boolean prepareVideoRecorder() {
    
        mCamera = getCameraInstance();
        mMediaRecorder = new MediaRecorder();
    
        // store the quality profile required
        CamcorderProfile profile = CamcorderProfile.get(cameraid, CamcorderProfile.QUALITY_HIGH);
    
        // Step 1: Unlock and set camera to MediaRecorder
        mCamera.unlock();
        mMediaRecorder.setCamera(mCamera);
    
        // Step 2: Set sources
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    
        // Step 3: Set all values contained in profile except audio settings
        mMediaRecorder.setOutputFormat(profile.fileFormat);
        mMediaRecorder.setVideoEncoder(profile.videoCodec);
        mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
        mMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
        mMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
    
        // Step 4: Set output file
        mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
    
        // Step 5: Set the preview output
        mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
    
        // Step 6: Prepare configured MediaRecorder
        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            releaseMediaRecorder();
            return false;
        } catch (IOException e) {
            releaseMediaRecorder();
            return false;
        }
        return true;
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 MediaRecorder 而无需对其调用 setAudio*。这是我第一次使用 MediaRecorder,但这个例子似乎有效:

      public class CamcorderView extends SurfaceView implements
              SurfaceHolder.Callback {
      
          private SurfaceHolder mHolder;
      
          private Camera mCamera;
          private MediaRecorder mRecorder;
      
          public CamcorderView(Context context, AttributeSet attrs) {
              super(context, attrs);
      
              mHolder = getHolder();
              mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
              mHolder.addCallback(this);
      
              mCamera = Camera.open();
              mRecorder = new MediaRecorder();
      
          }
      
          public void stop() {
              mRecorder.stop();
          }
      
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width,
                  int height) {
          }
      
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
          }
      
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
              mCamera.unlock();
              mRecorder.setCamera(mCamera);
      
              mRecorder.setPreviewDisplay(mHolder.getSurface());
      
              // You may want to change these
              mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
              mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
              mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
      
              // You'll definitely want to change this
              mRecorder.setOutputFile("/mnt/sdcard/out");
      
              try {
                  mRecorder.prepare();
              } catch (IllegalStateException e) {
                  Log.e("IllegalStateException", e.toString());
              } catch (IOException e) {
                  Log.e("IOException", e.toString());
              }
              mRecorder.start();
      
          }
      }
      

      您可能还想致电:

      • setVideoSize(int, int);
      • setVideoFrameRate(int);

      【讨论】:

      • 谢谢!真的行!也许您知道如何使用 CamcorderProfile 应用此设置?因为我使用自动生成的参数作为 camcorderProfile =CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); ,所以所有设置都应用为 mediaRecorder.setProfile(camcorderProfile);
      • 您能否提供更多信息,因为它无法正常工作。
      • 很遗憾,省略AudioSource 不适用于Android 10+; MediaRecorder.prepare() 失败。我一直在寻找适用于此 API 级别的方法,但没有成功。
      • 我没有 android 10+,所以我帮不上忙。也有一段时间不做安卓应用了。
      猜你喜欢
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多