Tuesday, April 2, 2013

Get Android AudioRecord startRecording time delay


I am using the AudioRecord class from https://github.com/nonameentername/soundtouch-android/blob/master/src/org/tecunhuman/ExtAudioRecorder.java but with it being slightly modified.


In the start() method, I start recording with the AudioRecord class. I also start a MediaPlayer to play a instrumental. To get these to sync at the server, I send the record delay (delay before AudioRecord actually starts recording and reads) and the instrumental delay (MediaPlayer delay from when start is called to it actually being played since there is latency).


My logic must be wrong because the timing is always off and I am unsure why. Any suggestions. Does audiorecording actually start when you call .startRecording or does it start after the first read? Any help would be greatly appreciated.


Note: I had to put the delay handler in there for some Jelly Bean devices because the AudioRecord listener was not being called.



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);
}


.

forums.androidcentral.com

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...