当我使用Surface视图和服务在我的Android应用程序中启动后台视频录制时,它正在捕获视频。当我尝试打开该视频时,视频将强行关闭。为此我写了这样的代码。当我在索尼手机上运行此应用程序时,呼叫接收功能无效。如果有人有想法,请与我分享。
public class VideoRecorderService extends Service implements SurfaceHolder.Callback {
private WindowManager windowManager;
private SurfaceView surfaceView;
private Camera camera = null;
private MediaRecorder mediaRecorder = null;
@Override
public void onCreate() {
/* // Start foreground service to avoid unexpected kill
Notification notification = new Notification.Builder(this)
.setContentTitle("Background Video Recorder")
.setContentText("")
.setSmallIcon(R.drawable.ic_launcher)
.build();
startForeground(1234, notification);*/
// Create new SurfaceView, set its size to 1x1, move it to the top left corner and set this service as a callback
windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
surfaceView = new SurfaceView(this);
LayoutParams layoutParams = new WindowManager.LayoutParams(
1, 1,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT
);
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
windowManager.addView(surfaceView, layoutParams);
surfaceView.getHolder().addCallback(this);
}
// Method called right after Surface created (initializing and starting MediaRecorder)
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
camera = Camera.open();
mediaRecorder = new MediaRecorder();
camera.unlock();
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
//mediaRecorder.setVideoSize(320, 240);
mediaRecorder.setOutputFile(
Environment.getExternalStorageDirectory()+"/"+
DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime())+
".mp4"
);
try { mediaRecorder.prepare(); } catch (Exception e) {}
mediaRecorder.start();
}
// Stop recording and remove SurfaceView
@Override
public void onDestroy() {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
camera.lock();
camera.release();
windowManager.removeView(surfaceView);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {}
@Override
public IBinder onBind(Intent intent) { return null; }
}
以下是我运行应用程序时得到的错误日志:
02-06 17:42:03.039: E/AndroidRuntime(8291): FATAL EXCEPTION: main
02-06 17:42:03.039: E/AndroidRuntime(8291): java.lang.IllegalStateException
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.media.MediaRecorder.start(Native Method)
02-06 17:42:03.039: E/AndroidRuntime(8291): at com.android.camerarecorder.VideoRecorderService.surfaceCreated(VideoRecorderService.java:86)
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.view.SurfaceView.updateWindow(SurfaceView.java:553)
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.view.SurfaceView.access$000(SurfaceView.java:81)
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1710)
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2539)
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.os.Looper.loop(Looper.java:137)
02-06 17:42:03.039: E/AndroidRuntime(8291): at android.app.ActivityThread.main(ActivityThread.java:4514)
02-06 17:42:03.039: E/AndroidRuntime(8291): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 17:42:03.039: E/AndroidRuntime(8291): at java.lang.reflect.Method.invoke(Method.java:511)
02-06 17:42:03.039: E/AndroidRuntime(8291): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-06 17:42:03.039: E/AndroidRuntime(8291): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-06 17:42:03.039: E/AndroidRuntime(8291): at dalvik.system.NativeStart.main(Native Method)