下面列出了android.media.MediaRecorder#setProfile ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void configureRecorder(int quality, MediaRecorder recorder) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraInfo.cameraId, info);
int displayOrientation = getDisplayOrientation(info, false);
recorder.setOrientationHint(displayOrientation);
int highProfile = getHigh();
boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile);
boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW);
if (canGoHigh && (quality == 1 || !canGoLow)) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile));
} else if (canGoLow) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW));
} else {
throw new IllegalStateException("cannot find valid CamcorderProfile");
}
isVideo = true;
}
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();
}
protected void configureRecorder(int quality, MediaRecorder recorder) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraInfo.cameraId, info);
int displayOrientation = getDisplayOrientation(info, false);
int outputOrientation = 0;
if (jpegOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
outputOrientation = (info.orientation - jpegOrientation + 360) % 360;
} else {
outputOrientation = (info.orientation + jpegOrientation) % 360;
}
}
recorder.setOrientationHint(outputOrientation);
int highProfile = getHigh();
boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile);
boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW);
if (canGoHigh && (quality == 1 || !canGoLow)) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile));
} else if (canGoLow) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW));
} else {
throw new IllegalStateException("cannot find valid CamcorderProfile");
}
isVideo = true;
}
protected void configureRecorder(int quality, MediaRecorder recorder) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraInfo.cameraId, info);
int displayOrientation = getDisplayOrientation(info, false);
int outputOrientation = 0;
if (jpegOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
outputOrientation = (info.orientation - jpegOrientation + 360) % 360;
} else {
outputOrientation = (info.orientation + jpegOrientation) % 360;
}
}
recorder.setOrientationHint(outputOrientation);
int highProfile = getHigh();
boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile);
boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW);
if (canGoHigh && (quality == 1 || !canGoLow)) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile));
} else if (canGoLow) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW));
} else {
throw new IllegalStateException("cannot find valid CamcorderProfile");
}
isVideo = true;
}
@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);
}
@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);
}
protected void configureRecorder(int quality, MediaRecorder recorder) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraInfo.cameraId, info);
int displayOrientation = getDisplayOrientation(info, false);
int outputOrientation = 0;
if (jpegOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
outputOrientation = (info.orientation - jpegOrientation + 360) % 360;
} else {
outputOrientation = (info.orientation + jpegOrientation) % 360;
}
}
recorder.setOrientationHint(outputOrientation);
int highProfile = getHigh();
boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile);
boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW);
if (canGoHigh && (quality == 1 || !canGoLow)) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile));
} else if (canGoLow) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW));
} else {
throw new IllegalStateException("cannot find valid CamcorderProfile");
}
isVideo = true;
}
protected void configureRecorder(int quality, MediaRecorder recorder) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraInfo.cameraId, info);
int displayOrientation = getDisplayOrientation(info, false);
int outputOrientation = 0;
if (jpegOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
outputOrientation = (info.orientation - jpegOrientation + 360) % 360;
} else {
outputOrientation = (info.orientation + jpegOrientation) % 360;
}
}
recorder.setOrientationHint(outputOrientation);
int highProfile = getHigh();
boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile);
boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW);
if (canGoHigh && (quality == 1 || !canGoLow)) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile));
} else if (canGoLow) {
recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW));
} else {
throw new IllegalStateException("cannot find valid CamcorderProfile");
}
isVideo = true;
}
@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;
}
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());
}
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
camera = Camera.open();
try {
mediaRecorder = new MediaRecorder();
List<int[]> fps = camera.getParameters().getSupportedPreviewFpsRange();
int preview_fps[] = fps.get(0);
for (int i[] : camera.getParameters().getSupportedPreviewFpsRange())
preview_fps = (mCaptureRate <= i[1] && mCaptureRate > i[0]) ? i : preview_fps;
Camera.Parameters param = camera.getParameters();
param.setVideoStabilization(true);
param.setPreviewFpsRange(preview_fps[0], preview_fps[1]);
camera.setParameters(param);
camera.unlock();
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setCamera(camera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile profile;
if (mCaptureRate > 25) {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
} else {
profile = CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_HIGH);
mediaRecorder.setCaptureRate(mCaptureRate);
profile.videoFrameRate = ((int) Math.ceil(mCaptureRate));
}
mediaRecorder.setProfile(profile);
mediaRecorder.setOutputFile(mOutFile);
mediaRecorder.prepare();
mediaRecorder.start();
ScaleAnimation a = new ScaleAnimation(3, 2, 3, 2);
a.setDuration(2000);
surfaceView.startAnimation(a);
Log.i(TAG, String.format("recording %s with rate %.2f", mOutFile, mCaptureRate));
} catch(Exception e) {
onDestroy();
Log.d(TAG, e.toString());
}
}