Reputation: 8384
I've read many posts about the problems of initialization of AudioRecorder so I used a function to test every possibility. Unfortunately, there is no working one. What should I check?
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.w("DEBUG", "Audio record");
// Capture mono data at 16kHz
int frequency = 44100;
int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
AudioRecord audioRecord=null;
// The minimal buffer size CANNOT be merely 20ms of data, it must be
// at least 1024 bytes in this case, this is most likely due to a MMIO
// hardware limit.
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
Log.w("DEBUG", "Buffer size: "+bufferSize);
try
{
// audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
// frequency, channelConfiguration,
// audioEncoding, bufferSize);
audioRecord=findAudioRecord();
short[] buffer = new short[bufferSize];
if(audioRecord==null || audioRecord.getState()!= AudioRecord.STATE_INITIALIZED)
{
Log.w("DEBUG", "Can't start");
//Log.w("DEBUG", "status: " + audioRecord.getState());
return;
}
audioRecord.startRecording();
// Blocking loop uses about 40% of the CPU to do this.
int sampleNumber = 0;
// We'll capture 3000 samples of 20ms each,
// giving us 60 seconds of audio data.
while (sampleNumber < 3000)
{
audioRecord.read(buffer, 0, 320);
// for (int i = 0; i < buffer.length; i++)
// {
// fileBuffer[i * 2] = (byte) (buffer[i] & (short) 0xFF);
// fileBuffer[i * 2 + 1] = (byte) (buffer[i] >> 8);
// }
sampleNumber++;
}
} catch (IllegalArgumentException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalStateException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
finally
{
if(audioRecord!=null && audioRecord.getState()==AudioRecord.STATE_INITIALIZED)
{
audioRecord.stop();
audioRecord.release();
}
}
}
private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
public AudioRecord findAudioRecord() {
for (int rate : mSampleRates) {
for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) {
for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
try {
Log.w("DEBUG", "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
+ channelConfig);
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
// check if we can instantiate and have a success
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
return recorder;
}
} catch (Exception e) {
Log.w("DEBUG", rate + "Exception, keep trying.",e);
}
}
}
}
return null;
}
Here is the output:
10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 3, channel: 16 10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 3, channel: 12 10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 2, channel: 16 10-06 22:18:33.867: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 2, channel: 12 10-06 22:18:33.875: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 3, channel: 16 10-06 22:18:33.875: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 3, channel: 12 10-06 22:18:33.875: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 2, channel: 16 10-06 22:18:33.906: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 2, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 3, channel: 16 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 3, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 2, channel: 16 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 2, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 3, channel: 16 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 3, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 2, channel: 16 10-06 22:18:33.937: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 2, channel: 12 10-06 22:18:33.937: WARN/DEBUG(26097): Can't start
I have the following in the manifest:
uses-sdk android:minSdkVersion="10" android:targetSdkVersion="16" uses-permission android:name="android.permission.RECORD_AUDIO"
My phone is a Galaxy Ace. What should I check?
Upvotes: 2
Views: 5178
Reputation: 8384
I restarted the phone and it works now. I guess, calling release()
in each situation (see the finally
block) is essential.
Upvotes: 2