我试图以 600hz 播放 10 秒的声音,但我得到 600hz 的时间不超过 3 秒。有人可以指出我做错了什么吗?
int minBuffersize = AudioTrack.getMinBufferSize(samplerate,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);
int size= (int) ((10f*samplerate)
/minBuffersize +1);
size*=minBuffersize;//round to the nearest minBuffersize multiple so I don't get an error
short[] play= new short[size];
for(int i =0;i<play.length;i++){
float time=(float)i/(float)samplerate;
play[i]=(short) (((float)Short.MAX_VALUE/10f)*Math.sin(
600f
*time
*(2f*Math.PI)));
}
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC,
samplerate,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,
play.length,
AudioTrack.MODE_STATIC);
int result=at.write(play,0,play.length);
Log.d("!!!", "result: "+result);
at.play();
调试行返回的位置
result: 221184
始终如一。
注意 play.length = 442368。
经过一些更多的实验,缓冲区总是 play.length 的一半,但声音本身只持续 3-5 秒,不管缓冲区有多大。