我们有一个安卓设备,我们想在上面同时使用microphone
in 2应用程序。
实际上,我们有一个在后台运行的语音命令服务(我们使用的是CMU Sphinx
库)。问题是,当我们启动录像机(相机应用程序)时,我们无法启动录制,因为两个应用程序无法同时访问麦克风。
错误
08-20 12:20:14.601: I/MediaRecorderJNI(7261): prepare: surface=0x59590668
08-20 12:20:15.916: E/MediaRecorder(7261): start failed: -38
08-20 12:20:15.916: E/com.example.CamcorderView(7261): Failed to start recorder.
08-20 12:20:15.916: E/com.example.CamcorderView(7261): java.lang.IllegalStateException
08-20 12:20:15.916: E/com.example.CamcorderView(7261): at android.media.MediaRecorder.start(Native Method)
请注意,当语音服务关闭时,摄像头工作正常。
此外,我准确地说我已经读过这个帖子了:
Android: Accessing the microphone simultaniously (RecognizerIntent + own app)
但这里的不同之处在于,我们需要处理操作系统和内核。因此,如果需要,我们可以应用补丁。
这是SDK/OS/Kernel
的限制吗?有什么变通方法吗?
发布于 2015-06-08 22:40:23
这种情况会发生。
例如
当您想要记录电话呼叫时。您可以使用开源的呼叫记录器。请参阅this和this
下面是一个代码示例
private MediaRecorder recorder = null;
public void onCreate()
{
super.onCreate();
recorder = new MediaRecorder();
Log.i("CallRecorder", "onCreate created MediaRecorder object");
}
public void onStart(Intent intent, int startId) {
if (isRecording) return;
Context c = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
Boolean shouldRecord = prefs.getBoolean(Preferences.PREF_RECORD_CALLS, false);
if (!shouldRecord) {
Log.i("CallRecord", "RecordService::onStartCommand with PREF_RECORD_CALLS false, not recording");
return;
}
int audiosource = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_SOURCE, "1"));
int audioformat = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_FORMAT, "1"));
recording = makeOutputFile(prefs);
if (recording == null) {
recorder = null;
return; //return 0;
}
Log.i("CallRecorder", "RecordService will config MediaRecorder with audiosource: " + audiosource + " audioformat: " + audioformat);
try {
// These calls will throw exceptions unless you set the
// android.permission.RECORD_AUDIO permission for your app
recorder.reset();
recorder.setAudioSource(audiosource);
recorder.setOutputFormat(audioformat);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(recording.getAbsolutePath());
//recorder.setMaxDuration(msDuration); //1000); // 1 seconds
//recorder.setMaxFileSize(bytesMax); //1024*1024); // 1KB
recorder.setOnInfoListener(this);
recorder.setOnErrorListener(this);
try {
recorder.prepare();
} catch (java.io.IOException e) {
Log.e("CallRecorder", "RecordService::onStart() IOException attempting recorder.prepare()\n");
t.show();
recorder = null;
return;
}
Log.d("CallRecorder", "recorder.prepare() returned");
recorder.start();
isRecording = true;
Log.i("CallRecorder", "recorder.start() returned");
updateNotification(true);
} catch (java.lang.Exception e) {
Log.e("CallRecorder", "RecordService::onStart caught unexpected exception", e);
recorder = null;
}
return;
}
https://stackoverflow.com/questions/25402434
复制相似问题