如何同时将Android手机上每个麦克风的音频录制到单独的文件中?

时间:2019-06-25 14:47:47

标签: java android android-mediarecorder android-audiorecord

在android studio中,我能够创建mediaRecorder实例并记录音频,然后可以创建具有不同音频源的mediaRecorder实例并记录音频。问题是我不能一次拥有两个mediaRecorder(或者我认为如此)。

除了mediaRecorder之外,我还研究了使用两个不同的AudioRecord对象,但似乎有人在一个月前尝试过这种方法,但它也不起作用。我已经研究过mediaMuxer,这可能是实现这一目标的关键,但是我对多路复用的概念并不陌生,并且不知道如何实现这种功能。

// not to professional standards  
btnRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(checkPermissionFromDevice()) {

                pathsave = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/"
                        + UUID.randomUUID().toString() + 
"_audio_record.3gp";
                setupMediaRecorder();
                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                btnPlay.setEnabled(false);
                btnStop.setEnabled(false);
                btnStopRecord.setEnabled(true);

                Toast.makeText(MainActivity.this, "Recording...", 
Toast.LENGTH_SHORT).show();
                }
                else{
                    requestPermission();
                }
            }
        });

//------------------------------------------------------------------------

btnRecord2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(checkPermissionFromDevice()) {

                pathsave2 = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/"
                        + UUID.randomUUID().toString() + 
"_audio_record.3gp";
                setupMediaRecorder2();
                try {
                    mediaRecorder2.prepare();
                    mediaRecorder2.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                btnPlay2.setEnabled(false);
                btnStop2.setEnabled(false);
                btnStopRecord2.setEnabled(true);

                Toast.makeText(MainActivity.this, "Recording...", 
Toast.LENGTH_SHORT).show();
            }
            else{
                requestPermission();
            }
        }
    });
 //-----------------------------------------------------------------------

 private void setupMediaRecorder() {
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    mediaRecorder.setOutputFile(pathsave);

}

//------------------------------------------------------------------------

private void setupMediaRecorder2() {
    mediaRecorder2 = new MediaRecorder();
    mediaRecorder2.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder2.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder2.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    mediaRecorder2.setOutputFile(pathsave2);

}

我可以分别记录,停止记录和播放两个媒体记录器。当我尝试同时记录两者时,应用程序崩溃。任何帮助将不胜感激。

0 个答案:

没有答案