我在使用AudioTrack播放连续的正弦波时遇到了麻烦,它可以工作,但是我得到了一个剪辑声音,基本上它在循环时音频流中有一个短暂的中断。我假设这里有一些容易做的事情,但我不知道它是什么。我尝试过更改loopLength,但它只更改剪辑的间隔。
这是我的代码:
public void playSound() {
AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
256, AudioTrack.MODE_STREAM);
int loopLength = 2 * this.mSampleRate;
while (isPlaying) {
double[] mSound = new double[loopLength];
for (int i = 0; i < loopLength; i = i + 1) {
mSound[i] = Math.sin((1.0 * Math.PI * i / (this.mSampleRate / pitch)));
mBuffer[i] = (short) (mSound[i] * Short.MAX_VALUE);
}
mAudioTrack.play();
mAudioTrack.write(mBuffer, 0, loopLength);
}
}