使用该方法将活动发送到后台后,我尝试录制几秒钟的音频moveTaskToBack(true);。我创建了一个服务,在其中开始录制,并设置了我想要录制音频的时间。一旦它最小化,问题就来了。大约2秒后它就会停止录制,而我需要它录制大约一分钟。我想知道如果应用程序在后台运行,是否真的可以录制音频。
我的服务记录如下:
@Override
protected void onHandleIntent(Intent intent) {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setOutputFile(fake.getFilename());
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
recorder.start();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recorder.stop();
}
}, 6000000);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我的活动调用服务并像这样关闭:
public void mecierro() {
Intent intent = new Intent(Panic.this, Record_service.class);
Panic.this.startService(intent); //starting the service
start1 = new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
moveTaskToBack(true); //"minimizing" the app
}
}.start();
}
Run Code Online (Sandbox Code Playgroud)
是的,在后台录制音频是百分百可能的。使用服务来启动记录器。然后您启动此服务的活动使用该方法
startForeground()而不是startService()
这是因为您的服务在进一步记录之前就被终止了。除非用户强制,否则前台服务不太可能被杀死。
| 归档时间: |
|
| 查看次数: |
7114 次 |
| 最近记录: |