`

Android Mediarecorder录制的时候屏蔽掉声音

阅读更多

Android Mediarecorder录制的时候屏蔽掉声音

 

项目需求,在拍摄音视频时候将声音屏蔽,找了有关方面的资料,现总结下:

一、在原有工程中使用如下代码:

mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
		if (SettingsUtils.SETTINGS_SHOW_VOICE_SWITCH == 0){
			
			mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
		}else{
			
			mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK);
		}
			
		if (DrivingRecorderUtil.CURSDKVERSION >= 8) {
			CamcorderProfile targetProfile = CamcorderProfile
					.get(CamcorderProfile.QUALITY_HIGH);
			mMediaRecorder.setProfile(targetProfile);
			
		} else {
			mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
			mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
		}
		
		mMediaRecorder.setVideoSize(SettingsUtils.SETTINGS_SHOW_FRAME[0],SettingsUtils.SETTINGS_SHOW_FRAME[1]);

 

 原因CamcorderProfile 设置不仅仅是分辨率,它还将设置的东西作为输出格式和编码器 (用于音频和视频),所以放弃此方法使用。

二、修改代码如下:

//start实现录像静音
		/*mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
		mMediaRecorder.setVideoSize(SettingsUtils.SETTINGS_SHOW_FRAME[0],SettingsUtils.SETTINGS_SHOW_FRAME[1]);
		//设置编码比特率,不设置会使视频图像模糊
		mMediaRecorder.setVideoEncodingBitRate(5*1024*1024);
		mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);*/
		//end

 

三、参考:

下面是我的工作代码来记录结构视频和音频:

问题: 1) 为什么是 CamcorderProfile 需要? setProfile(...) 似乎无论 QUALITY_HIGH 给,但后来我设置我想要的尺寸设置尺寸 setVideoSize(...) ,它会重写此。然而,当我删除 CamcorderProfile 两条线,这款应用程序崩溃在setVideoSize(...) 与 LogCat E/MediaRecorder(19526): setVideoSize called in an invalid state: 2 

2) 如何不录制音频?文件指出,如果 setAudioSource(...) 不是叫,会有没有音频轨道。然而,当我删除这条线时应用程序崩溃在 setProfile(...) 与 LogCat E/MediaRecorder(19946): try to set the audio encoder without setting the audio source first 

3) 如果删除了这两个 CamcorderProfile 线和 setAudioSource(...) 线,它与 1 时崩溃)。

4) 我也试过添加行

recorder.setOutputFormat(OutputFormat.DEFAULT);

而不是 CamcorderProfile 线。但现在它崩溃在 perpare() 。如果 setAudioSource(...) 被称为 LogCat 是:E/MediaRecorder(20737): audio source is set, but audio encoder is not set 如果它不叫 LogCat 就是:E/MediaRecorder(20544): video source is set, but video encoder is not set

我有一家印花布互联网,我无法找到正确的方法,设置 MediaRecorder 的一个好例子。在这里它意味着在 API 8 后你应该使用 CamcorderProfile 类,但对我来说它急转弯的问题。

任何帮助就太好了!谢谢 !

代码 (如下面是在运行时的工作方式):

recorder = new MediaRecorder();
recorder.setCamera(<<camera>>);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(profile);

recorder.setOutputFile(<<Path String>>);
recorder.setVideoSize(<<Width>>, <<Height>>);

recorder.setPreviewDisplay(<<Surface>>);

recorder.setOrientationHint(0); 
recorder.setMaxDuration(10000);
recorder.setOnInfoListener(this);

try
{
    recorder.prepare();
    recorder.start();
} catch ...

解决方法 1:

我没有很多的经验与 MediaRecorder 但我正在读一些相关的主题,我会尝试回答您的问题:

1、 3 和 4)CamcorderProfile 设置不仅仅是分辨率,它还将设置的东西作为输出格式和编码器 (用于音频和视频)。你给出错误,因为您可能需要使用 setOutputFormat 之前调用 setVideoSize 并且您需要致电 setVideoEncoder setAudioEncoder 在它之后,如果你不想要使用 CamcorderProfile。[根据这个答案]

2)再次,CamcorderProfile 还设置音频属性 (如比特率的编码解码器,SampleRate,...) 所以你需要在调用它,这就是为什么之前设置音频源应用程序崩溃。如果你不想录制音频请尝试下一个代码: (我没测试它所以我其实不知道如果它工作,但我敢肯定它不会)

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

另外请注意,如果您不想使用 CamcorderProfile (你想对记录音频或视频只有的意思) 您可能需要设置其他参数,以保证你有你想要的质量。看看下面的代码示例:

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

我希望这可以帮助您。

 

四、解决视频模糊情况参考

 

使用MediaRecorder類、Camera開發基於Android系統手機的錄像功能

獲得的視頻文件十分模糊,無法達到系統再帶的相機所錄製的效果

經過分析后,發現在錄像的時候沒有使用自動聚焦功能,從而導致視頻效果極差

但是添加了自動對焦的代碼后,SurfaceView中的預覽已經達到預期效果,但是問題是錄製的視頻卻是花屏

 

在这里我提高了帧频率,然后就清晰了
mMediaRecorder.setVideoEncodingBitRate(5*1024*1024);

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics