Android AudioRecord - 不会第二次初始化
Hej,我目前正在尝试让 AudioRecord 工作。因为我在一个更大的项目中需要它。但似乎乱了很多。 我一直在尝试很多事情,所以当我追踪这个错误时,我回到了基础。 我使用 Samsung Galaxy S 作为调试设备。
我的问题是,第一次重新启动设备后,我可以毫无问题地初始化我创建的 AudioRecord 对象。 但第二次运行它时,它不会初始化 AudioRecord 对象。 我已经尝试了几个频率,仅供参考。
这是我的代码:
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 信息几个小时后,我发现了这个事件
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() 覆盖 Finalize。但这不起作用......有人有任何想法吗?
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.
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.
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 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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我能够重现您的问题(在三星手机上)。我添加了一个释放记录的 onDestroy() 方法:
添加此方法后,audioRecord 似乎在每次活动启动时都正确初始化。
I was able to reproduce your problem (on a Samsung phone). I added an onDestroy() method releasing the record:
After adding this, the audioRecord seems to initialize correctly every time the activity is started.
我遇到了同样的问题,通常 audRec.release() 确实有帮助,但如果您需要多次停止和启动,下面的代码会更强大。另外,我遇到了一个问题,即录制是在单独的线程中进行的,Android 有时会在长时间运行时杀死线程。所以看一下这段代码,它确保即使另一个线程死了也能保留录音,并且在执行以下 audRec.start() 时它会停止并释放:
然后,如果您有一个录音机线程,您可以按如下方式使用它:
I had the same problem, usually the audRec.release() helps indeed, but if you need to stop and start several times the following code is more robust. Plus, I had an issue that the recording took place in a separate thread and Android sometimes kills threads when running for a long time. So take a look at this code, it makes sure the recording is held even when the other thread is dead and upon the following audRec.start() it stops and releases:
Then if you have a recorder thread you can use it as follows:
要回答我自己的问题,我发现可以使用 AudioRecord 的唯一方法是永远不要将其作为全局变量,不知道为什么,但如果这样做,它似乎不会让您正确释放实例的资源所以。
To Answer my own question, the only way i found it doable to use AudioRecord, is to never have it as an global variable, dont know why, but it seems it won't let you release the resources of the instance correctly if you do so.
您应该尝试调用 audRec.stop() 来释放资源。
You should try to call audRec.stop() to release the resource.
我的 AudioRecord 未初始化,因为它是静态的
My AudioRecord didn't initialize because it was static