I have the following functions:
void playSound(){
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
Button Click:
public void btnTx_click(View view){
textView.setText("CLICKED1");
int rem;
while(dataToSend != 0){
rem = dataToSend % 2;
if(rem==0)
genTone(0.0);
else
genTone(1.0);
dataToSend /= 2;
counter++;
}
// Use a new tread as this can take a while
final Thread thread = new Thread(new Runnable() {
public void run() {
// genTone(); // this line is left here for my recor donly
handler.post(new Runnable() {
public void run() {
playSound();
}
});
}
});
thread.start();
}
The while
loop in the btnTx_click()
function prepares the generatedSnd
array used in the playSound()
function. When a button is pressed, this program loads audio samples into the generatedSnd
array and then plays them using an AudioTrack. The loading and playing are done in a thread.
How do I know when this thread ends? Does it end after the playing of AudioTrack ends? Or does it end immediately after public void run()
exits? Is it possible to check exactly when the thread ends, and if yes, where in MainActivity.java should such a check be included?
What I want to do is, once the AudioTrack completes playing of generatedSnd
, I want to load a new set of values in generatedSnd
and start playing this new tone. In effect, I want to play Tone 1, followed by Tone 2, followed by Tone 3,.... and so on.