The phone's camera developed by android USES MediaRecorder to record and play video

  • 2020-05-09 19:15:48
  • OfStack

Let's play with the recording function of the phone. Do a DEMO.
Check out the recording process:
 
mediarecorder = new MediaRecorder();//  create mediarecorder object  
//  Set the recorded video source as Camera( The camera ) 
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
//  Set the packaging format of the video after recording THREE_GPP for 3gp.MPEG_4 for mp4 
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
//  Sets the video encoding for recording h263 h264 
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 
//  Set the resolution of the video recording. Must be placed after the setting code and format, otherwise error is reported  
mediarecorder.setVideoSize(176, 144); 
//  Sets the frame rate of the recorded video. Must be placed after the setting code and format, otherwise error is reported  
mediarecorder.setVideoFrameRate(20); 
mediarecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); 
//  Sets the path to the video file output  
lastFileName = newFileName(); 
mediarecorder.setOutputFile(lastFileName); 
try { 
//  To prepare the 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 of shooting:  
if (mediarecorder != null) { 
//  stop  
mediarecorder.stop(); 
mediarecorder.release(); 
mediarecorder = null; 
} 

Post my own encapsulation 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 mediarecorder object  
//  Set the recorded video source as Camera( The camera ) 
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
//  Set the packaging format of the video after recording THREE_GPP for 3gp.MPEG_4 for mp4 
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
//  Sets the video encoding for recording h263 h264 
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 
//  Set the resolution of the video recording. Must be placed after the setting code and format, otherwise error is reported  
mediarecorder.setVideoSize(176, 144); 
//  Sets the frame rate of the recorded video. Must be placed after the setting code and format, otherwise error is reported  
mediarecorder.setVideoFrameRate(20); 
mediarecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); 
//  Sets the path to the video file output  
lastFileName = newFileName(); 
mediarecorder.setOutputFile(lastFileName); 
try { 
//  To prepare the 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; 
} 
} } 

So at the same time we're going to play it:
 
public void play(String fileName, SurfaceView view) { 
mPlayer = new MediaPlayer(); 
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
mPlayer.setDisplay(view.getHolder()); //  define 1 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 play:  
public void stop() { 
if (mPlayer != null) { 
mPlayer.release(); 
mPlayer = null; 
} 
} 

Sample code is available for download
------------
Thanks to those who Shared their technical experience.

Related articles: