0

停止录制后,我似乎无法在我的应用程序中再次开始录制。

基本上,我似乎没有正确处理 MediaRecorder 的停止过程,因此一旦我再次点击“捕获按钮”,它就可以重新开始。

我知道有很多代码要复制/粘贴,但我想知道我的有什么问题。

这是我的代码:

    public class MainActivity extends Activity implements SurfaceHolder.Callback {

    private MediaRecorder mediaRecorder = null;
    private MediaPlayer mediaPlayer = null;
    private String fileName = null;
    private boolean recording = false;
    private String auxDate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SurfaceView surface = (SurfaceView) findViewById(R.id.cam_surface);
        SurfaceHolder holder = surface.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        final Button btnCapture = (Button)findViewById(R.id.button_capture);
        final Button btnStop = (Button)findViewById(R.id.button_stop);
        btnCapture.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                try{
                Time now = new Time();  
                now.setToNow();
                int auxforDate = 0;
                auxforDate = now.year;
                auxDate = Integer.toString(auxforDate);
                auxforDate = now.month + 1;
                auxDate = auxDate + "_" + Integer.toString(auxforDate);
                auxforDate = now.monthDay;
                auxDate = auxDate + "_" + Integer.toString(auxforDate);
                auxforDate = now.hour;
                auxDate = auxDate + "_" + Integer.toString(auxforDate);
                auxforDate = now.minute;
                auxDate = auxDate + "_" + Integer.toString(auxforDate);
                auxforDate = now.second;
                auxDate = auxDate + "_" + Integer.toString(auxforDate);
                } catch (Exception e){ e.printStackTrace();
                try {
                    throw e;
                } catch (Exception e1) {
                    e1.printStackTrace();
                }}
                fileName = Environment.getExternalStorageDirectory() + "/MobiNanny/" + auxDate + ".mp4";
                btnCapture.setEnabled(false);
                btnStop.setEnabled(true);
                prepareRecorder();
                mediaRecorder.setOutputFile(fileName);
                try {
                    mediaRecorder.prepare();
                } catch (IllegalStateException e) {
                } catch (IOException e) {
                } mediaRecorder.start();
                recording = true;
            }
        });

        btnStop.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(recording == true){
                    mediaRecorder.stop();
                    mediaRecorder.reset();
                    btnCapture.setEnabled(true);
                    btnStop.setEnabled(false);
                    recording = false;
                } else {
                    //Agregar pop-up (Dialog) que diga "The device is not recording."
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    public void surfaceCreated(SurfaceHolder holder) {

        if (mediaRecorder == null) {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setPreviewDisplay(holder.getSurface());
        }
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDisplay(holder);
        }

    }

    public void surfaceDestroyed(SurfaceHolder holder) {

        mediaRecorder.release();
        mediaPlayer.release();      
    }

    public void prepareRecorder(){

        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mediaRecorder.setVideoFrameRate(5);
        mediaRecorder.setVideoSize(320, 240);
        }
}

此致。

4

1 回答 1

0

您必须重新设置预览显示。移动此代码

mediaRecorder.setPreviewDisplay(holder.getSurface());

这里:

mediaRecorder.setOutputFile(fileName);
mediaRecorder.setPreviewDisplay(holder.getSurface());
try {
    mediaRecorder.prepare();
} catch (IllegalStateException e) {
} catch (IOException e) {
}

这应该适合你。

于 2013-08-26T11:21:24.257 回答