Android development calls the phone's camera to use Mediarecorder video and play _android

Source: Internet
Author: User
Tags prepare
Let's play the video function of the mobile phone. Do a demo.
look at the recording process
Copy Code code as follows:

Mediarecorder = new Mediarecorder ();//Create Mediarecorder Object
Set the recording video source to camera (camera)
Mediarecorder.setvideosource (MediaRecorder.VideoSource.CAMERA);
Set the package format for the video after the recording is complete THREE_GPP to 3gp.mpeg_4 as MP4
Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
Sets the video encoding for the recording h263 H264
Mediarecorder.setvideoencoder (MediaRecorder.VideoEncoder.H264);
Sets the resolution of the video recording. Must be placed behind the set encoding and formatting, otherwise the error
Mediarecorder.setvideosize (176, 144);
Sets the video frame rate for the recording. Must be placed behind the set encoding and formatting, otherwise the error
Mediarecorder.setvideoframerate (20);
Mediarecorder.setpreviewdisplay (Surfaceview.getholder (). Getsurface ());
Set the path for video file output
Lastfilename = NewFileName ();
Mediarecorder.setoutputfile (Lastfilename);
try {
Preparing to record
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 it
Mediarecorder.stop ();
Mediarecorder.release ();
Mediarecorder = null;
}

sticking out my own package class
Copy Code code 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 Mediarecorder Object
Set the recording video source to camera (camera)
Mediarecorder.setvideosource (MediaRecorder.VideoSource.CAMERA);
Set the package format for the video after the recording is complete THREE_GPP to 3gp.mpeg_4 as MP4
Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
Sets the video encoding for the recording h263 H264
Mediarecorder.setvideoencoder (MediaRecorder.VideoEncoder.H264);
Sets the resolution of the video recording. Must be placed behind the set encoding and formatting, otherwise the error
Mediarecorder.setvideosize (176, 144);
Sets the video frame rate for the recording. Must be placed behind the set encoding and formatting, otherwise the error
Mediarecorder.setvideoframerate (20);
Mediarecorder.setpreviewdisplay (Surfaceview.getholder (). Getsurface ());
Set the path for video file output
Lastfilename = NewFileName ();
Mediarecorder.setoutputfile (Lastfilename);
try {
Preparing to record
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 it
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 it
Mediarecorder.stop ();
Mediarecorder.release ();
Mediarecorder = null;
}
} }

And then we'll play it as well:
Copy Code code as follows:

public void Play (String fileName, Surfaceview view) {
MPlayer = new MediaPlayer ();
Mplayer.setaudiostreamtype (Audiomanager.stream_music);
Mplayer.setdisplay (View.getholder ()); Define a Surfaceview 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 ();
}
At the end of playback:
public void Stop () {
if (MPlayer!= null) {
Mplayer.release ();
MPlayer = null;
}
}

Provide sample code Downloads
------------
Thanks to those who share the 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.