2

I developed a game for android that uses Audio Record to get the mic input.

You can have a glance at https://play.google.com/store/apps/details?id=fixappmedia.micro

The thing is that I'm using the following function to get the sample rates available on the phone:

public int getValidSampleRates() {
            int r=8000;
            for (int rate : new int[] {8000,11025,16000,22050,44100}) {  // add the rates you wish to check against
                int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
                if (bufferSize > 0) {
                    r= rate;
                }
            }
            return r;
        }

I tested it initially on my phone (Samsung Galaxy Vibrant) and it worked pretty well... but today I tested it on a Samsung Galaxy Ace and the sample rate didn't work...

Any ideas on why?

jpaguerre
  • 1,130
  • 1
  • 13
  • 20
  • when I call recorder = new AudioRecord (MediaRecorder.AudioSource.MIC,rate,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT,bufferSize); using the rate given by the previous function, it throws an error... I don't have exactly what error here... but the thing is that it doesn't record anything... – jpaguerre Jan 29 '14 at 02:40
  • @progjose you should look at the Logcat for more info on your error. It's important to know if the recorder instance is 'null' (it means you're using an unsupported sample rate or channel) or if the recorder instance is internally unitialized (and you code should throw an IllegalStateException when calling startRecording()..). Once you'll know the exception that makes your app crash, you'll have different solutions. to adopt. – andrea.rinaldi Sep 04 '15 at 08:06

2 Answers2

1

I'm the developer of voice recording app (Hi-Q MP3 Voice Recorder), and I found out that all phones supports either 44100 Hz, 48000 Hz, or both.

Looking at your code, you missed out 48000.

Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
  • thank you, I'll try with that sample rate. I hope it solves it. Cheers! – jpaguerre Jan 29 '14 at 02:54
  • Hi @yuku. I'm trying to make AudioRecord work on a Nexus 5 and I've got a problem: the AudioRecord gets instantiated but it's in a wrong internal state, in fact an IllegalStateException's thrown when I call startRecording() on it. Any suggestion? – andrea.rinaldi Sep 04 '15 at 08:01
1

from the docs http://developer.android.com/reference/android/media/AudioRecord.html

the sample rate expressed in Hertz. 44100Hz is currently the only rate that is guaranteed to work on all devices, but other rates such as 22050, 16000, and 11025 may work on some devices.

Also noticed you are using AudioFormat.CHANNEL_CONFIGURATION_MONO which is deprecated according to the docs http://developer.android.com/reference/android/media/AudioFormat.html

This constant was deprecated in API level 5. use CHANNEL_OUT_MONO or CHANNEL_IN_MONO instead

Good luck

mnaa
  • 416
  • 4
  • 12