Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
public class MainActivity extends Activity {
int duration=3;
int sampleRate=44100;
int numSample=duration*sampleRate;
double sample[]=new double[numSample];
double freq1=18000;
double freq2=19000;
byte generatedSnd[]= new byte[2*numSample];
Handler handler=new Handler();
private static String mFileName = null;
private static final String LOG_TAG = "GenerateNrecord";
MediaRecorder mRecorder=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onResume()
{
super.onResume();
Thread thread=new Thread(new Runnable()
{
public void run()
{
genTone();
handler.post(new Runnable()
{
public void run()
{
playSound();
handler.post(new Runnable()
{
public void run()
{
startRecording();
new Timer().schedule(new TimerTask()
{
@Override
public void run()
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{ stopRecording();
}
});
}
}, duration);
}
});
}
});
}
});
thread.start();
}
void genTone(){
double instfreq=0, numerator;
for (int i=0;i<numSample; i++ )
{
numerator=(double)(i)/numSample;
instfreq =freq1+(numerator*(freq2-freq1));
sample[i]=Math.sin(2*Math.PI*i/(sampleRate/instfreq));
}
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(){
AudioTrack audioTrack= null;
try{
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length, AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
catch(Exception e)
{
System.out.print(e);
}
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
private void stopRecording() {
mRecorder.stop();
mRecorder.reset();
mRecorder.release();
mRecorder = null;
}
}