Android Development calls the mobile phone camera to use MediaRecorder for video recording and Playback

Source: Internet
Author: User

Let's play with the video recording function of mobile phones. Make a DEMO.
Check the recording process: Copy codeThe Code is as follows: mediarecorder = new MediaRecorder (); // create a mediarecorder object
// Set the recorded video source to Camera (Camera)
Mediarecorder. setVideoSource (MediaRecorder. VideoSource. CAMERA );
// Set the video Encapsulation Format THREE_GPP to 3gp. MPEG_4 to mp4 after recording.
Mediarecorder. setOutputFormat (MediaRecorder. OutputFormat. THREE_GPP );
// Sets the recorded video encoding h263 h264
Mediarecorder. setVideoEncoder (MediaRecorder. VideoEncoder. H264 );
// Set the video recording resolution. It must be placed behind the encoding and format. Otherwise, an error is returned.
Mediarecorder. setVideoSize (176,144 );
// Set the Frame Rate of the recorded video. It must be placed behind the encoding and format. Otherwise, an error is returned.
Mediarecorder. setVideoFrameRate (20 );
Mediarecorder. setPreviewDisplay (surfaceView. getHolder (). getSurface ());
// Set the output path of the video file
LastFileName = newFileName ();
Mediarecorder. setOutputFile (lastFileName );
Try {
// Prepare for recording
Mediarecorder. prepare ();
// Start recording
Mediarecorder. start ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
End Shooting:
If (mediarecorder! = Null ){
// Stop
Mediarecorder. stop ();
Mediarecorder. release ();
Mediarecorder = null;
}

Paste my own encapsulation class:Copy codeThe Code is as follows: package zyf. demo. moviedemo;
Import java. io. File;
Import java. io. IOException;
Import java. util. Timer;
Import java. util. TimerTask;
Import android. media. MediaRecorder;
Import android. view. SurfaceView;
Public class MovieRecorder {
Private MediaRecorder mediarecorder;
Boolean isRecording;
Public void startRecording (SurfaceView surfaceView ){
Mediarecorder = new MediaRecorder (); // create a mediarecorder object
// Set the recorded video source to Camera (Camera)
Mediarecorder. setVideoSource (MediaRecorder. VideoSource. CAMERA );
// Set the video Encapsulation Format THREE_GPP to 3gp. MPEG_4 to mp4 after recording.
Mediarecorder. setOutputFormat (MediaRecorder. OutputFormat. THREE_GPP );
// Sets the recorded video encoding h263 h264
Mediarecorder. setVideoEncoder (MediaRecorder. VideoEncoder. H264 );
// Set the video recording resolution. It must be placed behind the encoding and format. Otherwise, an error is returned.
Mediarecorder. setVideoSize (176,144 );
// Set the Frame Rate of the recorded video. It must be placed behind the encoding and format. Otherwise, an error is returned.
Mediarecorder. setVideoFrameRate (20 );
Mediarecorder. setPreviewDisplay (surfaceView. getHolder (). getSurface ());
// Set the output path of the video file
LastFileName = newFileName ();
Mediarecorder. setOutputFile (lastFileName );
Try {
// Prepare for recording
Mediarecorder. prepare ();
// Start recording
Mediarecorder. start ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
IsRecording = true;
TimeSize = 0;
Timer = new Timer ();
Timer. schedule (new TimerTask (){
@ Override
Public void run (){
// TODO Auto-generated method stub
TimeSize ++;
}
}, 0,1000 );
}
Timer timer;
Int timeSize = 0;
Private String lastFileName;
Public void stopRecording (){
If (mediarecorder! = Null ){
// Stop
Mediarecorder. stop ();
Mediarecorder. release ();
Mediarecorder = null;
Timer. cancel ();
If (null! = LastFileName &&! "". Equals (lastFileName )){
File f = new File (lastFileName );
String name = f. getName (). substring (0,
F. getName (). lastIndexOf (". 3gp "));
Name + = "_" + timeSize + "s.3gp ";
String newPath = f. getParentFile (). getAbsolutePath () + "/"
+ Name;
If (f. renameTo (new File (newPath ))){
Int I = 0;
I ++;
}
}
}
}
Public String newFileName (){
Try {
Return File. createTempFile ("/mov _", ". 3gp"). getAbsolutePath ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return null;
}
}
Public void release (){
If (mediarecorder! = Null ){
// Stop
Mediarecorder. stop ();
Mediarecorder. release ();
Mediarecorder = null;
}
}}

At the same time, we will play it back:Copy codeThe Code is as follows: public void play (String fileName, SurfaceView view ){
MPlayer = new MediaPlayer ();
MPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );
MPlayer. setDisplay (view. getHolder (); // defines a SurfaceView to play it.
MPlayer. setOnCompletionListener (new OnCompletionListener (){
@ Override
Public void onCompletion (MediaPlayer arg0 ){
Stop ();
// Canvas. drawColor (Color. TRANSPARENT,
// PorterDuff. Mode. CLEAR );
}
});
Try {
MPlayer. setDataSource (fileName );
MPlayer. prepare ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
MPlayer. start ();
}
When playing the video:
Public void stop (){
If (mPlayer! = Null ){
MPlayer. release ();
MPlayer = null;
}
}

Download Sample Code
------------
Thank you for sharing your technical experience.

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.