我有点腌渍。我正在尝试在从后台服务启动视图时显示摄像头源。我收到运行时错误
04-19 15:00:31.872 31125-31442/csce483.vbox E/Recorder: starting prepare
04-19 15:00:31.872 31125-31442/csce483.vbox E/MediaRecorderJNI: Application lost the surface
04-19 15:00:31.872 31125-31442/csce483.vbox E/Recorder: starting record
04-19 15:00:31.872 31125-31442/csce483.vbox E/MediaRecorder: start called in an invalid state: 4
当我使用动态创建的视图时。静态视图与Window Manager不兼容 在onCreate()中:
try {
mCamera = Camera.open();
}
catch (Exception e) {
e.printStackTrace();
}
mSurfaceView = (SurfaceView) View.inflate(getBaseContext(),R.layout.camera,null);
//mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceView.setVisibility(View.INVISIBLE);
yourBR = new MyReceiver();
yourBR.setMainActivityHandler(this);
IntentFilter callInterceptorIntentFilter = new IntentFilter();
callInterceptorIntentFilter.addAction("csce483.vbox.start");
callInterceptorIntentFilter.addAction("csce483.vbox.stop");
registerReceiver(yourBR, callInterceptorIntentFilter);
然后在我的接收者中:
public static class MyReceiver extends BroadcastReceiver {
MainActivity yourMain = null;
void setMainActivityHandler(MainActivity main){
yourMain = main;
}
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getAction();
if(message.equalsIgnoreCase("csce483.vbox.start")){
if(!yourMain.recording) {
Log.e("accel", "received a craSH!");
yourMain.mSurfaceView.setVisibility(View.VISIBLE);
yourMain.wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
yourMain.wm.addView( yourMain.mSurfaceView, windowManagerParams);
new Thread(new Runnable() {
@Override
public void run() {
yourMain.startRecording(); //record w/ new camera API
}
}).start();
}
}
else if(message.equalsIgnoreCase("csce483.vbox.stop")){
Log.e("accel", "recieved stop signal!");
yourMain.stopRecording();
}
}
}
启动录音机:
public void startRecording() {
//all file code is TEMPORARY until server is working
String timestamp = new SimpleDateFormat("MMddyyyy_HHmmss").format(new Date());
videoFile = new File(Environment.getExternalStorageDirectory(), "/Pictures/"+timestamp+".mp4");
Log.e("file", "Saving to: " + videoFile.getAbsolutePath());
if (!videoFile.exists()) {
File parent = videoFile.getParentFile();
if (parent != null) {
try {
videoFile.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
cameraTriggered = true;
if(!cameraPresent()) return;
if (mRecorder == null) {
mRecorder = new MediaRecorder();
}
mCamera.unlock();
mRecorder.setCamera(mCamera);
mRecorder.setPreviewDisplay(mHolder.getSurface());
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mRecorder.setPreviewDisplay(mHolder.getSurface());
if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
mRecorder.setOrientationHint(90);
}
else {
mRecorder.setOrientationHint(0);
}
mRecorder.setOutputFile(videoFile.getAbsolutePath());
try {
Log.e("Recorder","starting prepare");
mRecorder.prepare();
}
catch (Exception e) {
e.printStackTrace();
}
try {
Log.e("Recorder","starting record");
mRecorder.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
谢谢!