gpt4 book ai didi

java - 通过 AudioTrack 多次播放 WAV 文件

转载 作者:行者123 更新时间:2023-12-01 11:48:32 24 4
gpt4 key购买 nike

我正在制作一个节拍器应用程序,它使用 AudioTrack 为不同的乐器播放不同的声音。我想使用我在牛铃上敲击过的 .wav 文件作为其中一个选项。我可以让第一个牛铃响起来工作,但此后就不再播放了。

MainActivity.java

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

节拍器.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);
}

音频生成器.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();
}



}

}

最佳答案

我能够使用这个让它工作:

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();
}

}

关于java - 通过 AudioTrack 多次播放 WAV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28955003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com