Android uses the mediarecorder class to record video

Programming tips remind you to use mediarecorder to set the recording code steps in the order specified by the API, otherwise an error will be reported

The steps are:

1. Set video source, audio source, i.e. input source

2. Format output

3. Set the encoding format of audio and video

1、 First, look at the layout file. Here is a surfaceview, which is a drawing container that can directly obtain image data from hardware interfaces such as memory or DMA,

2、 Activity code

1. Let's first look at member variables

2. Do some initialization in the oncreate () method

     startRecord = (Button) findViewById(R.id.startRecord); stopRecord = (Button) findViewById(R.id.stopRecord); surfaceView = (SurfaceView) findViewById(R.id.surView); // Oncreate() is initialized. Recording was definitely not started at the beginning, so the stop button cannot be clicked. Stoprecord. Setenabled (false); / / setting the surface does not require maintaining its own buffer surfaceview. Getholder(). SetType (surfaceholder. Surface_type_push_buffers)// Set the resolution surfaceview. Getholder(). Setfixedsize (320280)// Setting this component will not make the screen close automatically. Surfaceview. Getholder(). Setkeepscreen on (true);

3. Now look at the "start recording" listening event

----3.1

----3.2 set three steps and fix the sequence

----3.3 other optional settings, please see API for more information

----3.4 setting the file storage path is simple here. In actual development, it is necessary to judge whether there is external storage, whether there is a target directory, whether there are files with the same name, etc

//Set output file path

----3.5 preview using surfaceview

----3.6 start recording

----3.7 stop recording

Full code:

This example is just a simple demo for coders who have just come into contact with mediarecorder audio recording. There are some bugs and shortcomings. Coders can continue to expand

Insufficient:

1. Only when you click "start recording", the surfaceview component can see the preview taken by the camera. Otherwise, it is a black article. Here you can see the canera class and make corresponding adjustments

2. To save the path, you need to determine whether there is external storage, whether the storage space is sufficient, whether the path does not exist, and whether there are existing files with the same file name. Set the file name. Here is the fixed path and fixed file name

3. The camera preview effect has a 90 degree rotation. Here, you need to look at the mediorecoder API to set it

4. No focus is set, the pixels are unclear, and the camera width and height are deformed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>