後半のコード
java
1 2 private void startRecordingVideo() { 3 Log.e(TAG,"startRecordingVideo()"); 4 if (null == mCameraDevice) { 5 Log.e(TAG,"return"); 6 return; 7 } 8 try { 9 Log.e(TAG,"try"); 10 closePreviewSession(); 11 setUpMediaRecorder(); 12 mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD); 13 List<Surface> surfaces = new ArrayList<>(); 14 15 Surface recorderSurface = mMediaRecorder.getSurface(); 16 surfaces.add(recorderSurface); 17 mPreviewBuilder.addTarget(recorderSurface); 18 mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() { 19 20 21 @Override 22 public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { 23 Log.e(TAG,"CameraCaptureSession:onCongigured"); 24 mPreviewSession = cameraCaptureSession; 25 mBackgroundHandler.post(new Runnable() { 26 @Override 27 public void run() { 28 mIsRecordingVideo = true; 29 mMediaRecorder.start(); 30 } 31 }); 32 } 33 34 @Override 35 public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { 36 Log.e(TAG,"onConfigureFailed"); 37 } 38 }, mBackgroundHandler); 39 } catch (CameraAccessException | IOException e) { 40 e.printStackTrace(); 41 } 42 43 } 44 45 46 47 private void closePreviewSession() { 48 Log.e(TAG,"closePreviewSession()"); 49 if (mPreviewSession != null) { 50 mPreviewSession.close(); 51 mPreviewSession = null; 52 } 53 } 54 55 56 57 private void setUpMediaRecorder() throws IOException { 58 Log.e(TAG,"setUpMediaRecorder"); 59 mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 60 mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); 61 mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 62 if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) { 63 //mNextVideoAbsolutePath = getVideoFilePath(this); 64 mNextVideoAbsolutePath = getVideoFilePath(); 65 Log.e(TAG,mNextVideoAbsolutePath); 66 } 67 mMediaRecorder.setOutputFile(mNextVideoAbsolutePath); 68 mMediaRecorder.setVideoEncodingBitRate(10000000); 69 mMediaRecorder.setVideoFrameRate(30); 70 mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight()); 71 mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 72 mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 73 WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 74 int rotation = windowManager.getDefaultDisplay().getRotation(); 75 switch (mSensorOrientation) { 76 case SENSOR_ORIENTATION_DEFAULT_DEGREES: 77 mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation)); 78 break; 79 case SENSOR_ORIENTATION_INVERSE_DEGREES: 80 mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation)); 81 break; 82 } 83 mMediaRecorder.prepare(); 84 } 85 86 87 public String getVideoFilePath() { 88 Log.e(TAG, "getVideoFilePahy"); 89 90 File filePathy; 91 String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/cameraMovie/"; 92 filePathy = new File(path); 93 if (!filePathy.exists()) { 94 boolean result = filePathy.mkdir(); 95 if (result) { 96 System.out.println("Success"); 97 } 98 } 99 return filePathy.toString() + "/" + System.currentTimeMillis() + ".mp4"; 100 } 101 102 103 public void stop(){ 104 Log.e(TAG,"stop()"); 105 if (mIsRecordingVideo) { 106 //この上の確認のためにあるstopメソッド 107 stopRecordingVideo(); 108 } 109 } 110 111 private void stopRecordingVideo() { 112 Log.e(TAG,"stopRecordingVideo()"); 113 mIsRecordingVideo = false; 114 //349行目です。 115 mMediaRecorder.stop(); 116 mMediaRecorder.reset(); 117 mNextVideoAbsolutePath = null; 118 } 119 120 @Override 121 public void onDestroy() { 122 // 削除作業を継続させる 123 super.onDestroy(); 124 Log.e(TAG,"onDestroy()"); 125 // windowからViewを排除する 126 windowManager.removeView(views); 127 128 //onPause()のやつ2つ 129 closeCamera(); 130 stopBackgroundThread(); 131 132 } 133 134 private void closeCamera() { 135 Log.e(TAG,"closeCamera()"); 136 try { 137 mCameraOpenCloseLock.acquire(); 138 closePreviewSession(); 139 if (null != mCameraDevice) { 140 mCameraDevice.close(); 141 mCameraDevice = null; 142 } 143 if (null != mMediaRecorder) { 144 mMediaRecorder.release(); 145 mMediaRecorder = null; 146 } 147 } catch (InterruptedException e) { 148 throw new RuntimeException("Interrupted while trying to lock camera closing."); 149 } finally { 150 mCameraOpenCloseLock.release(); 151 } 152 } 153 154 155 private void stopBackgroundThread() { 156 Log.e(TAG,"stopBackgroundThread"); 157 mBackgroundThread.quitSafely(); 158 try { 159 mBackgroundThread.join(); 160 mBackgroundThread = null; 161 mBackgroundHandler = null; 162 } catch (InterruptedException e) { 163 e.printStackTrace(); 164 } 165 } 166 167 168 @Override 169 public IBinder onBind(Intent intent) { 170 return null; 171 } 172 173} 174
あなたの回答
tips
プレビュー