@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (!_isRecording) { _recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); _recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); _recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); _recorder.setVideoEncodingBitRate(10000000); _recorder.setVideoFrameRate(5); _recorder.setVideoSize(100, 100); _recorder.setOutputFile("/sdcard/sample.3gp"); _recorder.setPreviewDisplay(_holder.getSurface()); try { _recorder.prepare(); } catch (Exception e) { Log.e("test", "recorder error"); } _recorder.start(); _isRecording = true; } else { _recorder.stop(); _recorder.reset(); _isRecording = false; } } return true; }
private void startRecording() { if (cameraBusy) return; System.out.println("start - lock enabled"); cameraBusy = true; mCamera.unlock(); recorder = new MediaRecorder(); recorder.setCamera(mCamera); recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); recorder.setOutputFormat(profile.fileFormat); recorder.setVideoFrameRate(profile.videoFrameRate); recorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight); recorder.setVideoEncodingBitRate(profile.videoBitRate); recorder.setVideoEncoder(profile.videoCodec); recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); recorder.setPreviewDisplay(mHolder.getSurface()); try { recorder.prepare(); recorder.start(); recording = true; System.out.println("start - lock disabled"); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } cameraBusy = false; }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) private boolean prepareVideoRecorder() { // BEGIN_INCLUDE (configure_preview) mCamera = CameraHelper.getDefaultCameraInstance(); Camera.Parameters parameters = mCamera.getParameters(); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); mCamera.setDisplayOrientation(90); mCamera.setParameters(parameters); // BEGIN_INCLUDE (configure_media_recorder) mMediaRecorder = new MediaRecorder(); // Step 1: Unlock and set camera to MediaRecorder mCamera.unlock(); // Camera.Parameters parameters = mCamera.getParameters(); mMediaRecorder.setCamera(mCamera); CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_QVGA); // Log.d(TAG, profile.videoBitRate+"<- videoBitRate r"); profile.videoBitRate = 1024000; // Step 2: Set sources mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // Step 3: Set all values contained in profile except audio settings mMediaRecorder.setOutputFormat(profile.fileFormat); mMediaRecorder.setVideoEncoder(profile.videoCodec); mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate); mMediaRecorder.setVideoFrameRate(profile.videoFrameRate); mMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight); mMediaRecorder.setOrientationHint(90); mMediaRecorder.setVideoFrameRate(24); // Step 4: Set output file save_to = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO).toString(); mMediaRecorder.setOutputFile(save_to); mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); // Step 5: Prepare configured MediaRecorder try { mMediaRecorder.prepare(); } catch (IllegalStateException e) { Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage()); releaseMediaRecorder(); return false; } catch (IOException e) { Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage()); releaseMediaRecorder(); return false; } return true; }
@SuppressLint("NewApi") private boolean initRecorder() { if (!EaseCommonUtils.isExitsSdcard()) { showNoSDCardDialog(); return false; } if (mCamera == null) { if (!initCamera()) { showFailDialog(); return false; } } mVideoView.setVisibility(View.VISIBLE); // TODO init button mCamera.stopPreview(); mediaRecorder = new MediaRecorder(); mCamera.unlock(); mediaRecorder.setCamera(mCamera); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); // 设置录制视频源为Camera(相机) mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); if (frontCamera == 1) { mediaRecorder.setOrientationHint(270); } else { mediaRecorder.setOrientationHint(90); } // 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); // 设置录制的视频编码h263 h264 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); // 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错 mediaRecorder.setVideoSize(previewWidth, previewHeight); // 设置视频的比特率 mediaRecorder.setVideoEncodingBitRate(384 * 1024); // // 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错 if (defaultVideoFrameRate != -1) { mediaRecorder.setVideoFrameRate(defaultVideoFrameRate); } // 设置视频文件输出的路径 localPath = PathUtil.getInstance().getVideoPath() + "/" + System.currentTimeMillis() + ".mp4"; mediaRecorder.setOutputFile(localPath); mediaRecorder.setMaxDuration(30000); mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); try { mediaRecorder.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } return true; }
private void configureMediaRecorder() { mediaRecorder.setCamera(camera); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setVideoSize(1280, 720); mediaRecorder.setVideoEncodingBitRate(17000000); mediaRecorder.setVideoFrameRate(30); mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); }
/** * 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); } }
private boolean prepareVideoRecorder() { mMediaRecorder = new MediaRecorder(); // Step 1: Unlock and set camera to MediaRecorder camera.unlock(); mMediaRecorder.setCamera(camera); // Step 2: Set sources mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) // mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); // Step 3: Set output format and encoding (for versions prior to API Level 8) mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); // Step 4: Set output file mMediaRecorder.setOutputFile(ls.getFileDescriptor()); // Step 5: Set the preview output // mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); mMediaRecorder.setPreviewDisplay(holder.getSurface()); Log.i(TAG, "Surface valid: " + holder.getSurface().isValid()); /*mMediaRecorder.setVideoSize(176, 144); mMediaRecorder.setVideoFrameRate(20); mMediaRecorder.setAudioChannels(1); */ // Step 6: Prepare configured MediaRecorder try { mMediaRecorder.prepare(); } catch (Exception e) { Log.d(TAG, "Exception preparing MediaRecorder: " + e.getMessage()); releaseMediaRecorder(); return false; } return true; }
private boolean startRecording() { prCamera.stopPreview(); try { prCamera.unlock(); prMediaRecorder.setCamera(prCamera); // set audio source as Microphone, video source as camera // state: Initial=>Initialized // prMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); prMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // set the file output format: 3gp or mp4 // state: Initialized=>DataSourceConfigured String lDisplayMsg = "Current container format: "; if (Utils.puContainerFormat == SettingsDialog.cpu3GP) { lDisplayMsg += "3GP\n"; videoFormat = ".3gp"; prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); } else if (Utils.puContainerFormat == SettingsDialog.cpuMP4) { lDisplayMsg += "MP4\n"; videoFormat = ".mp4"; prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); } else { lDisplayMsg += "3GP\n"; videoFormat = ".3gp"; prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); } lDisplayMsg += "Current encoding format: "; if (Utils.puEncodingFormat == SettingsDialog.cpuH263) { lDisplayMsg += "H263\n"; prMediaRecorder.setVideoEncoder(VideoEncoder.H263); } else if (Utils.puEncodingFormat == SettingsDialog.cpuMP4_SP) { lDisplayMsg += "MPEG4-SP\n"; prMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP); } else if (Utils.puEncodingFormat == SettingsDialog.cpuH264) { lDisplayMsg += "H264\n"; prMediaRecorder.setVideoEncoder(VideoEncoder.H264); } else { lDisplayMsg += "H263\n"; prMediaRecorder.setVideoEncoder(VideoEncoder.H263); } videoFileFullPath = myVideoFilePath + String.valueOf(System.currentTimeMillis()) + videoFormat; prRecordedFile = new File(videoFileFullPath); prMediaRecorder.setOutputFile(prRecordedFile.getPath()); if (Utils.puResolutionChoice == SettingsDialog.cpuRes176) { prMediaRecorder.setVideoSize(176, 144); } else if (Utils.puResolutionChoice == SettingsDialog.cpuRes320) { prMediaRecorder.setVideoSize(320, 240); } else if (Utils.puResolutionChoice == SettingsDialog.cpuRes720) { prMediaRecorder.setVideoSize(720, 480); } Toast.makeText(prContext, lDisplayMsg, Toast.LENGTH_LONG).show(); prMediaRecorder.setVideoFrameRate(cFrameRate); prMediaRecorder.setPreviewDisplay(prSurfaceHolder.getSurface()); prMediaRecorder.setMaxFileSize(cMaxFileSizeInBytes); // prepare for capturing // state: DataSourceConfigured => prepared prMediaRecorder.prepare(); // start recording // state: prepared => recording prMediaRecorder.start(); btnStartRecorder.setText("Stop"); prRecordInProcess = true; return true; } catch (IOException _le) { _le.printStackTrace(); return false; } }
public void onBtnClick(View view) { try { switch (view.getId()) { case R.id.btnRec: if (mRecorder != null) { mRecorder.stop(); mRecorder.release(); mRecorder = null; } mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 비디오 추가 mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); // 옵션을 바꾸면 화질을 개선할 수 있다. mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); // 저장장소를 추가해준다. // 내부저장: /data/data/com.example.user.helloworld/files/videoRec.mp4 FileOutputStream outStream = openFileOutput(REC_FILE, MODE_PRIVATE); mRecorder.setOutputFile(outStream.getFD()); // 미리보기 부분 추가 mRecorder.setPreviewDisplay(mHolder.getSurface()); mRecorder.prepare(); mRecorder.start(); break; case R.id.btnRecStop: if (mRecorder == null) return; mRecorder.stop(); mRecorder.release(); mRecorder = null; // 앨범에 등록하는 부분 // ContentValues => 안드로이드 갤러리 break; case R.id.btnPlay: if (mPlayer != null) { mPlayer.stop(); mPlayer.release(); mPlayer = null; } mPlayer = new MediaPlayer(); // 내부 저장된 파일을 읽어 온다. // 내부저장: /data/data/com.example.user.helloworld/files/videoRec.mp4 FileInputStream inStream = openFileInput(REC_FILE); mPlayer.setDataSource(inStream.getFD()); // 서피스뷰에 재생을 시킨다. mPlayer.setDisplay(mHolder); mPlayer.prepare(); mPlayer.start(); break; case R.id.btnStop: break; } } catch (Exception e) { e.printStackTrace(); } }
private boolean startRecording() { prCamera.stopPreview(); try { prCamera.unlock(); prMediaRecorder.setCamera(prCamera); // set audio source as Microphone, video source as camera // state: Initial=>Initialized prMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); prMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // set the file output format: 3gp or mp4 // state: Initialized=>DataSourceConfigured String lDisplayMsg = "Current container format: "; int lContainerFormat = SettingsStatic.getContainerFormat(this.getApplicationContext()); if (lContainerFormat == SettingsDialog.cpu3GP) { lDisplayMsg += "3GP\n"; mVideoFileFullPath = ".3gp"; prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); } else if (lContainerFormat == SettingsDialog.cpuMP4) { lDisplayMsg += "MP4\n"; mVideoFileFullPath = ".mp4"; prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); } else { lDisplayMsg += "3GP\n"; mVideoFileFullPath = ".3gp"; prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); } // the encoders: // audio: AMR-NB // prMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB); prMediaRecorder.setAudioEncoder(AudioEncoder.AAC); // video: H.263, MP4-SP, or H.264 // prMediaRecorder.setVideoEncoder(VideoEncoder.H263); // prMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP); lDisplayMsg += "Current encoding format: "; int lEncodingFormat = SettingsStatic.getEncodingFormat(getApplicationContext()); if (lEncodingFormat == SettingsDialog.cpuH263) { lDisplayMsg += "H263\n"; prMediaRecorder.setVideoEncoder(VideoEncoder.H263); } else if (lEncodingFormat == SettingsDialog.cpuMP4_SP) { lDisplayMsg += "MPEG4-SP\n"; prMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP); } else if (lEncodingFormat == SettingsDialog.cpuH264) { lDisplayMsg += "H264\n"; prMediaRecorder.setVideoEncoder(VideoEncoder.H264); } else { lDisplayMsg += "H263\n"; prMediaRecorder.setVideoEncoder(VideoEncoder.H263); } mVideoFileFullPath = FileUtilsStatic.DEFAULT_DIR + DateTimeUtilsStatic.get_current_date_time_format1_str() + mVideoFileFullPath; prRecordedFile = new File(mVideoFileFullPath); prMediaRecorder.setOutputFile(prRecordedFile.getPath()); int lRes = SettingsStatic.getResolutionChoice(this.getApplicationContext()); if (lRes == SettingsDialog.cpuRes176) { prMediaRecorder.setVideoSize(176, 144); } else if (lRes == SettingsDialog.cpuRes320) { prMediaRecorder.setVideoSize(320, 240); } else if (lRes == SettingsDialog.cpuRes720) { prMediaRecorder.setVideoSize(720, 480); } lDisplayMsg += "Video recording to file: " + mVideoFileFullPath; Toast.makeText(prContext, lDisplayMsg, Toast.LENGTH_LONG).show(); prMediaRecorder.setVideoFrameRate(cFrameRate); prMediaRecorder.setPreviewDisplay(prSurfaceHolder.getSurface()); prMediaRecorder.setMaxDuration(cMaxRecordDurationInMs); prMediaRecorder.setMaxFileSize(cMaxFileSizeInBytes); // prepare for capturing // state: DataSourceConfigured => prepared prMediaRecorder.prepare(); // start recording // state: prepared => recording prMediaRecorder.start(); prStartBtn.setText("Stop"); prRecordInProcess = true; return true; } catch (IOException _le) { _le.printStackTrace(); return false; } }