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. Why not build an installer for android on WM phones?

I was wondering.. I've been following the android development for
quite a time now.. I am certainly not an developer but why doesn't the
android group bring out an application with wich u can simpel install
android over your current windows mobile..

I know that there are lot's of things that may give error like unkown
hardware and stuff.. But if there is an application made that you can
run on your pc (win,mac,linux) witch can detect what sort of hardware
your phone has and if its capitble of running android (1.5) then it
would over you to install it from your desktop..

And the software alsof should backup the original windows mobile rom
file..

I was suprised they didn't do this already because it wouild be a
great change for Android to really steal some of the windows mobile
market..

Is there any changes such an idea is possible?

(sorry for my rubbish English)

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

2. How To Load Previous Activity

Hello,

How can we load previous activity.
My requirement is

I clicked "Home" from my current Activity.Then i try to get my
application again by clicking icon from application menu. But i am
getting new instance, i am not able to get my previous instance.
Is there any way to get the previous instance from stack. Do we need
to use semaphore to avoid multiple instance.
Can anybody guide me?

Thank you


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

3. Android and OpenJDK

4. NotePadv1 Example!!

5. Switching Between Multiple Views

6. How to config G1 to make both browser and MMS work?

7. How to get filter text from a ListView?