我在同一个过程中尝试了一个服务,问题是一样的.
我的视频处理器:
@OverrIDepublic voID onCreate() { super.onCreate(); isRunning = true; iDalerta = Common.getStorage().getString(Constants.ID_ALERTA_SERVICE,""); vIDeoCount = 1; mIntentRecorder = new Intent(VIDeoHandlerService.this,RecorderService.class); mIntentSend = new Intent(VIDeoHandlerService.this,SendVIDeoService.class); updateVIDeoTime(); mIntentRecorder.putExtra(Constants.VIDEO_TIME,vIDeoTime); mIntentRecorder.putExtra(Constants.ID_ALERTA_SERVICE,iDalerta); mIntentRecorder.putExtra(Constants.COUNT_ALERTA,vIDeoCount); startService(mIntentRecorder); newVIDeo();}public voID newVIDeo() { Common.log("new VIDeo"); new Handler().postDelayed(new Runnable() { @OverrIDe public voID run() { vIDeoCount++; startNewVIDeo(); if (vIDeoCount == 2) sendVIDeo(4000); } },vIDeoTime + 2000);}public voID startNewVIDeo() { final Intent intentRecorder = new Intent(VIDeoHandlerService.this,RecorderService.class); intentRecorder.putExtra(Constants.VIDEO_TIME,vIDeoTime); Common.log("iDalerta " + iDalerta); intentRecorder.putExtra(Constants.ID_ALERTA_SERVICE,iDalerta); intentRecorder.putExtra(Constants.COUNT_ALERTA,vIDeoCount); stopService(intentRecorder); if (Common.getStorage().getBoolean(Constants.RECORDER_ACTIVE,false)) { if (Common.getStorage().getString(Constants.ID_ALERTA_SERVICE,"") != null && Common.getStorage().getString(Constants.ID_ALERTA_SERVICE,"").length() > 0) { new Handler().postDelayed(new Runnable() { @OverrIDe public voID run() { Common.log("Start service recorder"); startService(intentRecorder); newVIDeo(); } },5000); } } else new Handler().postDelayed(new Runnable() { @OverrIDe public voID run() { startNewVIDeo(); } },5000);}
这是记录的过程:
@OverrIDepublic int onStartCommand(Intent intent,int flags,int startID) { Common.log("onStartCommand"); if (intent != null && ((Common.getStorage().getString(Constants.ID_ALERTA_SERVICE,"").length() > 0))) { windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); mLayoutParams = new WindowManager.LayoutParams( 1,1,WindowManager.LayoutParams.TYPE_SYstem_OVERLAY,WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_touch,PixelFormat.TRANSLUCENT ); mLayoutParams.gravity = Gravity.left | Gravity.top; //Cojo Información del vIDeohandler Common.log("Intent distinto de null"); mVIDeoTime = intent.getIntExtra(Constants.VIDEO_TIME,15000); mIDalerta = intent.getStringExtra(Constants.ID_ALERTA_SERVICE); mVIDeoCount = intent.getIntExtra(Constants.COUNT_ALERTA,1); Common.log("VIDeo time" + mVIDeoTime); Common.getStorage().putBoolean(Constants.RECORDER_ACTIVE,true); initRecording(); } return START_NOT_STICKY;}public voID initRecording() { Common.log("INIT RECORDING IN"); surfaceVIEw = new SurfaceVIEw(this); windowManager.addVIEw(surfaceVIEw,mLayoutParams); surfaceVIEw.getHolder().addCallback(this); Common.log("INIT RECORDING OUT");}@Nullable@OverrIDepublic IBinder onBind(Intent intent) { return null;}@OverrIDepublic voID onDestroy() { Common.log("onDestroy VIDeo"); PreferenceHelper preferenceHelper = PreferenceHelper.newInstance(RecorderService.this,Constants.USER_PREFERENCES); preferenceHelper.setBoolean(Constants.IS_RECORDING,false); try { camera.setPrevIEwCallback(null); } catch (Exception e){ Common.log("error setPrevIECallback" + (e.getMessage() != null ? e.getMessage() : "sin mensaje")); } try { muteSounds(false); } catch (Exception e) { Common.log(e.getMessage()); } try { Common.log("media recorder stop"); mediaRecorder.stop(); Common.log("media recorder reset"); mediaRecorder.reset(); Common.log("media recorder release"); mediaRecorder.release(); Common.log("media camera lock"); camera.lock(); Common.log("media camera release"); camera.release(); } catch (Exception e) { } Common.log("onDestroy VIDeo"); Common.getStorage().putBoolean(Constants.RECORDER_ACTIVE,false); RecorderService.super.onDestroy(); System.exit(0);}@OverrIDepublic voID surfaceCreated(SurfaceHolder holder) { mSurfaceHolder = holder; Common.log("SurfaceCreated"); try { try { muteSounds(true); } catch (Exception e) { Common.log(e.getMessage()); } Common.log("CAMERA OPEN 1"); try { camera = Camera.open(1); } catch (RuntimeException e) { Common.log(e.getMessage()); } Common.log("CAMERA OPEN 2"); /////////////////////////// mCamCoderProfile = CamcorderProfile.get(1,CamcorderProfile.QUAliTY_LOW); mCamCoderProfile.vIDeoCodec = MediaRecorder.VIDeoEncoder.MPEG_4_SP; mCamCoderProfile.audioCodec = MediaRecorder.AudioEncoder.AAC; /////////////////////////// startRecorder(); } catch (Exception e) { if (e.getMessage() != null) Common.log(e.getMessage()); }}public voID muteSounds(boolean mute) { Common.log("MUTE SOUNDS IN"); AudioManager mgr = ((AudioManager) getSystemService(Context.AUdio_SERVICE)); mgr.setStreamMute(AudioManager.STREAM_SYstem,true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (mute) { mgr.adjustStreamVolume(AudioManager.STREAM_SYstem,AudioManager.ADJUST_MUTE,0); mgr.adjustVolume(AudioManager.ADJUST_MUTE,0); mgr.adjustVolume(AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE,0); } else { mgr.adjustStreamVolume(AudioManager.STREAM_SYstem,AudioManager.ADJUST_UNMUTE,0); mgr.adjustVolume(AudioManager.ADJUST_UNMUTE,0); } } else { mgr.setStreamMute(AudioManager.STREAM_SYstem,mute); mgr.setStreamMute(AudioManager.STREAM_MUSIC,mute); if (mute) { molderVolumen = mgr.getStreamVolume(AudioManager.STREAM_RING); mgr.setStreamVolume(AudioManager.STREAM_RING,0); } else mgr.setStreamVolume(AudioManager.STREAM_RING,molderVolumen,0); mgr.adjustVolume(AudioManager.ADJUST_LOWER,0); } Common.log("MUTE SOUNDS OUT");}public voID startRecorder() { mediaRecorder = new MediaRecorder(); camera.unlock(); mediaRecorder.setCamera(camera); mediaRecorder.setPrevIEwdisplay(mSurfaceHolder.getSurface()); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mediaRecorder.setVIDeoSource(MediaRecorder.VIDeoSource.CAMERA); mediaRecorder.setProfile(mCamCoderProfile); vIDeofile = new file(Environment.getExternalStorageDirectory() + "/" + "wbunker_" + mIDalerta + "_" + mVIDeoCount + ".mp4"); Common.log("VIDeo rercorder name file " + vIDeoRecording); mediaRecorder.setoutputfile(vIDeofile.getPath()); mediaRecorder.setorIEntationHint(270); try { Common.log("mediaRecorder prepare"); mediaRecorder.prepare(); Common.log("mediaRecorder prepare"); } catch (Exception e) { } Common.log("mediaRecorder start"); mediaRecorder.start(); Common.log("mediaRecorder start"); Common.log("Estoy grabando macho");}
当我尝试停止服务时,服务停止,但即使杀死应用程序也不会释放相机.有人能帮我吗??提前致谢.
解决方法 在服务的onDestroy()方法中,用camera.unLock()替换camera.lock(),并在startRecorder()方法中用camera.lock()替换camera.unLock() 总结以上是内存溢出为你收集整理的Bug录制视频android全部内容,希望文章能够帮你解决Bug录制视频android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)