通过AudioTrack多次播放WAV文件

时间:2015-03-10 02:08:09

标签: java android wav audiotrack

我正在制作一个节拍器应用程序,它使用AudioTrack为不同的乐器演奏不同的声音。我想使用一个.wav文件,我有一个打在一个牛铃上的一个选项。我可以让第一个牛铃打到上班,但之后它就不会播放了。

MainActivity.java

metronome.playInstrumental(getResources().openRawResource(R.raw.cowbell));

Metronome.java

public Metronome(Handler handler) {
    audioGenerator.createPlayer();
    this.mHandler = handler;
}

public void calcSilence() {
    silence = (int) (((60 / bpm) * 8000) - tick);
    soundTickArray = new double[this.tick];
    soundTockArray = new double[this.tick];
    silenceSoundArray = new double[this.silence];
    double[] tick = audioGenerator.getSineWave(this.tick, 8000, beatSound);
    double[] tock = audioGenerator.getSineWave(this.tick, 8000, electronicSound);
    for(int i = 0; i < this.tick; i++) {
        soundTickArray[i] = tick[i];
        soundTockArray[i] = tock[i];
    }
    for(int i = 0; i < silence; i++)
        silenceSoundArray[i] = 0;
}

public void playInstrumental(InputStream inputStream) {
    calcSilence();
    do {
        msg = new Message();
        msg.obj = ""+currentBeat;

        audioGenerator.playSound(inputStream);
        mHandler.sendMessage(msg);
        audioGenerator.writeSound(silenceSoundArray);
    } while(play);
}

AudioGenerator.java

public class AudioGenerator {

private int sampleRate;
private AudioTrack audioTrack;

public AudioGenerator(int sampleRate) {
    this.sampleRate = sampleRate;
}

public double[] getSineWave(int samples, int sampleRate, double frequencyOfTone){
    double[] sample = new double[samples];
    for (int i = 0; i < samples; i++) {
        sample[i] = Math.sin(2 * Math.PI * i / (sampleRate / frequencyOfTone));
    }
    return sample;
}

public byte[] get16BitPcm(double[] samples) {
    byte[] generatedSound = new byte[2 * samples.length];
    int index = 0;
    for (double sample : samples) {
        // scale to maximum amplitude
        short maxSample = (short) ((sample * Short.MAX_VALUE));
        // in 16 bit wav PCM, first byte is the low order byte
        generatedSound[index++] = (byte) (maxSample & 0x00ff);
        generatedSound[index++] = (byte) ((maxSample & 0xff00) >>> 8);
    }
    return generatedSound;
}

public void createPlayer(){
    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            sampleRate, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, sampleRate,
            AudioTrack.MODE_STREAM);

    audioTrack.play();
}

public void writeSound(double[] samples) {
    byte[] generatedSnd = get16BitPcm(samples);
    audioTrack.write(generatedSnd, 0, generatedSnd.length);
}

public void destroyAudioTrack() {
    audioTrack.stop();
    audioTrack.release();
}

public void playSound(InputStream inputStream) {
    int i = 0;
    int bufferSize = 512;
    byte [] buffer = new byte[bufferSize];
    try {
        while((i = inputStream.read(buffer)) != -1) {
            audioTrack.write(buffer, 0, i);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }



}

}

1 个答案:

答案 0 :(得分:1)

我能够使用它来实现它:

public void playSound(InputStream inputStream) {
    int bufferSize = 512;
    byte[] buffer = new byte[bufferSize];
    i = 0;
    try {
        while((i = inputStream.read(buffer)) > -1) {
            audioTrack.write(buffer, 0, i);
        }
        inputStream.reset();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
相关问题