Android AcousticEchoCanceler不取消音频

时间:2016-12-20 14:37:31

标签: android audio

我正在尝试使用' AcousticEchoCanceler'在我的应用程序上录制我的声音,同时通过扬声器播放音频文件。我想通过我的扬声器播放的音频就像其他人通过VOIP与我交谈。重点是,当有人通过扬声器播放音频文件时,我想知道有人正在通过麦克风讲话。

我制作了一个简单的测试应用,可以在您按下按钮的同时录制您的声音,然后立即播放。我正在进行的测试是,当音频通过扬声器播放时,我再次按下按钮并再次录制。第二个录音"应该"只是我第二次录音的声音。

private Button recordButton;
private TextView recordText;
private MediaRecorder mRecorder = null;
private static String mFileName = null;
private MediaPlayer mPlayer = null;
public AcousticEchoCanceler aec;
//public int recordingNum = 0;
public AudioManager mAudioManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Set audio mode to MODE_IN_COMMUNICATION
    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    recordButton = (Button) findViewById(R.id.recordButton);
    recordText = (TextView) findViewById(R.id.recordText);

    recordButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                recordingStart();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                recordingStop();
                playingStart();
            }
            return true;
        }
    });
}

private void recordingStart() {
    recordText.setText("RECORDING");
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e("*******", "prepare() failed");
    }
    mRecorder.start();
}

private void recordingStop() {
    recordText.setText("RECORDING STOPPED");
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
}

private void playingStart() {
    recordText.setText("PLAYING");
    mPlayer = new MediaPlayer();
    try {
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mPlayer) {
                playingStop();
            }
        });
        mPlayer.setDataSource(mFileName);
        mPlayer.prepare();
        mPlayer.start();
        //Use AcousticEchoCanceler during the playing?
        aec.create(mPlayer.getAudioSessionId());
    } catch (IOException e) {
        Log.e("*******", "prepare() failed");
    }
}

private void playingStop() {
    recordText.setText("WAITING");
    mPlayer.release();
    mPlayer = null;
}

public MainActivity() {
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest1.3gp";
}

我可能没有正确使用AEC,但我不确定从哪里开始。

0 个答案:

没有答案