I am a high school student currently working on an arduidroid project using potentiometers, bluetooth, and a guitar app. My issue is that when I play my guitar app that I get an error that won't crashes.
AudioFlinger could not create track, status: -12
Error -12 initializing AudioTrack
Error code -20 when initializing AudioTrack.
How do I make it so that I can play as many sounds as I want with 3 different buttons using a tone generator from this question.
void genTone(double freq){
// fill out the array
for (int i = 0; i < numSamples; ++i) {
sample[i]= 2*(i%(sampleRate/freq))/(sampleRate/freq)-1;
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}
void playSound(){
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
if(BString.isPressed()){
/*genTone(BStringFreq);
playSound();*/
final Thread thread = new Thread(new Runnable() {
public void run() {
genTone(BStringFreq);
handler.post(new Runnable() {
public void run() {
playSound();
}
});
}
});
thread.start();
}
}
Here are some snippets of my code. Please help me out here as I am lost on this subject.