1. Use Mediarecorder for recording
The MediaRecorder class is used for media sampling, including audio and video. That is, the class used to record audio and video methods. The recorded data is generally written into the file. Therefore, we can use this class to implement the recording and video recording functions. The following describes how to use MediaRecorder for recording:
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 (); // You can reuse the object by going back to setAudioSource () step
Recorder. release (); // Now the object cannot be reused
The above is a simple process code using MediaRecorder recording. Basically, you can use the code to implement the recording function.
2. MediaRecorder-related classes
// Edited by mythou
// Http://www.cnblogs.com/mythou/
// Define the Audio Encoding
Class MediaRecorder. AudioEncoder
// Define sound resources
Class MediaRecorder. AudioSource
// Callback interface, called when a recording error occurs
Interface MediaRecorder. OnErrorListener
// Callback interface, called when a recording error occurs
Interface MediaRecorder. OnInfoListener
// Define the output format
Class MediaRecorder. OutputFormat
// Define the Video Encoding
Class MediaRecorder. VideoEncoder
// Define the video source
Class MediaRecorder. VideoSource
3. Common MediaRecorder Methods
// Obtain the maximum value of the audio signal source.
Final static int getAudioSourceMax ()
// The absolute value of the maximum amplitude is returned when this method is called for sampling.
Int getMaxAmplitude ()
// Prepare recorder to start capturing and encoding data
Void prepare ()
// Publish resources associated with this MediaRecorder object
Void release ()
// Restart mediarecorder to idle state
Void reset ()
// Set the number of audio channels for recording.
Void setAudioChannels (int numChannels)
// Set the audio encoding format
Void setAudioEncoder (int audio_encoder)
// Set the recorded audio encoding bit rate
Void setAudioEncodingBitRate (int bitRate)
// Set the recording audio sampling rate.
Void setAudioSamplingRate (int samplingRate)
// Set the audio source for recording.
Void setAudioSource (int audio_source)
// The path of the video file over the auxiliary time.
Void setAuxiliaryOutputFile (String path)
Void setAuxiliaryOutputFile (FileDescriptor fd)
// Video transmitted in the file descriptor over the auxiliary time
// Set a recording camera
Void setCamera (Camera c)
// Set the video frame capture rate
Void setCaptureRate (double fps)
// Set the maximum session recording duration (MS)
Void setMaxDuration (int max_duration_ms)
// Set the maximum size of the record session (in bytes)
Void setMaxFileSize (long max_filesize_bytes)
// When an error occurs when registering a callback called and recording
Void setOnErrorListener (MediaRecorder. OnErrorListener l)
// Register the callback to be called when an information event is recorded at the same time.
Void setOnInfoListener (MediaRecorder. OnInfoListener listener)
// Set the output video playback direction prompt
Void setOrientationHint (int degrees)
// Pass the file descriptor of the file to be written
Void setOutputFile (FileDescriptor fd)
// Set the path of the output file
Void setOutputFile (String path)
// Set the format of the output file generated during the recording process
Void setOutputFormat (int output_format)
// Preview of the display record media (video) on the surface settings
Void setPreviewDisplay (Surface sv)
// Set the usage of a recorded CamcorderProfile object
Void setProfile (CamcorderProfile profile)
// Sets the Video Encoder for recording.
Void setVideoEncoder (int video_encoder)
// Sets the recorded video encoding bit rate.
Void setVideoEncodingBitRate (int bitRate)
// Set the Frame Rate of the video to be captured
Void setVideoFrameRate (int rate)
// Set the width and height of the video to be captured.
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 a recording. Combined with the above recording code, you can achieve recording and playback.