下面列出了android.media.MediaRecorder#setVideoSource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void initRecorder()
{
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncodingBitRate(100000000);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoSize(videoSize.getWidth(),videoSize.getHeight());
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setAudioEncodingBitRate(128000);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setOutputFile(recordedPath);
}
/**
* 初始化保存屏幕录像的参数
*/
private void initRecorder() {
VMLog.i("初始化媒体记录器");
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(initSavePath());
mediaRecorder.setVideoSize(width, height);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
mediaRecorder.setVideoFrameRate(30);
try {
mediaRecorder.prepare();
VMLog.i("媒体记录器 准备 完成");
} catch (IOException e) {
VMLog.e("媒体记录器 准备 出错了");
e.printStackTrace();
}
}
private boolean prepareMediaRecorder() {
mMediaRecorder = new MediaRecorder();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P));
mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
String path = getSDPath();
if (path != null) {
File dir = new File(path + "/VideoRecorderTest");
if (!dir.exists()) {
dir.mkdir();
}
path = dir + "/" + getDate() + ".mp4";
mMediaRecorder.setOutputFile(path);
try {
mMediaRecorder.prepare();
} catch (IOException e) {
releaseMediaRecorder();
e.printStackTrace();
}
}
return true;
}
@Override
public void startRecording(Camera.PreviewCallback previewCallback) {
recording = true;
mCamera.unlock();
mRecordbutton.setBackgroundResource(R.drawable.red_circle_background);
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.setOutputFile("/sdcard/Video.mp4");
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOrientationHint(90);
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
mCamera.startPreview();
mCamera.setPreviewCallback(previewCallback);
} catch (IOException e) {
e.printStackTrace();
}
}
protected void startRecording() throws IOException {
mMediaRecorder = new MediaRecorder(); // Works well
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mMediaRecorder.setOutputFile("/sdcard/zzzz.mp4");
mMediaRecorder.prepare();
mMediaRecorder.start();
}
@Override
public void initRecorder(Camera camera, int cameraId, Surface surface, String filePath)
{
mMediaRecorder = new MediaRecorder();
mMediaRecorder.reset();
if (camera != null)
mMediaRecorder.setCamera(camera);
mMediaRecorder.setOnErrorListener(this);
mMediaRecorder.setPreviewDisplay(surface);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//视频源
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//音频源
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//视频输出格式 也可设为3gp等其他格式
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频格式
mMediaRecorder.setVideoSize(640, 480);//设置分辨率,市面上大多数都支持640*480
// mediaRecorder.setVideoFrameRate(25);//设置每秒帧数 这个设置有可能会出问题,有的手机不支持这种帧率就会录制失败,这里使用默认的帧率,当然视频的大小肯定会受影响
//这里设置可以调整清晰度
mMediaRecorder.setVideoEncodingBitRate(2 * 1024 * 512);
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK)
mMediaRecorder.setOrientationHint(90);
else
mMediaRecorder.setOrientationHint(270);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//视频录制格式
mMediaRecorder.setOutputFile(filePath);
}
/**
* 動画撮影のための準備を行います.
*
* @throws IOException 動画撮影の準備に失敗した場合に発生
*/
public synchronized void setUpMediaRecorder(File outputFile) throws IOException {
if (DEBUG) {
Log.e(TAG, "Set up MediaRecorder");
Log.e(TAG, " VideoSize: " + mSettings.getWidth() + "x" + mSettings.getHeight());
Log.e(TAG, " BitRate: " + mSettings.getBitRate());
Log.e(TAG, " FrameRate: " + mSettings.getFrameRate());
Log.e(TAG, " OutputFile: " + outputFile.getAbsolutePath());
}
int rotation = getDisplayRotation();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(outputFile.getAbsolutePath());
mMediaRecorder.setVideoEncodingBitRate(mSettings.getBitRate());
mMediaRecorder.setVideoFrameRate(mSettings.getFrameRate());
mMediaRecorder.setVideoSize(mSettings.getWidth(), mSettings.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setOrientationHint(ORIENTATIONS[mSettings.getSensorOrientation()].get(rotation));
mMediaRecorder.setOnInfoListener(mOnInfoListener);
mMediaRecorder.setOnErrorListener(mOnErrorListener);
mMediaRecorder.prepare();
}
private void start(Activity activity, int resultCode, Intent data) {
if (mediaProjectionManager == null) {
return;
}
startTime = System.currentTimeMillis();
try {
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int density = metrics.densityDpi;
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(getSaveFile());
mediaRecorder.setVideoSize(screenWidth, screenHeight);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncodingBitRate(screenWidth * screenHeight);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.prepare();
display = mediaProjection.createVirtualDisplay(QPMScreenRecorderManager.class.getSimpleName(),
screenWidth, screenHeight, density, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mediaRecorder.getSurface(), null, null);
mediaRecorder.start();
} catch (Exception e){
e.printStackTrace();
onRecorderFailed(activity);
}
}
public void recordVideo(VideoStartCallback videoStartCallback, VideoStopCallback videoStopCallback, VideoErrorCallback videoErrorCallback) {
try {
this.videoStartCallback = videoStartCallback;
this.videoStopCallback = videoStopCallback;
this.videoErrorCallback = videoErrorCallback;
if (mCameraDevice == null || !mTextureView.isAvailable() || mPreviewSize == null) {
this.videoErrorCallback.onVideoError("Camera not ready.");
return;
}
videoFile = Environment.getExternalStorageDirectory() + "/" + formatter.format(new Date()) + ".mp4";
mMediaRecorder = new MediaRecorder();
//mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(videoFile);
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
if (swappedDimensions) {
mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(mDisplayOrientation));
} else {
mMediaRecorder.setOrientationHint(ORIENTATIONS.get(mDisplayOrientation));
}
mMediaRecorder.prepare();
closePreviewSession();
createCameraRecordSession();
} catch (IOException ex) {
Log.d(TAG, "Video Recording error"+ex.getMessage());
}
}
public void recordVideo(VideoStartCallback videoStartCallback, VideoStopCallback videoStopCallback, VideoErrorCallback videoErrorCallback) {
try {
this.videoStartCallback = videoStartCallback;
this.videoStopCallback = videoStopCallback;
this.videoErrorCallback = videoErrorCallback;
if(mCameraDevice == null || !mTextureView.isAvailable() || mPreviewSize == null){
this.videoErrorCallback.onVideoError("Camera not ready.");
return;
}
videoFile = Environment.getExternalStorageDirectory() + "/" + formatter.format(new Date()) + ".mp4";
mMediaRecorder = new MediaRecorder();
//mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(videoFile);
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
if(swappedDimensions) {
mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(mDisplayOrientation));
} else {
mMediaRecorder.setOrientationHint(ORIENTATIONS.get(mDisplayOrientation));
}
mMediaRecorder.prepare();
closePreviewSession();
createCameraRecordSession();
} catch(IOException ex) {
Log.d(TAG, ex.getMessage());
}
}
private void startRecord() {
mediaRecorder = new MediaRecorder();
mediaRecorder.reset();
mCamera.unlock();
// 设置录制视频源为Camera(相机)
mediaRecorder.setCamera(mCamera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// 设置录制的视频编码h263 h264
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
mediaRecorder.setVideoSize(width, height);
// 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
if (SELECTED_CAMERA == CAMERA_FRONT_POSITION) {
mediaRecorder.setOrientationHint(270);
} else {
mediaRecorder.setOrientationHint(90);
}
mediaRecorder.setMaxDuration(10000);
mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
mediaRecorder.setVideoFrameRate(20);
mediaRecorder.setPreviewDisplay(mHolder.getSurface());
// 设置视频文件输出的路径
mediaRecorder.setOutputFile("/sdcard/love.mp4");
try {
//准备录制
mediaRecorder.prepare();
// 开始录制
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
protected void configureMediaRecorder(final MediaRecorder recorder, android.hardware.Camera camera) throws IllegalStateException, IllegalArgumentException {
recorder.setCamera(camera);
recorder.setAudioSource(mCaptureConfiguration.getAudioSource());
recorder.setVideoSource(mCaptureConfiguration.getVideoSource());
CamcorderProfile baseProfile = mCameraWrapper.getBaseRecordingProfile();
baseProfile.fileFormat = mCaptureConfiguration.getOutputFormat();
RecordingSize size = mCameraWrapper.getSupportedRecordingSize(mCaptureConfiguration.getVideoWidth(), mCaptureConfiguration.getVideoHeight());
baseProfile.videoFrameWidth = size.width;
baseProfile.videoFrameHeight = size.height;
baseProfile.videoBitRate = mCaptureConfiguration.getVideoBitrate();
baseProfile.audioCodec = mCaptureConfiguration.getAudioEncoder();
baseProfile.videoCodec = mCaptureConfiguration.getVideoEncoder();
recorder.setProfile(baseProfile);
recorder.setMaxDuration(mCaptureConfiguration.getMaxCaptureDuration());
recorder.setOutputFile(mVideoFile.getFullPath());
recorder.setOrientationHint(mCameraWrapper.getRotationCorrection());
try {
recorder.setMaxFileSize(mCaptureConfiguration.getMaxCaptureFileSize());
} catch (IllegalArgumentException e) {
CLog.e(CLog.RECORDER, "Failed to set max filesize - illegal argument: " + mCaptureConfiguration.getMaxCaptureFileSize());
} catch (RuntimeException e2) {
CLog.e(CLog.RECORDER, "Failed to set max filesize - runtime exception");
}
recorder.setOnInfoListener(this);
}
public void setUpMediaRecorder() {
if (null == mContext) {
return;
}
Activity activity = (Activity) mContext;
mMediaRecorder = new MediaRecorder();
mCamera.stopPreview();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mNextVideoAbsolutePath = getVideoFilePath(activity);
mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
switch (getOrientation(mCameraId)) {
case SENSOR_ORIENTATION_DEFAULT_DEGREES:
mMediaRecorder.setOrientationHint(ORIENTATIONS.get(rotation));
break;
case SENSOR_ORIENTATION_INVERSE_DEGREES:
mMediaRecorder.setOrientationHint(rotation);
break;
}
try {
mMediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
protected void configureMediaRecorder(final MediaRecorder recorder, android.hardware.Camera camera)
throws IllegalStateException, IllegalArgumentException {
recorder.setCamera(camera);
recorder.setAudioSource(mCaptureConfiguration.getAudioSource());
recorder.setVideoSource(mCaptureConfiguration.getVideoSource());
CamcorderProfile baseProfile = mCameraWrapper.getBaseRecordingProfile();
baseProfile.fileFormat = mCaptureConfiguration.getOutputFormat();
RecordingSize size = mCameraWrapper.getSupportedRecordingSize(mCaptureConfiguration.getVideoWidth(), mCaptureConfiguration.getVideoHeight());
baseProfile.videoFrameWidth = size.width;
baseProfile.videoFrameHeight = size.height;
baseProfile.videoBitRate = mCaptureConfiguration.getVideoBitrate();
baseProfile.audioCodec = mCaptureConfiguration.getAudioEncoder();
baseProfile.videoCodec = mCaptureConfiguration.getVideoEncoder();
recorder.setProfile(baseProfile);
recorder.setMaxDuration(mCaptureConfiguration.getMaxCaptureDuration());
recorder.setOutputFile(mVideoFile.getFullPath());
recorder.setOrientationHint(mCameraWrapper.getRotationCorrection());
recorder.setVideoFrameRate(mCaptureConfiguration.getVideoFPS());
try {
recorder.setMaxFileSize(mCaptureConfiguration.getMaxCaptureFileSize());
} catch (IllegalArgumentException e) {
CLog.e(CLog.RECORDER, "Failed to set max filesize - illegal argument: " + mCaptureConfiguration.getMaxCaptureFileSize());
} catch (RuntimeException e2) {
CLog.e(CLog.RECORDER, "Failed to set max filesize - runtime exception");
}
recorder.setOnInfoListener(this);
}
public void startRecord(final String filePath, final String camera, final int width, final int height, final int quality, final boolean withFlash){
Log.d(TAG, "CameraPreview startRecord camera: " + camera + " width: " + width + ", height: " + height + ", quality: " + quality);
Activity activity = getActivity();
muteStream(true, activity);
if (this.mRecordingState == RecordingState.STARTED) {
Log.d(TAG, "Already Recording");
return;
}
this.recordFilePath = filePath;
int mOrientationHint = calculateOrientationHint();
int videoWidth = 0;//set whatever
int videoHeight = 0;//set whatever
Camera.Parameters cameraParams = mCamera.getParameters();
if (withFlash) {
cameraParams.setFlashMode(withFlash ? Camera.Parameters.FLASH_MODE_TORCH : Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(cameraParams);
mCamera.startPreview();
}
mCamera.unlock();
mRecorder = new MediaRecorder();
try {
mRecorder.setCamera(mCamera);
CamcorderProfile profile;
if (CamcorderProfile.hasProfile(defaultCameraId, CamcorderProfile.QUALITY_HIGH)) {
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_HIGH);
} else {
if (CamcorderProfile.hasProfile(defaultCameraId, CamcorderProfile.QUALITY_480P)) {
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_480P);
} else {
if (CamcorderProfile.hasProfile(defaultCameraId, CamcorderProfile.QUALITY_720P)) {
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_720P);
} else {
if (CamcorderProfile.hasProfile(defaultCameraId, CamcorderProfile.QUALITY_1080P)) {
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_1080P);
} else {
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_LOW);
}
}
}
}
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mRecorder.setProfile(profile);
mRecorder.setOutputFile(filePath);
mRecorder.setOrientationHint(mOrientationHint);
mRecorder.prepare();
Log.d(TAG, "Starting recording");
mRecorder.start();
eventListener.onStartRecordVideo();
} catch (IOException e) {
eventListener.onStartRecordVideoError(e.getMessage());
}
}
private boolean prepareMediaRecorder() {
try {
// final Activity activity = getActivity();
//if (null == activity) return false;
//final BaseCaptureInterface captureInterface = (BaseCaptureInterface) activity;
// setCameraDisplayOrientation(mCamera.getParameters());
mMediaRecorder = new MediaRecorder();
camera.stopPreview();
camera.unlock();
mMediaRecorder.setCamera(camera);
// boolean canUseAudio = true;
//boolean audioEnabled = !mInterface.audioDisabled();
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
// canUseAudio = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
// if (canUseAudio && audioEnabled) {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
// } else if (audioEnabled) {
// Toast.makeText(getActivity(), R.string.mcam_no_audio_access, Toast.LENGTH_LONG).show();
// }
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
final CamcorderProfile profile = CamcorderProfile.get(currentCameraId, CamcorderProfile.QUALITY_HIGH);
mMediaRecorder.setOutputFormat(profile.fileFormat);
mMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mMediaRecorder.setVideoSize(previewSize.width, previewSize.height);
mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mMediaRecorder.setVideoEncoder(profile.videoCodec);
mMediaRecorder.setAudioEncodingBitRate(profile.audioBitRate);
mMediaRecorder.setAudioChannels(profile.audioChannels);
mMediaRecorder.setAudioSamplingRate(profile.audioSampleRate);
mMediaRecorder.setAudioEncoder(profile.audioCodec);
Uri uri = Uri.fromFile(FileUtils.makeTempFile(
new File(Environment.getExternalStorageDirectory(),
"/Omoshiroi/videos").getAbsolutePath(),
"VID_", ".mp4"));
mMediaRecorder.setOutputFile(uri.getPath());
// if (captureInterface.maxAllowedFileSize() > 0) {
// mMediaRecorder.setMaxFileSize(captureInterface.maxAllowedFileSize());
// mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
// @Override
// public void onInfo(MediaRecorder mediaRecorder, int what, int extra) {
// if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
// Toast.makeText(getActivity(), R.string.mcam_file_size_limit_reached, Toast.LENGTH_SHORT).show();
// stopRecordingVideo(false);
// }
// }
// });
// }
mMediaRecorder.setOrientationHint(90);
// mMediaRecorder.setPreviewDisplay(mPreviewView.getHolder().getSurface());
mMediaRecorder.prepare();
return true;
} catch (Exception e) {
camera.lock();
e.printStackTrace();
return false;
}
}
private boolean prepareMediaRecorder() {
// 默认使用最高质量拍摄
CamcorderProfile profile =
CamcorderProfile.
get(Integer.valueOf((String) mConfig.getCurrentCameraId()), CamcorderProfile.QUALITY_720P);
Size previewSize = chooseOptimalSize();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(profile.fileFormat);
mMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
// mMediaRecorder.setVideoSize(mAspectRatio.getX(), mAspectRatio.getY());
// mMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
mMediaRecorder.setVideoSize(previewSize.getWidth(), previewSize.getHeight());
@SuppressWarnings("ConstantConditions")
int sensorOrientation = mCameraCharacteristics.get(
CameraCharacteristics.SENSOR_ORIENTATION);
int rotation = (sensorOrientation +
mDisplayOrientation * (mConfig.getCurrentFacing() == Constants.CAMERA_FACING_FRONT ? 1 : -1) +
360) % 360;
mMediaRecorder.setOrientationHint(rotation);
mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mMediaRecorder.setVideoEncoder(profile.videoCodec);
mMediaRecorder.setAudioEncodingBitRate(profile.audioBitRate);
mMediaRecorder.setAudioChannels(profile.audioChannels);
mMediaRecorder.setAudioSamplingRate(profile.audioSampleRate);
mMediaRecorder.setAudioEncoder(profile.audioCodec);
mMediaRecorder.setOutputFile(mConfig.getResultFile().getAbsolutePath());
// 如果针对拍摄的文件大小和拍摄时间有设置的话,也可以在这里设置
try {
mMediaRecorder.prepare();
return true;
} catch (Exception e) {
Log.d(TAG, "prepareMediaRecorder: " + e);
}
// 这个时候出现了错误,应该释放有关资源
releaseVideoRecorder();
return false;
}
private boolean prepareMediaRecorder() {
try {
// final Activity activity = getActivity();
//if (null == activity) return false;
//final BaseCaptureInterface captureInterface = (BaseCaptureInterface) activity;
// setCameraDisplayOrientation(mCamera.getParameters());
mMediaRecorder = new MediaRecorder();
camera.stopPreview();
camera.unlock();
mMediaRecorder.setCamera(camera);
// boolean canUseAudio = true;
//boolean audioEnabled = !mInterface.audioDisabled();
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
// canUseAudio = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
// if (canUseAudio && audioEnabled) {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
// } else if (audioEnabled) {
// Toast.makeText(getActivity(), R.string.mcam_no_audio_access, Toast.LENGTH_LONG).show();
// }
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
final CamcorderProfile profile = CamcorderProfile.get(currentCameraId, CamcorderProfile.QUALITY_HIGH);
mMediaRecorder.setOutputFormat(profile.fileFormat);
mMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mMediaRecorder.setVideoSize(previewSize.width, previewSize.height);
mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mMediaRecorder.setVideoEncoder(profile.videoCodec);
mMediaRecorder.setAudioEncodingBitRate(profile.audioBitRate);
mMediaRecorder.setAudioChannels(profile.audioChannels);
mMediaRecorder.setAudioSamplingRate(profile.audioSampleRate);
mMediaRecorder.setAudioEncoder(profile.audioCodec);
Uri uri = Uri.fromFile(FileUtils.makeTempFile(
new File(Environment.getExternalStorageDirectory(),
"/Omoshiroi/videos").getAbsolutePath(),
"VID_", ".mp4"));
mMediaRecorder.setOutputFile(uri.getPath());
// if (captureInterface.maxAllowedFileSize() > 0) {
// mMediaRecorder.setMaxFileSize(captureInterface.maxAllowedFileSize());
// mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
// @Override
// public void onInfo(MediaRecorder mediaRecorder, int what, int extra) {
// if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
// Toast.makeText(getActivity(), R.string.mcam_file_size_limit_reached, Toast.LENGTH_SHORT).show();
// stopRecordingVideo(false);
// }
// }
// });
// }
mMediaRecorder.setOrientationHint(90);
// mMediaRecorder.setPreviewDisplay(mPreviewView.getHolder().getSurface());
mMediaRecorder.prepare();
return true;
} catch (Exception e) {
camera.lock();
e.printStackTrace();
return false;
}
}
@SuppressLint("SimpleDateFormat")
protected boolean prepareMediaRecorder() throws IOException {
mediaRecorder = new MediaRecorder(); // Works well
camera.stopPreview();
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
if (flag == 1) {
mediaRecorder.setProfile(CamcorderProfile.get(1, CamcorderProfile.QUALITY_HIGH));
} else {
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
}
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setOrientationHint(mOrientation);
if (Build.MODEL.equalsIgnoreCase("Nexus 6") && flag == 1) {
if (mOrientation == 90) {
mediaRecorder.setOrientationHint(mOrientation);
} else if (mOrientation == 180) {
mediaRecorder.setOrientationHint(0);
} else {
mediaRecorder.setOrientationHint(180);
}
} else if (mOrientation == 90 && flag == 1) {
mediaRecorder.setOrientationHint(270);
} else if (flag == 1) {
mediaRecorder.setOrientationHint(mOrientation);
}
mediaFileName = "wc_vid_" + System.currentTimeMillis();
mediaRecorder.setOutputFile(folder.getAbsolutePath() + "/" + mediaFileName + ".mp4"); // Environment.getExternalStorageDirectory()
mediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
public void onInfo(MediaRecorder mr, int what, int extra) {
// TODO Auto-generated method stub
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
long downTime = 0;
long eventTime = 0;
float x = 0.0f;
float y = 0.0f;
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_UP,
0,
0,
metaState
);
imgCapture.dispatchTouchEvent(motionEvent);
Toast.makeText(WhatsappCameraActivity.this, "You reached to Maximum(25MB) video size.", Toast.LENGTH_SHORT).show();
}
}
});
mediaRecorder.setMaxFileSize(1000 * 25 * 1000);
try {
mediaRecorder.prepare();
} catch (Exception e) {
releaseMediaRecorder();
e.printStackTrace();
return false;
}
return true;
}
private void setUpMediaRecorder(final File outputFile) throws IOException {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(outputFile.getAbsolutePath());
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
int hint;
SparseIntArray orientations;
if (mFacing == Camera2Recorder.CameraFacing.FRONT) {
switch (rotation) {
case Surface.ROTATION_0:
rotation = Surface.ROTATION_0;
break;
case Surface.ROTATION_90:
rotation = Surface.ROTATION_270;
break;
case Surface.ROTATION_180:
rotation = Surface.ROTATION_180;
break;
case Surface.ROTATION_270:
rotation = Surface.ROTATION_90;
break;
}
}
switch (mSensorOrientation) {
case SENSOR_ORIENTATION_INVERSE_DEGREES:
orientations = INVERSE_ORIENTATIONS;
break;
case SENSOR_ORIENTATION_DEFAULT_DEGREES:
default:
orientations = DEFAULT_ORIENTATIONS;
break;
}
hint = orientations.get(rotation);
mMediaRecorder.setOrientationHint(hint);
mMediaRecorder.prepare();
if (DEBUG) {
Log.d(TAG, "VideoSize: " + mVideoSize.getWidth() + "x" + mVideoSize.getHeight());
Log.d(TAG, "OutputFile: " + outputFile.getAbsolutePath());
Log.d(TAG, "Facing: " + mFacing.getName());
Log.d(TAG, "SensorOrientation: " + mSensorOrientation);
Log.d(TAG, "DisplayRotation: " + rotation);
Log.d(TAG, "OrientationHint: " + hint);
}
}