Using Mediarecorder to write an Android recording

Source: Internet
Author: User
Tags prepare reset

1. Use Mediarecorder Recording

The Mediarecorder class is used for media sampling, including audio and video. This is the class used to record audio and video methods, and the recorded data is generally written to the file. So we can use this class to achieve the function of audio recording and recording video. Here's an easy way to record Mediarecorder:

Mediarecorder Recorder = new Mediarecorder ();

Recorder.setaudiosource (MediaRecorder.AudioSource.MIC);

Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);

Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);

Recorder.setoutputfile (path_name);

Recorder.prepare ();

Recorder.start (); Start recording

...

Recorder.stop ();

Recorder.reset (); Can reuse the object by going back to Setaudiosource () step

Recorder.release (); Now the object cannot is reused

The above is a simple process code that uses Mediarecorder recordings, and basically the code you use will enable recording to function.

2, the Mediarecorder related class

Edited by Mythou

http://www.cnblogs.com/mythou/

Define audio encoding

Class Mediarecorder.audioencoder

Defining Sound Resources

Class Mediarecorder.audiosource

Callback interface, which is called when the recording error occurs

Interface Mediarecorder.onerrorlistener

Callback interface, which is called when the recording error occurs

Interface Mediarecorder.oninfolistener

Define output format

Class Mediarecorder.outputformat

Define video encoding

Class Mediarecorder.videoencoder

Define video Source

Class Mediarecorder.videosource

3, Mediarecorder Common methods

Gets the highest value of the audio signal source.

Final static int Getaudiosourcemax ()

Finally, the absolute value of the maximum amplitude is returned when this method is sampled

int Getmaxamplitude ()

Prepare recorder to start capturing and encoding data

void Prepare ()

Publish the resource associated with this Mediarecorder object

void Release ()

Reboot Mediarecorder to idle state

void Reset ()

Sets the number of audio channels recorded.

void setaudiochannels (int numchannels)

Set the encoding format for audio

void Setaudioencoder (int audio_encoder)

Set the recorded audio encoding bit rate

void setaudioencodingbitrate (int bitrate)

Sets the audio sample rate for the recording.

void setaudiosamplingrate (int samplingrate)

Sets the sound source to use for recording.

void Setaudiosource (int audio_source)

Pass the path of the video file over the auxiliary time.

void Setauxiliaryoutputfile (String path)

void Setauxiliaryoutputfile (FileDescriptor fd)

During the auxiliary time passed by the file descriptor the video

Set up a recording camera

void Setcamera (Camera c)

Set the capture rate for video frames

void Setcapturerate (double fps)

To set the maximum duration of a record session (in milliseconds)

void setmaxduration (int max_duration_ms)

Sets the maximum size (in bytes) for a record session

void Setmaxfilesize (Long max_filesize_bytes)

Registers a callback when an error is invoked and the recording

void Setonerrorlistener (Mediarecorder.onerrorlistener l)

Registers a callback to be invoked at the same time that an information event occurs.

void Setoninfolistener (Mediarecorder.oninfolistener listener)

To set the direction of the output video playback

void setorientationhint (int degrees)

Pass the file descriptor for the file to be written

void Setoutputfile (FileDescriptor fd)

To set the path of the output file

void Setoutputfile (String path)

Set the format of output files produced during recording

void Setoutputformat (int output_format)

Surface settings display a preview of the recording media (video)

void Setpreviewdisplay (Surface sv)

Use settings for Camcorderprofile objects from one record

void Setprofile (Camcorderprofile profile)

Set up a video encoder for recording

void Setvideoencoder (int video_encoder)

Set the recorded video encoding bit rate

void setvideoencodingbitrate (int bitrate)

Set the video frame rate to capture

void setvideoframerate (int rate)

Set the width and height of the video to capture

void setvideosize (int width, int height)

Start capturing and encoding data to Setoutputfile (specified file)

void Setvideosource (int video_source)

Start recording

void Start ()

Stop Recording

void Stop ()

4. Play the recording

MPlayer = new MediaPlayer ();

Mplayer.setdatasource (Msamplefile.getabsolutepath ());

Mplayer.setoncompletionlistener (this);

Mplayer.setonerrorlistener (this);

Mplayer.prepare ();

Mplayer.start ();

//.......

Mplayer.stop ();

Mplayer.release ();

MPlayer = null;

The above is a simple process of playing the recording, combined with the above recording code, you can achieve recording and playback.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.