How can i stop the thread once it starts ?
Code:
Code:
B4X:
#Region Project Attributes
#ApplicationLabel: StariSound_thr
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim nativeMe As JavaObject
Dim thread1 As Thread
Dim Lock1 As Lock
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private EditText1 As EditText
Private EditText2 As EditText
Private Button1 As Button
Private Button2 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("main")
If FirstTime Then
thread1.Initialise ("Thread1")
End If
nativeMe.InitializeContext
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Thread1_Ended(fail As Boolean, err As String) 'An error or Exception has occurred in the Thread
Log("Thread1 ended")
End Sub
Sub thread_Sub1
Dim freq As Double 'frequency to play
Dim dur As Double 'play for 5 seconds
freq = EditText1.Text
dur = EditText2.text
nativeMe.RunMethod("playTone",Array(freq,dur)) 'start the java code - it will run in the new thread and not in the main thread
End Sub
Sub Button1_Click
Log("Button1_Click")
Dim args(0) As Object
Lock1.Initialize(True)
thread1.Name = "B4A Thread 1"
thread1.Start(Null, "Thread_Sub1", args) 'start a new thread to run the java code in
End Sub
Sub Button2_Click
'Stop pulses ??? How ???
Log("Reacting to button 2 while java code thread is running")
End Sub
#If Java
import android.media.AudioTrack; //you need to add this as it is not part of the code posted on Stackoverflow - compiler will complain if it can't find a reference to AudioTrack so search for the correct way to import it on the web
import android.media.AudioFormat; //you need to add this as it is not part of the code posted on Stackoverflow - see comment above
import android.media.AudioManager; //you need to add this as it is not part of the code posted on Stackoverflow - see comment above
public void playTone(double freqOfTone, double duration) {
//double duration = 1000; // seconds
// double freqOfTone = 1000; // hz
int sampleRate = 8000; // a number
double dnumSamples = duration * sampleRate;
dnumSamples = Math.ceil(dnumSamples);
int numSamples = (int) dnumSamples;
double sample[] = new double[numSamples];
byte generatedSnd[] = new byte[2 * numSamples];
AudioTrack audioTrack = null; // Get audio track
if (freqOfTone == 0) {
//audioTrack.release();
audioTrack = null;
return;
}
for (int i = 0; i < numSamples; ++i) { // Fill the sample array
sample[i] = Math.sin(freqOfTone * 2 * Math.PI * i / (sampleRate));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalized.
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
int i = 0 ;
int ramp = numSamples / 20 ; // Amplitude ramp as a percent of sample count
for (i = 0; i< ramp; ++i) { // Ramp amplitude up (to avoid clicks)
double dVal = sample[i];
// Ramp up to maximum
final short val = (short) ((dVal * 32767 * i/ramp));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
for (i = i; i< numSamples - ramp; ++i) { // Max amplitude for most of the samples
double dVal = sample[i];
// 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);
}
for (i = i; i< numSamples; ++i) { // Ramp amplitude down
double dVal = sample[i];
// Ramp down to zero
final short val = (short) ((dVal * 32767 * (numSamples-i)/ramp ));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
try {
int bufferSize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize,
AudioTrack.MODE_STREAM);
audioTrack.play(); // Play the track
audioTrack.write(generatedSnd, 0, generatedSnd.length); // Load the track
}
catch (Exception e){
}
if (audioTrack != null) audioTrack.release(); // Track play done. Release track.
}
#End If