Well, after spending my free time for the last 5 days working on the AudioRecord class I´m here again to post my findings. But first let me make myself clear:
- What I´m about to write is the result of my tests and findings after having a lot of trouble to use the basic routines of the class. I found a lot of forums posts over the internet about the same problems so my idea here is to help people so they don´t face the same problems I´ve faced. - It may be beginner´s stuff, but a lot of people have faced or are facing the same problems. - If something here is wrong please don´t come here just to say it, share your knowledge with us and post not only what´s wrong but how to do it right. - I´ve done all tests on the EMULATOR provided by google. Also I´m using the sdk 2.1 (build 7) Ok, let´s go. Creating an AudioRecord object: When you create a new instance of AudioRecord there are two ways of finding out if it was created properly (the device can handle the audio parameters and resources were trully allocated to your object): by catching an exception or by verifying the return of the getState() method. - Handling the IllegalArgumentException : Well, tricky is what I can say. Exceptions will only be thrown if any of the parameters you have used are NOT acceptable by the CLASS (not if the device can´t handle it). For example, a sample rate = 12345 is not a standard value and then the class will throw an exception. If SR = 44100 is not supported by the DEVICE, the object will be created and no exception will be launched. * An interesting thing is that the official documentation says: "sampleRateInHz the sample rate expressed in Hertz. Examples of rates are (but not limited to) 44100, 22050 and 11025." If you try SR = 11025 the system will throw an exception as it considers this an illegal parameter. Strange, huh? - Then ok, you have created an instance of AudioRecord but you don´t know if the parameters you have set are available in an specific device. What you do? Verify AudioRecord.getState(), right? Ok then, it works. But if you find out your instance is not ok and then you have to create a new AudioRecord object you will be in trouble. Why? Even after running AudioRecord.release() the resources for the audio input you are interested in will keep being in use and you won´t be abble to recreate your object in a way it would work. To verify this create an instace of AudioRecord with parameters that are known not to work on the emulator (ex. SR = 44100). Then verify the errors on logcat. After that call release() and then try to create another instance of AudioRecorder with the parameters that work (sr = 8000, mono channel, pcm 16 bits) and check the first error line. Then how should you find out which parameters the device will allow you to use? Say with me: AudioRecord.getMinBufferSize() is my solution! Loop checking from the best combination of parameters to the worst. If it returns -2, you should try another combination, if it returns -1 the audio input source is already being in use or isn´t even available for the specific device. If it returns something > 0 then you´re ok and can proceed creating a new AudioRecord object. AudioRecord notification: YES, IT WORKS! Believe me. The trick is: the notification will only be called AFTER you call read() for the first time! And not only one time (Oo). After a periodic notification you need to read from the audio buffer again to receive another notification. A marker notification will be called only once: if you need it to be called again, set the marker again! I guess it covers the basics. Good luck and believe me, android is great! Gabriel Simões -- 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

