Is the following code (especially the listen() function) a proper way
to do it? I'm not exactly happy with the sound quality I get from
it... thanks for your help/comments in advance!

Marko

------

public class Main extends Activity {

    private volatile Thread listener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listen();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopListen();
    }

    private void listen() {
        listener = new Thread() {
            @Override
            public void run() {
                Thread thisThread = Thread.currentThread();
 
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

                int bufferRead = 0;
                int bufferSize = AudioRecord.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
                short[] tempBuffer = new short[bufferSize];

                AudioRecord ar = new
AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
                ar.startRecording();

                AudioTrack at = new
AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize * 2,
AudioTrack.MODE_STREAM);
                at.play();

                while (true) {
                    if (listener != thisThread) {
                        ar.stop();
                        ar.release();
                        at.stop();
                        at.release();
                        break;
                    }
                    bufferRead = ar.read(tempBuffer, 0, bufferSize);
                    at.write(tempBuffer, 0, bufferRead);
                }

            }
        };
        listener.start();
    }

    private void stopListen() {
        listener = null;
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to