我正在使用AudioRecord
来自https://github.com/nonameentername/soundtouch-android/blob/master/src/org/tecunhuman/ExtAudioRecorder.java的类,但稍作修改。
在方法中,我开始与班级start()
一起录制。AudioRecord
我也开始MediaPlayer
演奏器乐。为了让人声和乐器在服务器上的正确位置混合,我发送乐器延迟以在开始时用静音填充乐器(beatDelayInSeconds -MediaPlayer
从调用开始到实际播放的延迟,因为当有延迟时启动媒体播放器)。
我的逻辑一定是错误的,因为人声和乐器的播放时间在设备上是关闭的。与演奏乐器时相比,有时录音会太快,有时会太慢。有什么建议么。任何帮助将不胜感激。
为了更好的解释:
录音开始
|----------------------------------------|
Beat/Instrumental 之后开始播放
[X]|--------------------------------------------- -|
我正在尝试查找 X,以便可以将其作为 beatDelayInSeconds 发送到 startPlayback() 方法并发送到服务器。X 是从声音开始录制到节拍实际开始播放之间的时间。目前在我的 startPlayback() 方法中,同时播放节拍和人声,它们不像录制时那样同步。希望这是有道理的。
注意:我不得不为一些 Jelly Bean 设备放置延迟处理程序,因为AudioRecord
没有调用监听器。
public void start()
{
if (state == State.READY)
{
if (rUncompressed)
{
payloadSize = 0;
RecordReadDelayInSeconds = 0;
RecordDelayInSeconds = 0;
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mp3Path);
mPlayer.prepare();
} catch (IllegalArgumentException e) {e.printStackTrace();}
catch (SecurityException e) {e.printStackTrace();}
catch (IllegalStateException e) {e.printStackTrace();}
catch (IOException e) { e.printStackTrace();}
final long recordstarted = System.nanoTime() + 1500000000; //handler delay
audioRecorder.startRecording();
//Fix for recording issue with Samsung s3 Sprint phones.Doing a delayed first read
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
long recordstopped = System.nanoTime();
long recordDelay = recordstopped - recordstarted;
double RecordDelayInSeconds = recordDelay / 1000000.0;
Log.i("StartRecording() Delay in seconds",
String.valueOf(RecordDelayInSeconds));
long recordreadstarted = System.nanoTime();
int bytesReceived = audioRecorder.read(buffer, 0, buffer.length);
Log.d(TAG,"Delayed first read: bytes recieved "+ bytesReceived);
long recordreadstopped = System.nanoTime();
long recordreadDelay = recordreadstopped - recordreadstarted;
RecordReadDelayInSeconds = recordreadDelay / 1000000.0;
Log.i("Record read() Delay in seconds",
String.valueOf(RecordReadDelayInSeconds));
long mediastarted = System.nanoTime();
mPlayer.start();
long mediastopped = System.nanoTime();
long beatDelay = mediastopped - mediastarted;
beatDelayInSeconds = 0;
beatDelayInSeconds = (beatDelay) / 1000000000.0;
Log.i("Beat Delay in seconds",
String.valueOf(beatDelayInSeconds));
}
}, 1500);
}
这是用户在向服务器发送信息之前收听的播放代码。它首先开始播放用户刚刚录制的 wav,然后根据其他方法的 beatDelayInSeconds 变量播放器乐。
private void startPlayback() {
wavPlayer.start();
RefreshHandler mp3PlaybackHandler = new RefreshHandler();
mp3PlaybackHandler.sleep((long) (beatDelayInSeconds * 1000));
}
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
mp3Player.start();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
Log.i(TAG, "Beat delayed for " + delayMillis + " milliseconds");
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};