我正在为一个VoIP应用程序做一些声学回声消除实验,声音让我抓狂。我想做的很简单:我之前录过一个声音。现在我将播放该声音,并在播放时录制另一个声音,这是全双工VoIP场景的真实情况。我用MedaiPlayer播放声音,用MediaRecorder录制新声音。下面是类的完整代码,修改自Android SoundRecorder样本。重要的是,mPlayer.setAudioStreamType (AudioManager.MODE_IN_COMMUNICATION);Android的文档说"在通信音频模式下。"建立语音/视频聊天或VoIP通话mRecorder.setAudioSource (MediaRecorder.AudioSource.VOICE_COMMUNICATION);Android的文档说"麦克风音频源调谐语音通信,如VoIP。例如,如果可用,它将利用回声消除或自动增益控制。如果没有应用语音处理,它的行为就像默认一样。"这是很有希望的。但如果我播放扬声器发出的任何声音,我几乎什么也录不到,或者只有奇怪的声音。由于我是新用户,我无法附加录音的声波,但第一个是正常的录音,有正常的声波。第二次录音是播放第一次录音时的录音,几乎什么都没有,没有声波。如果扬声器中有任何活动,Android似乎会关闭麦克风。我尝试了MediaRecorder和MediaPlayer的不同可能性,但没有用。在Android中实现声学回声消除的正确方法是什么?我尝试在索尼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。麦克风、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的音响系统有点奇怪。
我部分明白了。我使用的是安装了Android 3.2系统的索尼Tablet S。我在安装了Android 2.3的Archos 70上尝试了同样的程序。当我一边录制一边播放另一个东西时,两种声音都会被记录下来。这意味着我有一个声音可以应用AEC。在索尼,录音只是噪音。现在有两种可能性:要么索尼Tablet S有麦克风实现的问题(顺便说一下,GTalk在索尼上工作得很好,但它可以在Android上实现的特殊的东西),要么Android 3.2有麦克风实现的问题。
Android中的Echo取消功能在很多情况下都不是很有效。我认为这与长回声尾有关。我知道有第三方算法可以解决这个问题。谷歌回声消除软件,并验证该软件是否支持长回声尾
为什么在流媒体应用程序中使用MediaRecorder
?使用AudioRecorder
代替。
我目前正在玩一个类似的用例,AudioRecorder
工作完美无瑕。