AudioTrack以stream模式MODE_STREAMING

我需要在运行时产生PCM数据。 所以我有一个循环的线程

public void run() { while(...) { mAudioTrack.write(getPCM(), ...); } } 

不幸的是,这是行不通的。 它似乎不依赖于AudioTrack缓冲区大小。 我希望它非常小,以模拟低延迟行为(150毫秒),所以用户可以dinamically改变由getPCM()拾取的PCM

 int bufferSize = 0.150 * sampleRate * channels * bitsPerSample / 8; 

但是,我试图将缓冲区大小增加到100k,没有任何结果

Solutions Collecting From Web of "AudioTrack以stream模式MODE_STREAMING"

这里是简短的例子,适用于我:

 public class Internal extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onPlayClicked(View v) { start(); } public void onStopClicked(View v) { stop(); } boolean m_stop = false; AudioTrack m_audioTrack; Thread m_noiseThread; Runnable m_noiseGenerator = new Runnable() { public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); /* 8000 bytes per second, 1000 bytes = 125 ms */ byte [] noiseData = new byte[1000]; Random rnd = new Random(); while(!m_stop) { rnd.nextBytes(noiseData); m_audioTrack.write(noiseData, 0, noiseData.length); } } }; void start() { m_stop = false; /* 8000 bytes per second*/ m_audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT, 8000 /* 1 second buffer */, AudioTrack.MODE_STREAM); m_audioTrack.play(); m_noiseThread = new Thread(m_noiseGenerator); m_noiseThread.start(); } void stop() { m_stop = true; m_audioTrack.stop(); } }