MediaRecorder输出0字节文件

时间:2016-04-19 15:30:07

标签: android mediarecorder

我想使用MediaRecorder录制视频和音频。

这是工作。

但是当我检查输出文件时。

我无法播放输出视频文件。

由于,

输出文件大小为0 byte.

视频时间也是0秒..

请检查我的代码。

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();
    }
}

}

什么是问题?

1 个答案:

答案 0 :(得分:2)

在您正在执行的代码else中:

recorder.stop();

initRecorder(); - >> "问题在这里"

问题在这里: 完成对方法initRecorder();

的调用后

其中包含以下代码行

recorder.setOutputFile(OUTPUT_FILE);

它具有相同的文件路径。 其中用新的空文件重写旧文件。

要避免这种情况,请使用带有文件名的时间戳,如下所示:

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;
}

现在方法initRecorder()看起来像这样:

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
    }