Skip to content

Instantly share code, notes, and snippets.

@muojp
Last active December 28, 2015 06:59
Show Gist options
  • Save muojp/7460868 to your computer and use it in GitHub Desktop.
Save muojp/7460868 to your computer and use it in GitHub Desktop.
final AudioTrack track = new AudioTrack(
AudioManager.STREAM_MUSIC, AUDIO_SAMPLING_RATE,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_DEFAULT, AUDIO_TOTAL_BYTES,
AudioTrack.MODE_STREAM);
track.setPlaybackPositionUpdateListener(
new AudioTrack.OnPlaybackPositionUpdateListener() {
@Override
public void onMarkerReached(AudioTrack track) {
track.stop();
}
@Override
public void onPeriodicNotification(AudioTrack track) {
}
});
track.setNotificationMarkerPosition(AUDIO_TOTAL_BYTES / 2);
track.write(bufToFill, 0, AUDIO_TOTAL_BYTES);
final long startedNanoTime = System.nanoTime();
track.play();
final AudioTimestamp timestamp = new AudioTimestamp();
final Runnable uiLoop = new Runnable() {
@Override
public void run() {
boolean isTimestampAvailable = track.getTimestamp(timestamp);
if (track.getPlayState() == AudioTrack.PLAYSTATE_STOPPED) {
uiUpdater.removeCallbacks(this);
jitterView.setText(jitterView.getText() + "\n");
Log.d(TAG, jitterView.getText().toString());
return;
}
if (isTimestampAvailable) {
long jitterMs = (timestamp.nanoTime - System.nanoTime()) / 1000000;
jitterView.setText(jitterView.getText() + String.format("%1$,d, ", jitterMs));
msgView.setText(
String.format("time=%1$,13d ns, pos=%2$d(pbhead=%3$d)",
timestamp.nanoTime - startedNanoTime, timestamp.framePosition,
track.getPlaybackHeadPosition()));
}
else {
msgView.setText("AudioTrack#getTimestamp is not available");
}
rootView.invalidate();
uiUpdater.removeCallbacks(this);
uiUpdater.postDelayed(this, 16);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment