Android AudioRecord - will not initialize a second time

Hej, im currently trying to get AudioRecord to work. Because I need it in a larger project. But it looks like a lot. I tried many things, so I returned to the main thing when I traced this error. I am using my Samsung Galaxy S as my debugdevice.

My problem is that the first time I reboot my device, I can initialize the AudioRecord object that I created without problems. But the second time I ran it, it does not initialize the AudioRecord object. I tried a few frequencies, fyi.

Here is my code:

package android.audiorecordtest; import android.app.Activity; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class AudioRecordTest extends Activity { int frequency; AudioRecord audRec; TextView txtVw; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtVw = (TextView) findViewById(R.id.txtVw); frequency=8000; int bufferSize=(AudioRecord.getMinBufferSize(frequency, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT))*2; if (bufferSize>0) { audRec = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize); int status = audRec.getState(); if (status == AudioRecord.STATE_INITIALIZED) { txtVw.setText("Initialized" + frequency); } else { txtVw.setText("Not Initialized i=" + frequency); } } 

After hours of viewing logcat information, I found this event

  02-28 10:46:37.048: DEBUG/dalvikvm(4477): GC_EXPLICIT freed 1801 objects / 98944 bytes in 97ms 02-28 10:46:37.048: VERBOSE/AudioRecord(4477): stop 

It seems that β€œrelease the native hold on AudioRecord. So I tried to do a redefinition of completion with my Audiorecord object. Release (). That didn't work though .. Does anyone have an idea?

+13
android audio alsa android-audiorecord
Feb 28 '11 at 8:41
source share
5 answers

I was able to reproduce your problem (on a Samsung phone). I added the onDestroy () method to free the entry:

 @Override public void onDestroy() { super.onDestroy(); System.out.println("OnDestroy"); audRec.release(); } 

After adding this sound, the sound recording seems to be initialized correctly every time it starts.

+10
Apr 13 2018-11-11T00:
source share
β€” -

I had the same problem, usually the audRec.release () function is valid, but if you need to stop and start several times, the following code is more reliable. In addition, I had a problem with the fact that the recording took place in a separate stream, and Android sometimes kills streams when working for a long time. Therefore, take a look at this code, it guarantees that the record will be saved even when another thread is dead, and after the next audRec.start () it will stop and free:

 import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; public class RecorderSingleton { private static final int FREQUENCY = 16000; public static RecorderSingleton instance = new RecorderSingleton(); private AudioRecord recordInstance = null; private int bufferSize; private RecorderSingleton() { bufferSize = AudioRecord.getMinBufferSize(FREQUENCY, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); } public boolean init() { recordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, FREQUENCY, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize); if (recordInstance.getState() == AudioRecord.STATE_UNINITIALIZED) { return false; } return true; } public int getBufferSize() { return bufferSize; } public boolean start() { if (recordInstance != null && recordInstance.getState() != AudioRecord.STATE_UNINITIALIZED) { if (recordInstance.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) { recordInstance.stop(); } recordInstance.release(); } if (!init()) { return false; } recordInstance.startRecording(); return true; } public int read(short[] tempBuffer) { if (recordInstance == null) { return AudioRecord.ERROR_INVALID_OPERATION; } int ret = recordInstance.read(tempBuffer, 0, bufferSize); return ret; } public void stop() { if (recordInstance == null) { return; } recordInstance.stop(); recordInstance.release(); } } 

Then, if you have a recorder stream, you can use it as follows:

 import android.media.AudioRecord; public class Recorder implements Runnable { private int requiredSamples; private int takenSamples = 0; private boolean cancelled = false; public void run() { // We're important... android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); int bufferRead = 0; int bufferSize = RecorderSingleton.instance.getBufferSize(); short[] tempBuffer = new short[bufferSize]; if (!RecorderSingleton.instance.start()) { return; } try { Log.d(RoomieConstants.LOG_TAG, "Recorder Started"); while (takenSamples < requiredSamples && !cancelled) { bufferRead = RecorderSingleton.instance.read(tempBuffer); if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) { throw new IllegalStateException("read() returned AudioRecord.ERROR_INVALID_OPERATION"); } else if (bufferRead == AudioRecord.ERROR_BAD_VALUE) { throw new IllegalStateException("read() returned AudioRecord.ERROR_BAD_VALUE"); } takenSamples += bufferRead; // do something with the samples ... // ... // ... } } finally { // Close resources... stop(); } } public void stop() { RecorderSingleton.instance.stop(); } public void cancel() { cancelled = true; } } 
+5
Jul 25 2018-11-11T00:
source share

To answer my own question, the only way I found it possible to use AudioRecord is to never have it as a global variable, I don’t know why, but it does not seem to allow you to release instance resources correctly if you do this.

+1
Mar 07 2018-11-11T00:
source share

You should try calling audRec.stop () to free the resource.

0
Mar 02 2018-11-21T00:
source share

My AudioRecord is not initialized because it was static

0
July 24. '19 at 10:15
source share



All Articles