📜 ⬆️ ⬇️

Working with the camera in Android: shoot video

In the comments on the article Working with the camera in Android , a question was asked about how to make a video. It turns out that all this is done quite simply.

For recording video (as well as audio) the MediaRecorder class is responsible .
Actually, to enable recording, you need to do the following:

1. Create an object of class MediaRecorder
MediaRecorder recorder = new MediaRecorder(); 

2. Configure audio and video sources
 recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 

as a sound source, you can also specify:
MediaRecorder.AudioSource.MIC - ,
MediaRecorder.AudioSource.CAMCORDER - ,

3. Set the output format
 recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 

You can also set:
MediaRecorder.OutputFormat.MPEG_4 - mp4
MediaRecorder.OutputFormat.THREE_GPP - 3gp

4. Set video and audio recording parameters (for example, by the following values)
 recorder.setVideoEncodingBitRate(150000); //   recorder.setAudioEncodingBitRate(8000); //   recorder.setAudioSamplingRate(8000); //     recorder.setAudioChannels(1); //     recorder.setVideoFrameRate(30); //    recorder.setVideoSize(640, 480); //   recorder.setMaxDuration(0); //    recorder.setMaxFileSize(0); //    

5. Set file name for recording
 recorder.setOutputFile(); 

6. Set preview
 recorder.setPreviewDisplay(Surface); 

7. Prepare to record
 recorder.prepare(); 

8. The long-awaited launch. At this point, the preview will turn on and recording will begin.
 recorder.start(); 

To finish:
9. Stop recording
 recorder.stop(); 

10. If you want to use the same object for another entry with different settings.
 recorder.reset(); //   ,  setAudioSource() 

11. Freeing the object
 recorder.release(); 

If you want to see a preview before recording, then you need to make friends with the camera and recorder. To do this, do the following:

before recording:
- stop camera preview
 camera.stopPreview(); 

- allow camera sharing
 camera.unlock(); 

- set the camera object to the recorder
 recorder.setCamera(camera); 

after recording:
- we prohibit camera sharing
 camera.reconnect(); 

- turn on camera preview
 camera.startPreview(); 

And, of course, do not forget to turn off photography during video recording.
')
Sources adapted for video recording of the photography program from the previous article (the settings menu has also been added) can be downloaded here .

When writing, the following sources of information were used:

1. Shawn Van Every. Android Media Pro: Developing Graphics, Music, Video and Rich Media Apps for Smartphones and Tablets. Apress 2009.
2. Camera class description
3. Class Description MediaRecorder

Source: https://habr.com/ru/post/113480/


All Articles