问题
I wanna record video and audio using MediaRecorder.
It's work.
But when i check output file.
I can't play output video file.
Because,
output filesize is 0 byte.
also video time is 0 second..
Please check my code.
public class Talk extends Activity implements SurfaceHolder.Callback{
Context context;
SurfaceView sfv_Preview;
private SurfaceHolder holder;
private MediaRecorder recorder = null;
boolean recording = false;
private static final String OUTPUT_FILE = "/sdcard/videooutput.mp4";
private static final String OUTPUT_FILE_NAME = "videooutput.mp4";
private static final int RECORDING_TIME = 10000;
//Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_talk);
recorder = new MediaRecorder();
initRecorder();
sfv_Preview = (SurfaceView) findViewById(R.id.sfv_Preview);
holder = sfv_Preview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
context = this;
((ImageButton)findViewById(R.id.ibt_Record)).setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
recorder.start();
Log.d("recod", "start");
}
else if (MotionEvent.ACTION_UP == event.getAction() || event.getAction() == MotionEvent.ACTION_CANCEL) {
Log.d("recod", "stop");
recorder.stop();
initRecorder();
prepareRecorder();
}
return false;
}
});
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
prepareRecorder();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (recording) {
recorder.stop();
recording = false;
}
recorder.release();
finish();
}
private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile(OUTPUT_FILE);
recorder.setMaxDuration(RECORDING_TIME);
recorder.setMaxFileSize(10485760); // Approximately 5 megabytes
}
private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
//recorder.setOrientationHint(90);
try {
recorder.prepare();
//finish();
} catch (IllegalStateException e) {
e.printStackTrace();
//finish();
} catch (IOException e) {
e.printStackTrace();
//finish();
}
}
}
What is problem?
回答1:
In the else
pare of your code you are doing :
recorder.stop();
initRecorder();
-->> "Problem is here"
Problem is here :
After finish recording call to method initRecorder();
which has following line of code
recorder.setOutputFile(OUTPUT_FILE);
It has same file path. Which rewrites old file by new empty file.
To avoid this use timestamp with file name like this:
private File getVideoFile() {
File videoDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "VideoList");
// Create parent directory
if (!videoDir.exists()) {
if (!videoDir.mkdirs()) {
Log.d("ZZZ", "failed to create directory");
return null;
}
}
// Create a video file
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File videoFile;
videoFile = new File(videoDir.getPath() + File.separator +
"REC_" + timeStamp + ".mp4");
return videoFile;
}
Now method initRecorder() would look like this :
private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
// recorder.setOutputFile(OUTPUT_FILE);//OLD
recorder.setOutputFile(getVideoFile());// NEW
recorder.setMaxDuration(RECORDING_TIME);
recorder.setMaxFileSize(10485760); // Approximately 5 megabytes
}
来源:https://stackoverflow.com/questions/36722936/mediarecorder-output-0-byte-file