0

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;
        }

    }

1 Answers1

0

Is the problem that the screen turns off which closes the activity? If this is the case, you should add android:immersive="true" to your <activity> declared in your manifest. This ensures that when the screen turns back on, you are brought back to your activity (see https://developers.google.com/glass/develop/gdk/immersions).

If there is a permission problem causing the activity to force close, you should be able to see a message printed out in logcat.

Also make sure that getMinBufferSize is not returning ERROR_BAD_VALUE. If ERROR_BAD_VALUE is returned, then you are trying to allocate an array with a negative size which would cause a crash. logcat should also be able to indicate if this is the issue.

Brandon Wuest
  • 206
  • 1
  • 2