At first I should appologize if code below looks horrible and not formatted correctly. I don't know much about programming, so I took pieces of programs from different sources and just put them together.
In app I'd like to display frequency (using zero crossing method) of sound from phone mic. When I running app it shows frequency once but doesn't updating it.
1) Can you please help me to find solution for displaying frequency values repeatedly in the same textbox (or in more appropriate widget on the screen)?
*2) Is it possible to make this app without buttons. I mean is that app shows frequency right after it starts?*SOLVED
Thank You!
package alar.alar.com.frequencyFreq;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int RECORDER_SAMPLERATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
public AudioRecord recorder = null;
int numCrossing, p;
short audioData[];
public int frequency;
public boolean recording;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
recorder.startRecording();
recording = true;
audioData = new short[bufferSize];
recorder.read(audioData, 0, bufferSize);
numCrossing = 0;
for (p = 0; p < bufferSize - 1; p++) {
if ((audioData[p] > 0 && audioData[p + 1] <= 0) ||
(audioData[p] < 0 && audioData[p + 1] >= 0)) {
numCrossing++;
}
}
frequency = (8000 / bufferSize) * (numCrossing / 2);
TextView textView = (TextView) this.findViewById(R.id.textView);
textView.setText(String.valueOf(frequency));
}//onCreate
}//activity
try this