Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void startRecording() throws IOException {
- // Creating file
- File dir = MainActivity.this.getExternalFilesDir("test/" + currentFolderName);
- try {
- videofile = File.createTempFile("video", ".mp4", dir);
- } catch (IOException e) {
- Log.e(TAG, "External storage access error");
- return;
- }
- Log.i("CAMERA","Recording");
- // Creating MediaRecorder and specifying video source, output format, encoder, and output file
- recorder = new MediaRecorder();
- recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
- recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
- recorder.setVideoSize(640, 480);
- recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
- recorder.setOutputFile(videofile.getAbsolutePath());
- recorder.prepare();
- Surface surface = recorder.getSurface();
- recorder.start();
- }
- public void stopRecording() {
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- try {
- recorder.stop();
- } catch (IllegalStateException e) {
- Log.e(TAG, "Error stopping MediaRecorder: " + e.getMessage());
- }
- }
- }, 500); // Add a 500ms delay before stopping the recording
- recorder.reset();
- recorder.release();
- // After stopping the recorder, create the video file and add it to the media library
- addRecordingToMediaLibrary();
- }
- protected void addRecordingToMediaLibrary() {
- // Creating content values
- ContentValues values = new ContentValues();
- long current = System.currentTimeMillis();
- values.put(MediaStore.Video.Media.TITLE, "video" + videofile.getName());
- values.put(MediaStore.Video.Media.DATE_ADDED, (int) (current / 1000));
- values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
- values.put(MediaStore.Video.Media.DATA, videofile.getAbsolutePath());
- // Creating content resolver and storing it in the external content uri
- ContentResolver contentResolver = getContentResolver();
- Uri base = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
- Uri newUri = contentResolver.insert(base, values);
- // Sending broadcast message to scan the media file so that it can be available
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
- Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
- }
Advertisement
Add Comment
Please, Sign In to add comment