Android之路

导航

MediaRecorder实现无声录制视频的思考

    本文是由文章https://blog.csdn.net/u010998327/article/details/71447618  引发的思考。在文中作者说,同时取消设置setAudioSource()和setAudioEncoder()能实现无声录制视频。但自测发现即使不设置上述两个,但若设置了setProfile(),则可能导致失败,具体原因,可从源码看出setProfile()在一定的条件下默认也会设置setAudioEncoder,因此需注意:

源码如下:

/**
     * Uses the settings from a CamcorderProfile object for recording. This method should
     * be called after the video AND audio sources are set, and before setOutputFile().
     * If a time lapse CamcorderProfile is used, audio related source or recording
     * parameters are ignored.
     *
     * @param profile the CamcorderProfile to use
     * @see android.media.CamcorderProfile
     */
public void setProfile(CamcorderProfile profile) {
        setOutputFormat(profile.fileFormat);
        setVideoFrameRate(profile.videoFrameRate);
        setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
        setVideoEncodingBitRate(profile.videoBitRate);
        setVideoEncoder(profile.videoCodec);
        if (profile.quality >= CamcorderProfile.QUALITY_TIME_LAPSE_LOW &&
             profile.quality <= CamcorderProfile.QUALITY_TIME_LAPSE_QVGA) {
            // Nothing needs to be done. Call to setCaptureRate() enables
            // time lapse video recording.
        } else {
            setAudioEncodingBitRate(profile.audioBitRate);
            setAudioChannels(profile.audioChannels);
            setAudioSamplingRate(profile.audioSampleRate);
            setAudioEncoder(profile.audioCodec);    //此处.....同理我认为audio相关的其他方法也不能设置,读者可自行验证!
        }
}

 另一种解决思路:

在我们的项目中,是通过控制是否静音麦克风来实现无声录制视频的,即:

AudioManager am = (AudioManager)mActivity.getSystemService(Context.AUDIO_SERVICE);
am.setMicrophoneMute(enable);    //enable是否静音麦克风

 

 

 

 

 

 

    

posted on 2019-07-29 15:38  Android之路  阅读(1250)  评论(0编辑  收藏  举报