0
我目前正在嘗試創建一個Android應用程序,將麥克風從音頻循環到聽筒,我可以做到這一點,但是當我在應用程序中一遍又一遍地執行操作時,內存不足錯誤。音頻緩衝區上的java.lang.OutOfMemoryError
這裏是我用來創建音頻循環代碼:
static final int bufferSize = 200000;
final short[] buffer = new short[bufferSize];
short[] readBuffer = new short[bufferSize];
public void run() {
isRecording = true;
android.os.Process.setThreadPriority
(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
int buffersize = AudioRecord.getMinBufferSize(11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
MediaRecorder.AudioEncoder.AMR_NB,
buffersize);
atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
MediaRecorder.AudioEncoder.AMR_NB,
buffersize,
AudioTrack.MODE_STREAM);
Log.d("AUDIO", "sample rate = : " + arec.getSampleRate());
atrack.setPlaybackRate(11025);
byte[] buffer = new byte[buffersize];
arec.startRecording();
atrack.play();
while(isRecording) {
arec.read(buffer, 0, buffersize);
atrack.write(buffer, 0, buffer.length);
}
}
,這裏是錯誤,我得到:
ERROR/AndroidRuntime(3442): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(3442): java.lang.OutOfMemoryError
ERROR/AndroidRuntime(3442): at com.phone.engine.CallAudio$Record.<init>(CallAudio.java:114)
ERROR/AndroidRuntime(3442): at com.phone.engine.CallAudio.onCreate(CallAudio.java:42)
ERROR/AndroidRuntime(3442): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2465)
ERROR/AndroidRuntime(3442): at android.app.ActivityThread.access$2800(ActivityThread.java:112)
ERROR/AndroidRuntime(3442): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
ERROR/AndroidRuntime(3442): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(3442): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(3442): at android.app.ActivityThread.main(ActivityThread.java:3948)
ERROR/AndroidRuntime(3442): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(3442): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(3442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
ERROR/AndroidRuntime(3442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
ERROR/AndroidRuntime(3442): at dalvik.system.NativeStart.main(Native Method)
所以錯誤特別關注代碼
0123中的這一行short[] readBuffer = new short[bufferSize];
它發生在我嘗試做幾次循環後,所以說我啓動應用程序,並在10次循環開始後,我得到的錯誤。
所以我認爲緩衝區簡直是完全或東西?那是對的嗎?如果是這樣,我將如何清理緩衝區?
在此先感謝
乾杯亞倫,所以它很可能是填補了緩衝區? – 2010-03-04 12:25:53
沒有。這只是意味着進程永遠不會終止,舊的緩衝區仍然存在(與所有其他對象一起)。由於內存永遠不會返回到系統,您最終會用完它。 – 2010-03-04 12:45:12