Correct way of making a simple "talking parrot" app

by Marko 艩tamcar » Wed, 30 Jun 2010 02:51:36 GMT


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

--



Other Threads

1. Possible to send object via intent extras?

Hi,

Is it possible to send an object via intent extras?

I would like to send a pipeoutputstream to another APK (project via
extra).

Can I save an object to a bundle somehow?

Regards,
Oskar
--~--~---------~--~----~------------~-------~--~----~

2. dblite file?

Hello,

Is it possible to bundle a read/only dblite file with data for the
application?

I mean I need a bunch of pre-compiled data and distribute it with my
app and sqlite could be great!

But I've found this line in the api docs "All databases, SQLite and
others, are stored on the device in /data/data/package_name/
databases." telling that it may not be possible.

Thanks!

--~--~---------~--~----~------------~-------~--~----~

3. magic kalau jadi modem...?

4. Intent between 2 APK (different projects)?

5. need to build custom calendar

6. View.removeCallbacks problem

7. Cannot get PhoneNumber on Real device but code works in Emulator