gpt4 book ai didi

java - IntentService 的 onDestroy 方法在 onHandleIntent 之后立即被调用

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:47 25 4
gpt4 key购买 nike

我有一个 IntentService,用于通过应用程序中的 AudioRecord 录制音频。在 onHandleIntent 方法中,我调用用于处理录制的单独类的 .getInstance,然后调用该类的几个方法。

然而,一旦intentService启动,它的onDestroy方法就会被调用,并且记录会停止。在 onHandleIntent 中实例化的类中,音频仍在录制,直到调用 onDestroy 为止。因此,在录制完成之前,onDestroy 肯定不会被调用。

如果有人可以就为什么这样做提出建议,我们将不胜感激。

我的 IntentService 的代码如下:

public class RecordService extends IntentService {
public File directory;
AudioRecord audioRecord;
int sampleRate = 44100;
int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
File file;
short channels = 1;
int bitsPerSample = 16;
public RecordService() {
super("Record");
}
ExtAudioRecorder extAudioRecord;

@Override
protected void onHandleIntent(Intent intent) {

Log.v("Record", "Record called");
extAudioRecord = ExtAudioRecorder.getInstanse(false);
extAudioRecord.reset();
extAudioRecord.setOutputFile(getOutputFile("RecV2").toString());
extAudioRecord.prepare();

extAudioRecord.start();
//record();
}
public void onDestroy(){
extAudioRecord.stop();
//audioRecord.stop();
Log.v("Record", "onDestroy called, Record stopped");

}

最佳答案

我相信extAudioRecord.start();是非阻塞的,这意味着它会立即返回退出onHandleIntent()。 IntentService 在调用 onDestroy 后不久就会终止。

关于java - IntentService 的 onDestroy 方法在 onHandleIntent 之后立即被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26890235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com