AudioTrack没有声音产生音

时间:2011-09-17 13:34:35

标签: android byte media wav pcm

我正在尝试使用以下代码让Android的AudioTrack播放方波

public Synthesizer() {
    bufferSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
    player = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize,
            AudioTrack.MODE_STREAM);

}

public void writeSamples(byte[] samples) {
    fillBuffer(samples);
    player.write(buffer, 0, samples.length);
}

private void putBuffer(byte[] samples) {
    if (buffer.length < samples.length)
        buffer = new byte[samples.length];

    for (int i = 0; i < samples.length; i++)
        buffer[i] = samples[i];  

即使样本是负数,其他样本也是正数,以获得方波:

btnPlay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            int frequency = 44100;
            byte[] sample = new byte[frequency];
            for (int i = 0; i < frequency; i++) {
                if (i % 2 == 0) {
                    sample[i] = Byte.MAX_VALUE;
                }

                else {
                    sample[i] = Byte.MIN_VALUE;

                }
            }
            squareSynth.writeSamples(sample);  

不幸的是,我根本没有声音,我已经检查了我的音量,但甚至无法获得静音或一些噼啪声。我觉得这很奇怪。任何人都知道如何解决它?

0 个答案:

没有答案