我正在为 VoIP 应用程序进行一些回声消除实验,声音让我发疯。我想做的很简单:我以前录过声音。现在我将播放该声音并在播放时录制另一个声音,这是全双工 VoIP 场景的真实情况。我使用 MedaiPlayer 播放声音,使用 MediaRecorder 录制新声音。下面是类的完整代码,从 Android SoundRecorder Sample 修改而来。重点是, mPlayer.setAudioStreamType(AudioManager.MODE_IN_COMMUNICATION); 对于 Android 文档,请说“在通信音频模式下。已建立音频/视频聊天或 VoIP 呼叫。” 和 mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 对于 Android 文档说“为语音通信(如 VoIP)调整的麦克风音频源。例如,如果可用,它将利用回声消除或自动增益控制。如果没有应用语音处理,它的行为就像 DEFAULT 一样。”这很有希望。但如果我播放从扬声器发出的任何东西,我几乎什么也不录或只录奇怪的噪音。我不能像我一样附加录音的声波一个新用户,但是第一个是普通录音,有正常的声波。第二个录音是播放第一个时的录音,几乎没有任何内容,没有声波。Android似乎有麦克风就会关闭扬声器中的活动。我尝试了 MediaRecorder 和 MediaPlayer 的不同可能性,但没有用。在 Android 中实现 Acoustic Echo Cancellation 的正确方法是什么?我在 Sony Tablet S 上尝试并使用 Android 3.0 SDK 开发。提前致谢。
package com.kadir.sample;
import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.content.Context;
import android.util.Log;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;
public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private static String mFileNameConst = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
private PlayConstButton mPlayConstButton = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void onPlayConst(boolean start) {
if (start) {
startConstPlaying();
} else {
stopConstPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Toast.makeText(this, "prepare() failed", Toast.LENGTH_LONG).show();
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startConstPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileNameConst);
mPlayer.setAudioStreamType(AudioManager.MODE_IN_COMMUNICATION);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Toast.makeText(this, "prepare() failed", Toast.LENGTH_LONG).show();
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopConstPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Toast.makeText(this, "startRecording() failed", Toast.LENGTH_LONG).show();
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
class PlayConstButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlayConst(mStartPlaying);
if (mStartPlaying) {
setText("Stop Constant playing");
} else {
setText("Start Constant playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayConstButton(Context ctx) {
super(ctx);
setText("Start Constant playing");
setOnClickListener(clicker);
}
}
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
mFileNameConst = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileNameConst += "/constant.3gp";
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
mPlayButton = new PlayButton(this);
ll.addView(mPlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
mPlayConstButton = new PlayConstButton(this);
ll.addView(mPlayConstButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
更新:我有一点研发如下:我改变了 MediaRecorder 和 MediaPlayer 参数。对于每个值,我都记录了自己,在记录过程中,我开始了另一场比赛。然后完成了录制,听了我刚刚录制的内容。对于 MediaRecorder,我尝试了以下值:MediaRecorder.AudioSource.DEFAULT,MediaRecorder.AudioSource.MIC, MediaRecorder.AudioSource.VOICE_CALL,MediaRecorder.AudioSource.VOICE_COMMUNICATION,MediaRecorder.AudioSource.VOICE_DOWNLINK,MediaRecorder.AudioSource.VOICE_RECOGNITION,MediaRecorder.AudioSource.VOICE_UPLINK MediaPlayer,我尝试了这些值:AudioManager.MODE_NORMAL、AudioManager.MODE_CURRENT、AudioManager.MODE_IN_CALL、AudioManager.MODE_IN_COMMUNICATION、AudioManager.STREAM_VOICE_CALL、AudioManager.STREAM_MUSIC。但无论我尝试什么,我总是要么沉默,要么纯粹的噪音。我认为 MediaRecorder 和 MediaPlayer 类对于 VoIP 来说是不够的。而且Android的声音系统对于我这样的初学者来说有点奇怪。