I am developing an app for google glass, wherein I wish to record and audio and play it with a certain delay. For this, I am using AudioRecord class.
The problem is that as soon as the app is opened on the glass, it goes to the layout page and exits itself. I don't seem to understand the issue.
Are there any specific permissions that have to be added for audio capture and play, in the Android Manifest ??
The following is my code:
package com.example.audio;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class ha2 extends Activity{
private static final String TAG = "MainActivity";
private AsyncTask lpt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.activity_main_audio);
if (null == lpt || lpt.isCancelled()) {
lpt = new ListenPlayTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if(lpt != null)
lpt.cancel(true);
Log.d(TAG, "onDestroy");
super.onDestroy();
}
private class ListenPlayTask extends AsyncTask{
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
Log.d(TAG,"ListenPlayTask - doInBackground");
boolean isRecording=true;
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
int bufferSize = AudioRecord.getMinBufferSize(11025, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC,11025,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,bufferSize);
AudioTrack atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,11025,AudioFormat.CHANNEL_OUT_MONO,AudioFormat.ENCODING_PCM_16BIT,bufferSize,AudioTrack.MODE_STATIC);
atrack.setPlaybackRate(11025);
byte[] buffer = new byte[bufferSize];
arec.startRecording();
atrack.setStereoVolume(AudioTrack.getMaxVolume(), AudioTrack.getMaxVolume());
atrack.play();
while(isRecording){
arec.read(buffer,0,bufferSize);
atrack.write(buffer, 0, buffer.length);
if( isCancelled())
break;
}
Log.d(TAG,"Task Cancelled");
arec.stop();
atrack.stop();
return null;
}
}