Android的AudioRecord - 不会初始化第二次 [英] Android AudioRecord - Won't Initialize 2nd time
问题描述
HEJ,即时通讯目前正试图获得AudioRecord工作。因为我需要在一个更大的项目。但似乎惹了不少。 我一直在尝试了很多事情,所以我又回到基本的,当我跟踪这个bug。 我用我的三星Galaxy S作为我debugdevice。
Hej, im currently trying to get AudioRecord to work. Because I need it in a bigger project. But it seems to mess up a lot. I have been trying alot of things, so I went back to basic when I traced this bug. I am using my Samsung Galaxy S as my debugdevice.
我的问题是,第一次我的设备重新启动后,我可以初始化我创建一个没有问题的AudioRecord对象。 但我第二次运行它,它也不会初始化AudioRecord对象。 我尝试了好几种频率,仅供参考。
My problem is, first time after a reboot of my device I can initialize the AudioRecord object I create without problems. But the second time I run it, it won't initialize the AudioRecord object. I have tried several frequencies, fyi.
下面是我的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);
}
}
几个小时的寻找通过logcat的信息后,我发现这个事件
After a few hours of looking through 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
这似乎为释放的AudioRecord本地举行。 所以我试图做最后确定我Audiorecord object.release的)一个覆盖(。这没有工作,但..任何人有什么想法?
Which seems to "release the native hold on the AudioRecord. So i tried doing an override of finalize with my Audiorecord object.release(). This didnt work though.. Anyone have any idea?
推荐答案
我能够重现您的问题(在三星手机)。我增加了一个的onDestroy()方法释放记录:
I was able to reproduce your problem (on a Samsung phone). I added an onDestroy() method releasing the record:
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("OnDestroy");
audRec.release();
}
添加此之后,audioRecord似乎每个活动启动时正确初始化。
After adding this, the audioRecord seems to initialize correctly every time the activity is started.
这篇关于Android的AudioRecord - 不会初始化第二次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!