我正在尝试制作一个可以在不关闭屏幕的情况下继续录制的视频录制应用程序。我正在使用Android服务,如果我按下主屏幕按钮并再次进入该应用程序以停止录制,该应用程序仍会录制视频。但是一旦我关闭屏幕,应用程序就会停止录制,并且媒体文件会损坏。我应该怎么做才能使其正常工作?
我已经尝试了平台上的所有可能解决方案,但仍然不知道如何在锁定屏幕后使视频保持录制状态。但是,当我通过“主页”按钮退出应用程序时,该应用程序可以正常工作并保持录制状态。
这是从我的MainActivity类启动和停止服务的代码:
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(! isRecording){
Intent intent = new Intent(MainActivity.this, CameraService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
isRecording = true;
} else {
Intent intent = new Intent(MainActivity.this, CameraService.class);
stopService(intent);
isRecording = false;
}
}
});
这是CameraService类中处理记录的代码:
@Override
public void onCreate() {
super.onCreate();
mPreview = MainActivity.mPreview;
captureButton = MainActivity.captureButton;
mCamera = MainActivity.mCamera;
//initialize camera
if(prepareVideoRecorder()){
//Camera available and unlocked, media recorder prepared, start recording
mediaRecorder.start();
//inform user recording start
captureButton.setText("Stop");
Log.v("", "In onCreate(), isRecording is " + isRecording);
} else{
//prepare didn't work
releaseMediaRecorder();
Toast.makeText(this, "Cannot prepare video", Toast.LENGTH_LONG).show();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
//stop rec and release camera
mediaRecorder.stop();
releaseMediaRecorder();
mCamera.lock();
//inform user recording has stopped
captureButton.setText("Capture");
Log.v("", "In onDestroy(), isRecording is " + isRecording);
}
这是我在录制时关闭屏幕后立即出现在调试控制台中的内容:
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@e29724c nm : com.example.mycamera ic=null
I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: fd=73
Input channel destroyed: fd=72
D/Graph: removeVertex() : insertDummyVertex, because there is no ancestor.
D/ViewRootImpl@750598e[MainActivity]: dispatchDetachedFromWindow
D/InputTransport: Input channel destroyed: fd=64
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
I/art: Do partial code cache collection, code=26KB, data=29KB
After code cache collection, code=26KB, data=29KB
Increasing code cache capacity to 128KB
D/TextView: setTypeface with style : 0
D/InputTransport: Input channel constructed: fd=68
D/ViewRootImpl@8ad5cfe[MainActivity]: setView = DecorView@e9a2c5f[MainActivity] touchMode=true
I/Choreographer: Skipped 73 frames! The application may be doing too much work on its main thread.
D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [1440x2560]-format:1
W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
E/Camera: Error 2
D/InputTransport: Input channel destroyed: fd=73
W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
E/ViewRootImpl: sendUserActionEvent() mView == null
I/Choreographer: Skipped 61 frames! The application may be doing too much work on its main thread.
D/ViewRootImpl@8ad5cfe[MainActivity]: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1