demand

Use your Android phone’s camera to take photos.

Recording process:

mediarecorder = new MediaRecorder(); / / create the mediarecorder object/source/set to record video Camera (Camera) mediarecorder. SetVideoSource (mediarecorder. VideoSource. Camera); / / set to record after the completion of the video format of encapsulation THREE_GPP for 3 gp. MPEG_4 to mp4 mediarecorder setOutputFormat (mediarecorder. OutputFormat. THREE_GPP); / / set to record the video encoding h263 h264 mediarecorder. SetVideoEncoder (mediarecorder. VideoEncoder. H264); // Set the video recording resolution. Must be placed behind the set encoding and format, otherwise an error mediarecorder. SetVideoSize (176, 144); // Set the video frame rate for recording. Must be placed behind the set encoding and format, otherwise an error mediarecorder. SetVideoFrameRate (20); mediarecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); // Set the output path of the video file lastFileName = newFileName(); mediarecorder.setOutputFile(lastFileName); Try {// Prepare 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(); }Copy the code

End of shoot:

if (mediarecorder ! = null) {// Stop mediaRecorder (); mediarecorder.release(); mediarecorder = null; }Copy the code

My own wrapper class:

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 the mediarecorder object/source/set to record video Camera (Camera) mediarecorder. SetVideoSource (mediarecorder. VideoSource. Camera); / / set to record after the completion of the video format of encapsulation THREE_GPP for 3 gp. MPEG_4 to mp4 mediarecorder setOutputFormat (mediarecorder. OutputFormat. THREE_GPP); / / set to record the video encoding h263 h264 mediarecorder. SetVideoEncoder (mediarecorder. VideoEncoder. H264); // Set the video recording resolution. Must be placed behind the set encoding and format, otherwise an error mediarecorder. SetVideoSize (176, 144); // Set the video frame rate for recording. Must be placed behind the set encoding and format, otherwise an error mediarecorder. SetVideoFrameRate (20); mediarecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); // Set the output path of the video file lastFileName = newFileName(); mediarecorder.setOutputFile(lastFileName); Try {// Prepare 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++; }}, 0100, 0); } Timer timer; int timeSize = 0; private String lastFileName; public void stopRecording() { if (mediarecorder ! = null) {// Stop mediaRecorder (); 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 (); mediarecorder.release(); mediarecorder = null; }}}Copy the code

Play:

public void play(String fileName, SurfaceView view) { mPlayer = new MediaPlayer(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setDisplay(view.getHolder()); / / define a SurfaceView mPlayer. Play it 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(); }Copy the code

At the end of playback:

    public void stop() {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
Copy the code

reference

Blog.csdn.net/peijiangpin…