Using android MediaRecorder

前端 未结 1 808
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 07:49

Below is the structure of my working code to record video and audio:

Questions: 1) Why is the CamcorderProfile needed? setProfile(...) appe

相关标签:
1条回答
  • 2020-12-30 08:07

    I don't have a lot of experience with MediaRecorder but I was reading some related topics and I will try to answer your questions:

    1, 3 and 4) CamcorderProfile sets more than just the resolution, it also sets things as output format and encoders (for both audio and video). You are getting the error because you probably need to use setOutputFormat before calling setVideoSize and you have to call setVideoEncoder and setAudioEncoder after it, if you do not want to use CamcorderProfile. [According to this answer]

    2) Again, CamcorderProfile also sets audio properties (such as Codec, BitRate, SampleRate,...) so you need to set an Audio Source before calling it, that's why the app crashed. If you don't want to record audio try the next code: (I didn't test it so I don't actually know if it works, but I am pretty sure it does)

    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setVideoSize(WIDTH, HEIGHT);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    recorder.setOutputFile(PATH);
    recorder.setPreviewDisplay(SURFACE);
    
    recorder.prepare();
    recorder.start();
    

    Also be aware that if you do not want to use CamcorderProfile (meaning that you want to record audio or video only) you may have to set additional parameters to assure you have the quality you want. Take a look at the following example code:

    recorder = new MediaRecorder();
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    
    // Following code does the same as getting a CamcorderProfile (but customizable)
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    // Video Settings 
    recorder.setVideoSize(WIDTH, HEIGHT);
    recorder.setVideoFrameRate(FRAME_RATE);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    recorder.setVideoEncodingBitRate(VIDEO_BITRATE);
    // Audio Settings
    recorder.setAudioChannels(AUDIO_CHANNELS);
    recorder.setAudioSamplingRate(SAMPLE_RATE);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
    recorder.setAudioEncodingBitRate(AUDIO_BITRATE);
    
    // Customizable Settings such as:
    //   recorder.setOutputFile(PATH);
    //   recorder.setPreviewDisplay(SURFACE);
    //   etc...
    
    // Prepare and use the MediaRecorder
    recorder.prepare();
    recorder.start();
    ...
    recorder.stop();
    recorder.reset();
    recorder.release();
    

    I hope this helps you.

    0 讨论(0)
提交回复
热议问题