【发布时间】:2012-08-13 04:13:42
【问题描述】:
我试图以 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 秒,无论缓冲区有多大。
【问题讨论】:
-
检查 AudioTrack.write() 的返回值,确保整个缓冲区都被写入。
-
好点,我相应地更新了问题。我仍然不确定为什么它只写了一半的缓冲区。
标签: java android audio audiotrack