Guest User

Untitled

a guest
Jul 18th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | Source Code | 0 0
  1. public void startRecording() throws IOException {
  2.  
  3. // Creating file
  4. File dir = MainActivity.this.getExternalFilesDir("test/" + currentFolderName);
  5. try {
  6. videofile = File.createTempFile("video", ".mp4", dir);
  7. } catch (IOException e) {
  8. Log.e(TAG, "External storage access error");
  9. return;
  10. }
  11. Log.i("CAMERA","Recording");
  12. // Creating MediaRecorder and specifying video source, output format, encoder, and output file
  13.  
  14. recorder = new MediaRecorder();
  15. recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
  16. recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  17. recorder.setVideoSize(640, 480);
  18. recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
  19. recorder.setOutputFile(videofile.getAbsolutePath());
  20.  
  21.  
  22. recorder.prepare();
  23. Surface surface = recorder.getSurface();
  24. recorder.start();
  25. }
  26. public void stopRecording() {
  27.  
  28. new Handler().postDelayed(new Runnable() {
  29. @Override
  30. public void run() {
  31. try {
  32. recorder.stop();
  33. } catch (IllegalStateException e) {
  34. Log.e(TAG, "Error stopping MediaRecorder: " + e.getMessage());
  35. }
  36. }
  37. }, 500); // Add a 500ms delay before stopping the recording
  38. recorder.reset();
  39. recorder.release();
  40. // After stopping the recorder, create the video file and add it to the media library
  41. addRecordingToMediaLibrary();
  42. }
  43. protected void addRecordingToMediaLibrary() {
  44. // Creating content values
  45. ContentValues values = new ContentValues();
  46. long current = System.currentTimeMillis();
  47. values.put(MediaStore.Video.Media.TITLE, "video" + videofile.getName());
  48. values.put(MediaStore.Video.Media.DATE_ADDED, (int) (current / 1000));
  49. values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
  50. values.put(MediaStore.Video.Media.DATA, videofile.getAbsolutePath());
  51.  
  52. // Creating content resolver and storing it in the external content uri
  53. ContentResolver contentResolver = getContentResolver();
  54. Uri base = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  55. Uri newUri = contentResolver.insert(base, values);
  56.  
  57. // Sending broadcast message to scan the media file so that it can be available
  58. sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
  59. Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
  60. }
Advertisement
Add Comment
Please, Sign In to add comment