我正在尝试
我的问题是第二个录制文件(2.)和第一个录制文件(1.)不同步。一旦我把它们混合在一起,我听到一个我没有记录的延迟。为了测试我的应用程序,我在麦克风中说“测试1 2 3”。在第二次录音中,我同时说“测试1 2 3”。但是在混合(覆盖)我的2个文件后,我得到了延迟。
我做错了什么?
final Thread recordingThread = new Thread(new Runnable() {
final int SAMPLING_RATE = 44100;
final int AUDIO_SOURCE = MediaRecorder.AudioSource.MIC;
final int CHANNEL_IN_CONFIG = AudioFormat.CHANNEL_IN_MONO;
final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
final int BUFFER_SIZE = AudioRecord.getMinBufferSize(SAMPLING_RATE, CHANNEL_IN_CONFIG, AUDIO_FORMAT);
final String AUDIO_RECORDING_FILE_NAME = project.getPath()+"/track"+String.valueOf(project.getTrackNumber())+".raw";
final int playbackBufferSize = AudioTrack.getMinBufferSize(SAMPLING_RATE, CHANNEL_IN_CONFIG, AUDIO_FORMAT);
@Override
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
Log.d("Record", "[Starting recording]");
byte audioData[] = new byte[BUFFER_SIZE];
boolean playSound = true;
ByteArrayOutputStream playbackOutput = new ByteArrayOutputStream();
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(project.getPath()+"/output.wav"));
} catch (FileNotFoundException e) {
playSound = false;
}
int playbackRead = 1;
byte[] playbackBuffer = new byte[BUFFER_SIZE];
AudioRecord recorder = new AudioRecord(AUDIO_SOURCE,
SAMPLING_RATE, CHANNEL_IN_CONFIG,
AUDIO_FORMAT, BUFFER_SIZE);
AudioTrack player = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLING_RATE, AudioFormat.CHANNEL_OUT_MONO, AUDIO_FORMAT, playbackBufferSize, AudioTrack.MODE_STREAM);
player.play();
recorder.startRecording();
String filePath = AUDIO_RECORDING_FILE_NAME;
BufferedOutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(filePath));
} catch (FileNotFoundException e) {
Log.e("Record", "File not found for recording ", e);
}
while (!mStop) {
int status = recorder.read(audioData, 0, audioData.length);
if (status == AudioRecord.ERROR_INVALID_OPERATION ||
status == AudioRecord.ERROR_BAD_VALUE) {
Log.e("Record", "Error reading audio data!");
return;
}
try {
os.write(audioData, 0, audioData.length);
if (playSound) {
try {
if (playbackRead > 0) {
playbackRead = in.read(playbackBuffer);
}
if (playbackRead > 0){
player.write(playbackBuffer, 0, playbackBuffer.length);
}
} catch (IOException e) {
Toast.makeText(MainActivity.this, "ERROR!", Toast.LENGTH_SHORT).show();
return;
}
}
} catch (IOException e) {
Log.e("Record", "Error saving recording ", e);
return;
}
}
try {
os.close();
recorder.stop();
recorder.release();
player.stop();
player.release();
Log.v("Record", "Recording done…");
mStop = false;
File out = new File(project.getPath()+"/output.wav");
if (!out.exists()) {
out.createNewFile();
SoundUtils.rawToWave(new File(AUDIO_RECORDING_FILE_NAME), out, SAMPLING_RATE);
} else {
mixSound(project.getPath()+"/track1.raw", project.getPath()+"/track2.raw", project.getPath()+"/track3.raw", project.getPath()+"/track4.raw");
}
} catch (IOException e) {
Log.e("Record", "Error when releasing", e);
}
}
});
说明:
( - 一旦没有什么可玩的话,playbackRead就是-1)
之后我尝试混合我的录音。然而,我的第二次录音有延迟,我没有录音。
我做错了什么?我如何(几乎)完全正确地同步AudioRecord和AudioTrack,以便当我说出它在背景录制的位置播放时,就像我录制它时一样?
答案 0 :(得分:2)
最后,我找到了一个非常好的解决方案,我希望将来与所有面临这个问题的人分享。我打印出我正在录制的pcm数据的每个isSelected()
值。它看起来像这样:
public boolean isSelected(int itemPosition) {
return selectedItemsPositions.contains(itemPosition);
}
正如您所看到的,录制开始时有很多零。但在正常的环境中,背景中始终存在(非常相当)噪音。它不能完全为0.
我想,也许,这个范围,所有的0都是从录音开始到第一个声音实际接收文件的时间。
=> 这是延迟
在第二次录音中(当前一次录音作为背景声音播放时)我从整个数据中删除了起始0,以便立即开始声音。然后我将两个录音混合在一起,它们非常精确同步。
我不知道这是否是一种如何在所有情况下消除延迟的方式,但我在几部手机上尝试过它并且效果很好。我很高兴能够解决我的问题。
长话短说:要删除延迟,请删除声音数据中前面的所有0。