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 に答える