質問編集履歴

8

コードの変更

2017/08/08 22:00

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -8,333 +8,333 @@
8
8
 
9
9
 
10
10
 
11
+ private void startRecordingVideo() {
12
+
13
+ Log.e(TAG,"startRecordingVideo()");
14
+
15
+ if (null == mCameraDevice) {
16
+
17
+ Log.e(TAG,"return");
18
+
19
+ return;
20
+
21
+ }
22
+
23
+ try {
24
+
25
+ Log.e(TAG,"try");
26
+
27
+ closePreviewSession();
28
+
29
+ setUpMediaRecorder();
30
+
31
+ mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
32
+
33
+ List<Surface> surfaces = new ArrayList<>();
34
+
35
+
36
+
37
+ Surface recorderSurface = mMediaRecorder.getSurface();
38
+
39
+ surfaces.add(recorderSurface);
40
+
41
+ mPreviewBuilder.addTarget(recorderSurface);
42
+
43
+ mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
44
+
45
+
46
+
47
+
48
+
49
+ @Override
50
+
51
+ public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
52
+
53
+ Log.e(TAG,"CameraCaptureSession:onCongigured");
54
+
55
+ mPreviewSession = cameraCaptureSession;
56
+
57
+ mBackgroundHandler.post(new Runnable() {
58
+
59
+ @Override
60
+
61
+ public void run() {
62
+
63
+ mIsRecordingVideo = true;
64
+
65
+ mMediaRecorder.start();
66
+
67
+ }
68
+
69
+ });
70
+
71
+ }
72
+
73
+
74
+
75
+ @Override
76
+
77
+ public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
78
+
79
+ Log.e(TAG,"onConfigureFailed");
80
+
81
+ }
82
+
83
+ }, mBackgroundHandler);
84
+
85
+ } catch (CameraAccessException | IOException e) {
86
+
87
+ e.printStackTrace();
88
+
89
+ }
90
+
91
+
92
+
93
+ }
94
+
95
+
96
+
97
+
98
+
99
+
100
+
11
- private void closePreviewSession() {
101
+ private void closePreviewSession() {
12
-
102
+
13
- Log.e(TAG,"closePreviewSession()");
103
+ Log.e(TAG,"closePreviewSession()");
14
-
104
+
15
- if (mPreviewSession != null) {
105
+ if (mPreviewSession != null) {
16
-
106
+
17
- mPreviewSession.close();
107
+ mPreviewSession.close();
18
-
108
+
19
- mPreviewSession = null;
109
+ mPreviewSession = null;
110
+
111
+ }
112
+
113
+ }
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+ private void setUpMediaRecorder() throws IOException {
122
+
123
+ Log.e(TAG,"setUpMediaRecorder");
124
+
125
+ mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
126
+
127
+ mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
128
+
129
+ mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
130
+
131
+ if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
132
+
133
+ //mNextVideoAbsolutePath = getVideoFilePath(this);
134
+
135
+ mNextVideoAbsolutePath = getVideoFilePath();
136
+
137
+ Log.e(TAG,mNextVideoAbsolutePath);
138
+
139
+ }
140
+
141
+ mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
142
+
143
+ mMediaRecorder.setVideoEncodingBitRate(10000000);
144
+
145
+ mMediaRecorder.setVideoFrameRate(30);
146
+
147
+ mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
148
+
149
+ mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
150
+
151
+ mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
152
+
153
+ WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
154
+
155
+ int rotation = windowManager.getDefaultDisplay().getRotation();
156
+
157
+ switch (mSensorOrientation) {
158
+
159
+ case SENSOR_ORIENTATION_DEFAULT_DEGREES:
160
+
161
+ mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
162
+
163
+ break;
164
+
165
+ case SENSOR_ORIENTATION_INVERSE_DEGREES:
166
+
167
+ mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
168
+
169
+ break;
170
+
171
+ }
172
+
173
+ mMediaRecorder.prepare();
174
+
175
+ }
176
+
177
+
178
+
179
+
180
+
181
+ public String getVideoFilePath() {
182
+
183
+ Log.e(TAG, "getVideoFilePahy");
184
+
185
+
186
+
187
+ File filePathy;
188
+
189
+ String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/cameraMovie/";
190
+
191
+ filePathy = new File(path);
192
+
193
+ if (!filePathy.exists()) {
194
+
195
+ boolean result = filePathy.mkdir();
196
+
197
+ if (result) {
198
+
199
+ System.out.println("Success");
20
200
 
21
201
  }
22
202
 
23
203
  }
24
204
 
25
-
205
+ return filePathy.toString() + "/" + System.currentTimeMillis() + ".mp4";
206
+
26
-
207
+ }
208
+
209
+
210
+
211
+
212
+
27
-
213
+ public void stop(){
214
+
28
-
215
+ Log.e(TAG,"stop()");
216
+
29
-
217
+ if (mIsRecordingVideo) {
218
+
30
-
219
+ //この上の確認のためにあるstopメソッド
220
+
31
-
221
+ stopRecordingVideo();
222
+
32
-
223
+ }
224
+
225
+ }
226
+
227
+
228
+
229
+ private void stopRecordingVideo() {
230
+
231
+ Log.e(TAG,"stopRecordingVideo()");
232
+
233
+ mIsRecordingVideo = false;
234
+
235
+ //349行目です。
236
+
237
+ mMediaRecorder.stop();
238
+
239
+ mMediaRecorder.reset();
240
+
241
+ mNextVideoAbsolutePath = null;
242
+
243
+ }
244
+
245
+
246
+
33
- @Override
247
+ @Override
34
-
248
+
35
- public void onDisconnected(@NonNull CameraDevice cameraDevice) {
249
+ public void onDestroy() {
250
+
36
-
251
+ // 削除作業を継続させる
252
+
253
+ super.onDestroy();
254
+
37
- Log.e(TAG,"onDisConnected");
255
+ Log.e(TAG,"onDestroy()");
256
+
257
+ // windowからViewを排除する
258
+
259
+ windowManager.removeView(views);
260
+
261
+
262
+
263
+ //onPause()のやつ2つ
264
+
265
+ closeCamera();
266
+
267
+ stopBackgroundThread();
268
+
269
+
270
+
271
+ }
272
+
273
+
274
+
275
+ private void closeCamera() {
276
+
277
+ Log.e(TAG,"closeCamera()");
278
+
279
+ try {
280
+
281
+ mCameraOpenCloseLock.acquire();
282
+
283
+ closePreviewSession();
284
+
285
+ if (null != mCameraDevice) {
286
+
287
+ mCameraDevice.close();
288
+
289
+ mCameraDevice = null;
290
+
291
+ }
292
+
293
+ if (null != mMediaRecorder) {
294
+
295
+ mMediaRecorder.release();
296
+
297
+ mMediaRecorder = null;
298
+
299
+ }
300
+
301
+ } catch (InterruptedException e) {
302
+
303
+ throw new RuntimeException("Interrupted while trying to lock camera closing.");
304
+
305
+ } finally {
38
306
 
39
307
  mCameraOpenCloseLock.release();
40
308
 
41
- cameraDevice.close();
42
-
43
- mCameraDevice = null;
44
-
45
- }
46
-
47
-
48
-
49
- @Override
50
-
51
- public void onError(@NonNull CameraDevice cameraDevice, int error) {
52
-
53
- Log.e(TAG,"onError");
54
-
55
- mCameraOpenCloseLock.release();
56
-
57
- cameraDevice.close();
58
-
59
- mCameraDevice = null;
60
-
61
- }
62
-
63
-
64
-
65
- };
66
-
67
-
68
-
69
- private void closePreviewSession() {
70
-
71
- Log.e(TAG,"closePreviewSession");
72
-
73
- if (mPreviewSession != null) {
74
-
75
- mPreviewSession.close();
76
-
77
- mPreviewSession = null;
78
-
79
- }
80
-
81
- }
82
-
83
-
84
-
85
-
86
-
87
- private void startRecordingVideo() {
88
-
89
- Log.e(TAG,"startRecordingVideo()");
90
-
91
- if (null == mCameraDevice) {
92
-
93
- Log.e(TAG,"return");
94
-
95
- return;
96
-
97
- }
98
-
99
- try {
100
-
101
- Log.e(TAG,"try");
102
-
103
- closePreviewSession();
104
-
105
- setUpMediaRecorder();
106
-
107
- mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
108
-
109
- List<Surface> surfaces = new ArrayList<>();
110
-
111
-
112
-
113
- Surface recorderSurface = mMediaRecorder.getSurface();
114
-
115
- surfaces.add(recorderSurface);
116
-
117
- mPreviewBuilder.addTarget(recorderSurface);
118
-
119
- mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
120
-
121
-
122
-
123
-
124
-
125
- @Override
126
-
127
- public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
128
-
129
- Log.e(TAG,"CameraCaptureSession:onCongigured");
130
-
131
- mPreviewSession = cameraCaptureSession;
132
-
133
- mBackgroundHandler.post(new Runnable() {
134
-
135
- @Override
136
-
137
- public void run() {
138
-
139
- mIsRecordingVideo = true;
140
-
141
- mMediaRecorder.start();
142
-
143
- }
144
-
145
- });
146
-
147
- }
148
-
149
-
150
-
151
- @Override
152
-
153
- public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
154
-
155
- Log.e(TAG,"onConfigureFailed");
156
-
157
- }
158
-
159
- }, mBackgroundHandler);
160
-
161
- } catch (CameraAccessException | IOException e) {
162
-
163
- e.printStackTrace();
164
-
165
- }
166
-
167
-
168
-
169
- }
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
- //もう一度closePreviewSession()が呼ばれる
178
-
179
-
180
-
181
- private void setUpMediaRecorder() throws IOException {
182
-
183
-
184
-
185
- Log.e(TAG,"setUpMediaRecorder");
186
-
187
- // mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
188
-
189
- mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
190
-
191
- mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
192
-
193
- if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
194
-
195
- //mNextVideoAbsolutePath = getVideoFilePath(this);
196
-
197
- mNextVideoAbsolutePath = getVideoFilePathy();
198
-
199
- Log.e(TAG,mNextVideoAbsolutePath);
200
-
201
- }
202
-
203
-
204
-
205
- mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
206
-
207
- mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
208
-
209
- mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
210
-
211
- mMediaRecorder.setVideoEncodingBitRate(10000000);
212
-
213
- mMediaRecorder.setVideoFrameRate(30);
214
-
215
- mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
216
-
217
- mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
218
-
219
- mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
220
-
221
- WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
222
-
223
- int rotation = windowManager.getDefaultDisplay().getRotation();
224
-
225
- switch (mSensorOrientation) {
226
-
227
- case SENSOR_ORIENTATION_DEFAULT_DEGREES:
228
-
229
- mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
230
-
231
- break;
232
-
233
- case SENSOR_ORIENTATION_INVERSE_DEGREES:
234
-
235
- mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
236
-
237
- break;
238
-
239
- }
240
-
241
- mMediaRecorder.prepare();
242
-
243
- }
244
-
245
-
246
-
247
- public String getVideoFilePathy() {
248
-
249
- Log.e(TAG, "getVideoFilePahy");
250
-
251
-
252
-
253
- File filePathy;
254
-
255
- String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/cameraMovie/";
256
-
257
- filePathy = new File(path);
258
-
259
- if (!filePathy.exists()) {
260
-
261
- boolean result = filePathy.mkdir();
262
-
263
- if (result) {
264
-
265
- System.out.println("Success");
266
-
267
- }
268
-
269
- }
270
-
271
- return filePathy.toString() + "/" + System.currentTimeMillis() + ".mp4";
272
-
273
-
274
-
275
- }
276
-
277
-
278
-
279
- //ここで、pathが表示される
280
-
281
- //ファイルが、保存されてたら、logを出す。
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
- //この下が多分呼ばれる
290
-
291
-
292
-
293
- private void stopRecordingVideo() {
294
-
295
- Log.e(TAG,"stopRecordingVideo");
296
-
297
- mIsRecordingVideo = false;
298
-
299
-
300
-
301
- try{
302
-
303
- mMediaRecorder.stop();
304
-
305
- }catch(RuntimeException stopException){
306
-
307
- //例外の例外らしい https://translate.googleusercontent.com/translate_c?depth=1&hl=ja&prev=search&rurl=translate.google.co.jp&sl=en&sp=nmt4&u=https://stackoverflow.com/questions/16221866/mediarecorder-failed-when-i-stop-the-recording&usg=ALkJrhiJDHh9TLeR_qko07QXWEqsYG8hAg
308
-
309
- }
310
-
311
- mMediaRecorder.reset();
312
-
313
- mNextVideoAbsolutePath = null;
314
-
315
- startPreview();
316
-
317
- }
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
- public void stop(){
326
-
327
- Log.e(TAG,"stop()");
328
-
329
- if (mIsRecordingVideo) {
330
-
331
- stopRecordingVideo();
332
-
333
- }
334
-
335
- }
336
-
337
-
309
+ }
310
+
311
+ }
312
+
313
+
314
+
315
+
316
+
317
+ private void stopBackgroundThread() {
318
+
319
+ Log.e(TAG,"stopBackgroundThread");
320
+
321
+ mBackgroundThread.quitSafely();
322
+
323
+ try {
324
+
325
+ mBackgroundThread.join();
326
+
327
+ mBackgroundThread = null;
328
+
329
+ mBackgroundHandler = null;
330
+
331
+ } catch (InterruptedException e) {
332
+
333
+ e.printStackTrace();
334
+
335
+ }
336
+
337
+ }
338
338
 
339
339
 
340
340
 
@@ -342,118 +342,14 @@
342
342
 
343
343
  @Override
344
344
 
345
- public void onDestroy() {
346
-
347
- Log.e(TAG,"onDestroy()");
348
-
349
- // windowからViewを排除する
350
-
351
- windowManager.removeView(views);
352
-
353
-
354
-
355
- //onPause()のやつ2つ
356
-
357
- closeCamera();
358
-
359
- stopBackgroundThread();
360
-
361
-
362
-
363
- // 削除作業を継続させる
364
-
365
- super.onDestroy();
366
-
367
- }
368
-
369
-
370
-
371
- @Override
372
-
373
345
  public IBinder onBind(Intent intent) {
374
346
 
375
- Log.e(TAG, "onBind");
376
-
377
347
  return null;
378
348
 
379
349
  }
380
350
 
381
351
 
382
352
 
383
-
384
-
385
-
386
-
387
- private void closeCamera() {
388
-
389
- Log.e(TAG,"closeCamera()");
390
-
391
- try {
392
-
393
- mCameraOpenCloseLock.acquire();
394
-
395
- closePreviewSession();
396
-
397
- if (null != mCameraDevice) {
398
-
399
- mCameraDevice.close();
400
-
401
- mCameraDevice = null;
402
-
403
- }
404
-
405
- if (null != mMediaRecorder) {
406
-
407
- mMediaRecorder.release();
408
-
409
- mMediaRecorder = null;
410
-
411
- }
412
-
413
- } catch (InterruptedException e) {
414
-
415
- throw new RuntimeException("Interrupted while trying to lock camera closing.");
416
-
417
- } finally {
418
-
419
- mCameraOpenCloseLock.release();
420
-
421
- }
422
-
423
- }
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
- private void stopBackgroundThread() {
432
-
433
- Log.e(TAG,"stopBackgroundThread");
434
-
435
- mBackgroundThread.quitSafely();
436
-
437
- try {
438
-
439
- mBackgroundThread.join();
440
-
441
- mBackgroundThread = null;
442
-
443
- mBackgroundHandler = null;
444
-
445
- } catch (InterruptedException e) {
446
-
447
- e.printStackTrace();
448
-
449
- }
450
-
451
- }
452
-
453
-
454
-
455
-
456
-
457
353
  }
458
354
 
459
355
 

7

コードの修正

2017/08/08 22:00

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -244,21 +244,33 @@
244
244
 
245
245
 
246
246
 
247
- private String getVideoFilePath(Context context) {
247
+ public String getVideoFilePathy() {
248
-
248
+
249
- Log.e(TAG, "getVideoFilePath");
249
+ Log.e(TAG, "getVideoFilePahy");
250
-
250
+
251
+
252
+
251
- final File dir = context.getExternalFilesDir(null);
253
+ File filePathy;
252
-
254
+
253
- Log.e(TAG, (dir == null ? "" : (dir.getAbsolutePath() + "/"))
255
+ String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/cameraMovie/";
256
+
254
-
257
+ filePathy = new File(path);
258
+
259
+ if (!filePathy.exists()) {
260
+
261
+ boolean result = filePathy.mkdir();
262
+
263
+ if (result) {
264
+
255
- + System.currentTimeMillis() + ".mp4");
265
+ System.out.println("Success");
256
-
266
+
257
- //E/MyService7: /storage/emulated/0/Android/data/com.google.android.exoplayer2.demo/files/1501948606535.mp4
267
+ }
268
+
258
-
269
+ }
270
+
259
- return (dir == null ? "" : (dir.getAbsolutePath() + "/"))
271
+ return filePathy.toString() + "/" + System.currentTimeMillis() + ".mp4";
260
-
261
- + System.currentTimeMillis() + ".mp4";
272
+
273
+
262
274
 
263
275
  }
264
276
 

6

コードの変更

2017/08/08 13:32

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,66 @@
6
6
 
7
7
  ```java
8
8
 
9
+
10
+
11
+ private void closePreviewSession() {
12
+
13
+ Log.e(TAG,"closePreviewSession()");
14
+
15
+ if (mPreviewSession != null) {
16
+
17
+ mPreviewSession.close();
18
+
19
+ mPreviewSession = null;
20
+
21
+ }
22
+
23
+ }
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+ @Override
34
+
35
+ public void onDisconnected(@NonNull CameraDevice cameraDevice) {
36
+
37
+ Log.e(TAG,"onDisConnected");
38
+
39
+ mCameraOpenCloseLock.release();
40
+
41
+ cameraDevice.close();
42
+
43
+ mCameraDevice = null;
44
+
45
+ }
46
+
47
+
48
+
49
+ @Override
50
+
51
+ public void onError(@NonNull CameraDevice cameraDevice, int error) {
52
+
53
+ Log.e(TAG,"onError");
54
+
55
+ mCameraOpenCloseLock.release();
56
+
57
+ cameraDevice.close();
58
+
59
+ mCameraDevice = null;
60
+
61
+ }
62
+
63
+
64
+
65
+ };
66
+
67
+
68
+
9
69
  private void closePreviewSession() {
10
70
 
11
71
  Log.e(TAG,"closePreviewSession");

5

コードの修正

2017/08/08 13:14

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -184,35 +184,21 @@
184
184
 
185
185
 
186
186
 
187
-
188
-
189
- public String getVideoFilePathy() {
187
+ private String getVideoFilePath(Context context) {
190
-
191
-
192
-
188
+
193
- Log.e(TAG,"getVideoFilePahy");
189
+ Log.e(TAG, "getVideoFilePath");
194
-
195
- //もしmovieがDownload/movie以下に存在しなかったらダウンロード
190
+
196
-
197
- // File filePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "checkMovie");
198
-
199
- //File[] storedFiles = filePath.listFiles();
200
-
201
-
202
-
203
- // File filePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "checkMovie");
204
-
205
- File filePath=Environment.getExternalStorageDirectory();
191
+ final File dir = context.getExternalFilesDir(null);
192
+
206
-
193
+ Log.e(TAG, (dir == null ? "" : (dir.getAbsolutePath() + "/"))
194
+
207
- String newfile=filePath.getAbsolutePath();
195
+ + System.currentTimeMillis() + ".mp4");
196
+
208
-
197
+ //E/MyService7: /storage/emulated/0/Android/data/com.google.android.exoplayer2.demo/files/1501948606535.mp4
198
+
199
+ return (dir == null ? "" : (dir.getAbsolutePath() + "/"))
200
+
209
- newfile = newfile + System.currentTimeMillis() + ".mp4";
201
+ + System.currentTimeMillis() + ".mp4";
210
-
211
-
212
-
213
- return newfile;
214
-
215
-
216
202
 
217
203
  }
218
204
 

4

説明の追加

2017/08/08 13:05

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,8 @@
1
+ [前半のコードと質問のURL](https://teratail.com/questions/87492)
2
+
3
+
4
+
1
- コード用 2
5
+ 後半のコード
2
-
3
-
4
-
5
-
6
6
 
7
7
  ```java
8
8
 
@@ -401,5 +401,3 @@
401
401
 
402
402
 
403
403
  ```
404
-
405
- [前半のコードと質問のURL](https://teratail.com/questions/87492)

3

説明の追加

2017/08/08 12:33

投稿

giant
giant

スコア132

test CHANGED
@@ -1 +1 @@
1
- MediaRecorderインスタンスを再利用する際のIllegalStateExceptionの解決
1
+ (後半コード)MediaRecorderインスタンスを再利用する際のIllegalStateExceptionの解決
test CHANGED
@@ -2,83 +2,281 @@
2
2
 
3
3
 
4
4
 
5
+
6
+
5
7
  ```java
6
8
 
7
-
8
-
9
- public class MyService7 extends Service {
10
-
11
- public MyService7() {
12
-
13
- }
14
-
15
-
16
-
17
- private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
18
-
19
- private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
20
-
21
- private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
22
-
23
- private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();
24
-
25
- private static final String TAG = "MyService7";
26
-
27
-
28
-
29
- static {
30
-
31
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_0, 90);
32
-
33
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_90, 0);
34
-
35
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_180, 270);
36
-
37
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_270, 180);
38
-
39
- }
40
-
41
-
42
-
43
- static {
44
-
45
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_0, 270);
46
-
47
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_90, 180);
48
-
49
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_180, 90);
50
-
51
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0);
52
-
53
- }
54
-
55
-
56
-
57
- private CameraDevice mCameraDevice;
58
-
59
- private CameraCaptureSession mPreviewSession;
60
-
61
- private Size mVideoSize;
62
-
63
- private MediaRecorder mMediaRecorder;
64
-
65
- private boolean mIsRecordingVideo;
66
-
67
- private HandlerThread mBackgroundThread;
68
-
69
- private Handler mBackgroundHandler;
70
-
71
- private Semaphore mCameraOpenCloseLock = new Semaphore(1);
72
-
73
-
74
-
75
- Handler handler;
76
-
77
- WindowManager windowManager;
78
-
79
- View views;
80
-
81
- String firstOpened="first";
9
+ private void closePreviewSession() {
10
+
11
+ Log.e(TAG,"closePreviewSession");
12
+
13
+ if (mPreviewSession != null) {
14
+
15
+ mPreviewSession.close();
16
+
17
+ mPreviewSession = null;
18
+
19
+ }
20
+
21
+ }
22
+
23
+
24
+
25
+
26
+
27
+ private void startRecordingVideo() {
28
+
29
+ Log.e(TAG,"startRecordingVideo()");
30
+
31
+ if (null == mCameraDevice) {
32
+
33
+ Log.e(TAG,"return");
34
+
35
+ return;
36
+
37
+ }
38
+
39
+ try {
40
+
41
+ Log.e(TAG,"try");
42
+
43
+ closePreviewSession();
44
+
45
+ setUpMediaRecorder();
46
+
47
+ mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
48
+
49
+ List<Surface> surfaces = new ArrayList<>();
50
+
51
+
52
+
53
+ Surface recorderSurface = mMediaRecorder.getSurface();
54
+
55
+ surfaces.add(recorderSurface);
56
+
57
+ mPreviewBuilder.addTarget(recorderSurface);
58
+
59
+ mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
60
+
61
+
62
+
63
+
64
+
65
+ @Override
66
+
67
+ public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
68
+
69
+ Log.e(TAG,"CameraCaptureSession:onCongigured");
70
+
71
+ mPreviewSession = cameraCaptureSession;
72
+
73
+ mBackgroundHandler.post(new Runnable() {
74
+
75
+ @Override
76
+
77
+ public void run() {
78
+
79
+ mIsRecordingVideo = true;
80
+
81
+ mMediaRecorder.start();
82
+
83
+ }
84
+
85
+ });
86
+
87
+ }
88
+
89
+
90
+
91
+ @Override
92
+
93
+ public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
94
+
95
+ Log.e(TAG,"onConfigureFailed");
96
+
97
+ }
98
+
99
+ }, mBackgroundHandler);
100
+
101
+ } catch (CameraAccessException | IOException e) {
102
+
103
+ e.printStackTrace();
104
+
105
+ }
106
+
107
+
108
+
109
+ }
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+ //もう一度closePreviewSession()が呼ばれる
118
+
119
+
120
+
121
+ private void setUpMediaRecorder() throws IOException {
122
+
123
+
124
+
125
+ Log.e(TAG,"setUpMediaRecorder");
126
+
127
+ // mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
128
+
129
+ mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
130
+
131
+ mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
132
+
133
+ if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
134
+
135
+ //mNextVideoAbsolutePath = getVideoFilePath(this);
136
+
137
+ mNextVideoAbsolutePath = getVideoFilePathy();
138
+
139
+ Log.e(TAG,mNextVideoAbsolutePath);
140
+
141
+ }
142
+
143
+
144
+
145
+ mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
146
+
147
+ mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
148
+
149
+ mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
150
+
151
+ mMediaRecorder.setVideoEncodingBitRate(10000000);
152
+
153
+ mMediaRecorder.setVideoFrameRate(30);
154
+
155
+ mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
156
+
157
+ mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
158
+
159
+ mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
160
+
161
+ WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
162
+
163
+ int rotation = windowManager.getDefaultDisplay().getRotation();
164
+
165
+ switch (mSensorOrientation) {
166
+
167
+ case SENSOR_ORIENTATION_DEFAULT_DEGREES:
168
+
169
+ mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
170
+
171
+ break;
172
+
173
+ case SENSOR_ORIENTATION_INVERSE_DEGREES:
174
+
175
+ mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
176
+
177
+ break;
178
+
179
+ }
180
+
181
+ mMediaRecorder.prepare();
182
+
183
+ }
184
+
185
+
186
+
187
+
188
+
189
+ public String getVideoFilePathy() {
190
+
191
+
192
+
193
+ Log.e(TAG,"getVideoFilePahy");
194
+
195
+ //もしmovieがDownload/movie以下に存在しなかったらダウンロード
196
+
197
+ // File filePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "checkMovie");
198
+
199
+ //File[] storedFiles = filePath.listFiles();
200
+
201
+
202
+
203
+ // File filePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "checkMovie");
204
+
205
+ File filePath=Environment.getExternalStorageDirectory();
206
+
207
+ String newfile=filePath.getAbsolutePath();
208
+
209
+ newfile = newfile + System.currentTimeMillis() + ".mp4";
210
+
211
+
212
+
213
+ return newfile;
214
+
215
+
216
+
217
+ }
218
+
219
+
220
+
221
+ //ここで、pathが表示される
222
+
223
+ //ファイルが、保存されてたら、logを出す。
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+ //この下が多分呼ばれる
232
+
233
+
234
+
235
+ private void stopRecordingVideo() {
236
+
237
+ Log.e(TAG,"stopRecordingVideo");
238
+
239
+ mIsRecordingVideo = false;
240
+
241
+
242
+
243
+ try{
244
+
245
+ mMediaRecorder.stop();
246
+
247
+ }catch(RuntimeException stopException){
248
+
249
+ //例外の例外らしい https://translate.googleusercontent.com/translate_c?depth=1&hl=ja&prev=search&rurl=translate.google.co.jp&sl=en&sp=nmt4&u=https://stackoverflow.com/questions/16221866/mediarecorder-failed-when-i-stop-the-recording&usg=ALkJrhiJDHh9TLeR_qko07QXWEqsYG8hAg
250
+
251
+ }
252
+
253
+ mMediaRecorder.reset();
254
+
255
+ mNextVideoAbsolutePath = null;
256
+
257
+ startPreview();
258
+
259
+ }
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+ public void stop(){
268
+
269
+ Log.e(TAG,"stop()");
270
+
271
+ if (mIsRecordingVideo) {
272
+
273
+ stopRecordingVideo();
274
+
275
+ }
276
+
277
+ }
278
+
279
+
82
280
 
83
281
 
84
282
 
@@ -86,314 +284,122 @@
86
284
 
87
285
  @Override
88
286
 
89
- public int onStartCommand(Intent intent, int flags, int startId) {
287
+ public void onDestroy() {
90
-
288
+
91
- Log.d(TAG, "onStartCommand");
289
+ Log.e(TAG,"onDestroy()");
290
+
92
-
291
+ // windowからViewを排除する
292
+
93
-
293
+ windowManager.removeView(views);
294
+
295
+
296
+
94
-
297
+ //onPause()のやつ2つ
298
+
299
+ closeCamera();
300
+
95
- startBackgroundThread();
301
+ stopBackgroundThread();
302
+
303
+
304
+
96
-
305
+ // 削除作業を継続させる
306
+
97
- openCamera();
307
+ super.onDestroy();
98
-
99
- return super.onStartCommand(intent, flags, startId);
308
+
100
-
101
- }
309
+ }
310
+
311
+
312
+
102
-
313
+ @Override
103
-
104
-
314
+
105
- private void startBackgroundThread() {
315
+ public IBinder onBind(Intent intent) {
106
-
316
+
107
- Log.e(TAG,"startBackgroundThread()");
317
+ Log.e(TAG, "onBind");
108
-
109
- mBackgroundThread = new HandlerThread("CameraBackground");
318
+
110
-
111
- mBackgroundThread.start();
319
+ return null;
112
-
113
- mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
320
+
114
-
115
- }
321
+ }
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
- @SuppressWarnings("MissingPermission")
322
+
124
-
323
+
324
+
325
+
326
+
327
+
328
+
125
- private void openCamera() {
329
+ private void closeCamera() {
126
-
127
-
128
-
330
+
129
- Log.e(TAG,"openCamera");
331
+ Log.e(TAG,"closeCamera()");
130
-
131
- CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
132
332
 
133
333
  try {
134
334
 
135
- Log.d(TAG, "tryAcquire");
136
-
137
- if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
335
+ mCameraOpenCloseLock.acquire();
336
+
138
-
337
+ closePreviewSession();
338
+
139
- throw new RuntimeException("Time out waiting to lock camera opening.");
339
+ if (null != mCameraDevice) {
340
+
140
-
341
+ mCameraDevice.close();
342
+
343
+ mCameraDevice = null;
344
+
141
- }
345
+ }
142
-
143
- String cameraId = manager.getCameraIdList()[0];
346
+
144
-
145
-
146
-
147
- CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
148
-
149
- StreamConfigurationMap map = characteristics
150
-
151
- .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
152
-
153
- mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
154
-
155
- if (map == null) {
347
+ if (null != mMediaRecorder) {
156
-
348
+
157
- throw new RuntimeException("Cannot get available preview/video sizes");
349
+ mMediaRecorder.release();
350
+
158
-
351
+ mMediaRecorder = null;
352
+
159
- }
353
+ }
160
-
161
-
162
-
163
- mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
164
-
165
- mMediaRecorder = new MediaRecorder();
166
-
167
- manager.openCamera(cameraId, mStateCallback, null);
168
-
169
-
170
-
171
- } catch (CameraAccessException e) {
172
-
173
- Log.e(TAG,"CameraAccessException e");
174
-
175
- } catch (NullPointerException e) {
176
-
177
- Log.e(TAG,"NullPointerException e");
178
354
 
179
355
  } catch (InterruptedException e) {
180
356
 
181
- throw new RuntimeException("Interrupted while trying to lock camera opening.");
357
+ throw new RuntimeException("Interrupted while trying to lock camera closing.");
182
-
183
- }
358
+
184
-
185
- }
186
-
187
-
188
-
189
-
190
-
191
- private Integer mSensorOrientation;
192
-
193
- private String mNextVideoAbsolutePath;
194
-
195
- private CaptureRequest.Builder mPreviewBuilder;
196
-
197
-
198
-
199
-
200
-
201
- private static Size chooseVideoSize(Size[] choices) {
202
-
203
- Log.e(TAG,"chooseVideoSize");
204
-
205
- for (Size size : choices) {
206
-
207
- if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
208
-
209
- return size;
359
+ } finally {
210
-
211
- }
212
-
213
- }
214
-
215
- Log.e(TAG, "Couldn't find any suitable video size");
216
-
217
- return choices[choices.length - 1];
218
-
219
- }
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
- private void startPreview() {
228
-
229
- Log.e(TAG,"startPreview");
230
-
231
- if (null == mCameraDevice) {
232
-
233
- return;
234
-
235
- }
236
-
237
- closePreviewSession();
238
-
239
- }
240
-
241
-
242
-
243
-
244
-
245
- private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
246
-
247
-
248
-
249
- @Override
250
-
251
- public void onOpened(@NonNull CameraDevice cameraDevice) {
252
-
253
- Log.e(TAG, "onOpened");
254
-
255
- mCameraDevice = cameraDevice;
256
-
257
- startPreview();
258
360
 
259
361
  mCameraOpenCloseLock.release();
260
362
 
261
- //ここでクラス変数に値を入れて、その値が、何かで、これを呼ぶかどうかを決める
262
-
263
- if (firstOpened.equals("first")) {
264
-
265
- Log.e(TAG, "firstOpend");
266
-
267
- startRecordingVideo();
268
-
269
- firstOpened = "second";
270
-
271
-
272
-
273
- //ここで、stop処理をしないといけない
274
-
275
- Log.e(TAG, "STOPサセルーーーーーーーー");
276
-
277
- stop();
278
-
279
- mMediaRecorder.stop();
280
-
281
- mMediaRecorder.reset();
282
-
283
- }else{
284
-
285
- Log.e(TAG, "secondOpened");
286
-
287
- Timer mTimer = new Timer();
288
-
289
- handler = new Handler();
290
-
291
- mTimer.schedule(new TimerTask() {
292
-
293
- @Override
294
-
295
- public void run() {
296
-
297
- // mHandlerを通じてUI Threadへ処理をキューイング
298
-
299
- handler.post(new Runnable() {
300
-
301
- public void run() {
302
-
303
- Log.e(TAG, "STOPサセルーーーーーーーー");
304
-
305
- stop();
306
-
307
- mMediaRecorder.stop();
308
-
309
- mMediaRecorder.reset();
310
-
311
- //これは、302ぐらいのコードを持ってきた奴、nullでエラーになるから、かえる
312
-
313
- //これでは、MediaRecorderを初期状態にできてない
314
-
315
- // if (null != mMediaRecorder) {
316
-
317
- // mMediaRecorder.release();
318
-
319
- // mMediaRecorder = null;
320
-
321
- // }
322
-
323
- startRecordingVideo();
324
-
325
- }
326
-
327
- });
328
-
329
- }
330
-
331
- }, 10000, 10000);
332
-
333
- }
334
-
335
- }
336
-
337
-
338
-
339
- private void closePreviewSession() {
340
-
341
- Log.e(TAG,"closePreviewSession()");
342
-
343
- if (mPreviewSession != null) {
344
-
345
- mPreviewSession.close();
346
-
347
- mPreviewSession = null;
348
-
349
- }
350
-
351
- }
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
- @Override
362
-
363
- public void onDisconnected(@NonNull CameraDevice cameraDevice) {
364
-
365
- Log.e(TAG,"onDisConnected");
366
-
367
- mCameraOpenCloseLock.release();
368
-
369
- cameraDevice.close();
370
-
371
- mCameraDevice = null;
372
-
373
- }
374
-
375
-
376
-
377
- @Override
378
-
379
- public void onError(@NonNull CameraDevice cameraDevice, int error) {
380
-
381
- Log.e(TAG,"onError");
382
-
383
- mCameraOpenCloseLock.release();
384
-
385
- cameraDevice.close();
386
-
387
- mCameraDevice = null;
388
-
389
- }
390
-
391
-
392
-
393
- };
363
+ }
364
+
365
+ }
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+ private void stopBackgroundThread() {
374
+
375
+ Log.e(TAG,"stopBackgroundThread");
376
+
377
+ mBackgroundThread.quitSafely();
378
+
379
+ try {
380
+
381
+ mBackgroundThread.join();
382
+
383
+ mBackgroundThread = null;
384
+
385
+ mBackgroundHandler = null;
386
+
387
+ } catch (InterruptedException e) {
388
+
389
+ e.printStackTrace();
390
+
391
+ }
392
+
393
+ }
394
+
395
+
396
+
397
+
398
+
399
+ }
394
400
 
395
401
 
396
402
 
397
403
  ```
398
404
 
399
- [メインの質問のURL](https://teratail.com/questions/87492)
405
+ [前半コードと質問のURL](https://teratail.com/questions/87492)

2

コードを呼ばれる順番に並べた

2017/08/08 12:32

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,191 @@
4
4
 
5
5
  ```java
6
6
 
7
+
8
+
9
+ public class MyService7 extends Service {
10
+
11
+ public MyService7() {
12
+
13
+ }
14
+
15
+
16
+
17
+ private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
18
+
19
+ private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
20
+
21
+ private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
22
+
23
+ private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();
24
+
25
+ private static final String TAG = "MyService7";
26
+
27
+
28
+
29
+ static {
30
+
31
+ DEFAULT_ORIENTATIONS.append(Surface.ROTATION_0, 90);
32
+
33
+ DEFAULT_ORIENTATIONS.append(Surface.ROTATION_90, 0);
34
+
35
+ DEFAULT_ORIENTATIONS.append(Surface.ROTATION_180, 270);
36
+
37
+ DEFAULT_ORIENTATIONS.append(Surface.ROTATION_270, 180);
38
+
39
+ }
40
+
41
+
42
+
43
+ static {
44
+
45
+ INVERSE_ORIENTATIONS.append(Surface.ROTATION_0, 270);
46
+
47
+ INVERSE_ORIENTATIONS.append(Surface.ROTATION_90, 180);
48
+
49
+ INVERSE_ORIENTATIONS.append(Surface.ROTATION_180, 90);
50
+
51
+ INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0);
52
+
53
+ }
54
+
55
+
56
+
57
+ private CameraDevice mCameraDevice;
58
+
59
+ private CameraCaptureSession mPreviewSession;
60
+
61
+ private Size mVideoSize;
62
+
63
+ private MediaRecorder mMediaRecorder;
64
+
65
+ private boolean mIsRecordingVideo;
66
+
67
+ private HandlerThread mBackgroundThread;
68
+
69
+ private Handler mBackgroundHandler;
70
+
71
+ private Semaphore mCameraOpenCloseLock = new Semaphore(1);
72
+
73
+
74
+
75
+ Handler handler;
76
+
77
+ WindowManager windowManager;
78
+
79
+ View views;
80
+
81
+ String firstOpened="first";
82
+
83
+
84
+
85
+
86
+
87
+ @Override
88
+
89
+ public int onStartCommand(Intent intent, int flags, int startId) {
90
+
91
+ Log.d(TAG, "onStartCommand");
92
+
93
+
94
+
95
+ startBackgroundThread();
96
+
97
+ openCamera();
98
+
99
+ return super.onStartCommand(intent, flags, startId);
100
+
101
+ }
102
+
103
+
104
+
105
+ private void startBackgroundThread() {
106
+
107
+ Log.e(TAG,"startBackgroundThread()");
108
+
109
+ mBackgroundThread = new HandlerThread("CameraBackground");
110
+
111
+ mBackgroundThread.start();
112
+
113
+ mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
114
+
115
+ }
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+ @SuppressWarnings("MissingPermission")
124
+
125
+ private void openCamera() {
126
+
127
+
128
+
129
+ Log.e(TAG,"openCamera");
130
+
131
+ CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
132
+
133
+ try {
134
+
135
+ Log.d(TAG, "tryAcquire");
136
+
137
+ if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
138
+
139
+ throw new RuntimeException("Time out waiting to lock camera opening.");
140
+
141
+ }
142
+
143
+ String cameraId = manager.getCameraIdList()[0];
144
+
145
+
146
+
147
+ CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
148
+
149
+ StreamConfigurationMap map = characteristics
150
+
151
+ .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
152
+
153
+ mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
154
+
155
+ if (map == null) {
156
+
157
+ throw new RuntimeException("Cannot get available preview/video sizes");
158
+
159
+ }
160
+
161
+
162
+
163
+ mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
164
+
165
+ mMediaRecorder = new MediaRecorder();
166
+
167
+ manager.openCamera(cameraId, mStateCallback, null);
168
+
169
+
170
+
171
+ } catch (CameraAccessException e) {
172
+
173
+ Log.e(TAG,"CameraAccessException e");
174
+
175
+ } catch (NullPointerException e) {
176
+
177
+ Log.e(TAG,"NullPointerException e");
178
+
179
+ } catch (InterruptedException e) {
180
+
181
+ throw new RuntimeException("Interrupted while trying to lock camera opening.");
182
+
183
+ }
184
+
185
+ }
186
+
187
+
188
+
189
+
190
+
7
- private Integer mSensorOrientation;
191
+ private Integer mSensorOrientation;
8
192
 
9
193
  private String mNextVideoAbsolutePath;
10
194
 
@@ -12,6 +196,8 @@
12
196
 
13
197
 
14
198
 
199
+
200
+
15
201
  private static Size chooseVideoSize(Size[] choices) {
16
202
 
17
203
  Log.e(TAG,"chooseVideoSize");
@@ -26,6 +212,8 @@
26
212
 
27
213
  }
28
214
 
215
+ Log.e(TAG, "Couldn't find any suitable video size");
216
+
29
217
  return choices[choices.length - 1];
30
218
 
31
219
  }
@@ -36,219 +224,173 @@
36
224
 
37
225
 
38
226
 
39
- private void startBackgroundThread() {
40
-
41
- mBackgroundThread = new HandlerThread("CameraBackground");
42
-
43
- mBackgroundThread.start();
44
-
45
- mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
46
-
47
- }
48
-
49
-
50
-
51
- private void stopBackgroundThread() {
52
-
53
- mBackgroundThread.quitSafely();
54
-
55
- mBackgroundThread.join();
56
-
57
- mBackgroundThread = null;
58
-
59
- mBackgroundHandler = null;
60
-
61
- }
62
-
63
-
64
-
65
-
66
-
67
- private void setUpMediaRecorder() throws IOException {
68
-
69
-
70
-
71
- mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
72
-
73
- mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
74
-
75
- mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
76
-
77
- if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
78
-
79
- mNextVideoAbsolutePath = getVideoFilePathy();
80
-
81
- }
82
-
83
-
84
-
85
- mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
86
-
87
- mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
88
-
89
- mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
90
-
91
- mMediaRecorder.setVideoEncodingBitRate(10000000);
92
-
93
- mMediaRecorder.setVideoFrameRate(30);
94
-
95
- mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
96
-
97
- mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
98
-
99
- mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
100
-
101
- WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
102
-
103
- int rotation = windowManager.getDefaultDisplay().getRotation();
104
-
105
- switch (mSensorOrientation) {
106
-
107
- case SENSOR_ORIENTATION_DEFAULT_DEGREES:
108
-
109
- mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
110
-
111
- break;
112
-
113
- case SENSOR_ORIENTATION_INVERSE_DEGREES:
114
-
115
- mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
116
-
117
- break;
118
-
119
- }
120
-
121
- mMediaRecorder.prepare();
122
-
123
- }
124
-
125
-
126
-
127
- public String getVideoFilePathy() {
128
-
129
-
130
-
131
- getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "checkMovie");
132
-
133
- File filePath=Environment.getExternalStorageDirectory();
134
-
135
- String newfile=filePath.getAbsolutePath();
136
-
137
- newfile = newfile + System.currentTimeMillis() + ".mp4";
138
-
139
-
140
-
141
- return newfile;
142
-
143
-
144
-
145
- }
146
-
147
-
148
-
149
-
150
-
151
- private void startRecordingVideo() {
227
+ private void startPreview() {
228
+
229
+ Log.e(TAG,"startPreview");
152
230
 
153
231
  if (null == mCameraDevice) {
154
232
 
155
- Log.e(TAG,"return");
156
-
157
233
  return;
158
234
 
159
235
  }
160
236
 
161
- try {
162
-
163
- closePreviewSession();
164
-
165
- setUpMediaRecorder();
166
-
167
- mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
168
-
169
- List<Surface> surfaces = new ArrayList<>();
170
-
171
-
172
-
173
- Surface recorderSurface = mMediaRecorder.getSurface();
174
-
175
- surfaces.add(recorderSurface);
176
-
177
- mPreviewBuilder.addTarget(recorderSurface);
178
-
179
- mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
180
-
181
-
182
-
183
-
184
-
185
- @Override
186
-
187
- public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
188
-
189
- Log.e(TAG,"CameraCaptureSession:onCongigured");
190
-
191
- mPreviewSession = cameraCaptureSession;
192
-
193
- mBackgroundHandler.post(new Runnable() {
194
-
195
- @Override
196
-
197
- public void run() {
198
-
199
- mIsRecordingVideo = true;
200
-
201
- mMediaRecorder.start();
202
-
203
- }
204
-
205
- });
206
-
207
- }
208
-
209
-
210
-
211
- @Override
212
-
213
- public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
214
-
215
- Log.e(TAG,"onConfigureFailed");
216
-
217
- }
218
-
219
- }, mBackgroundHandler);
220
-
221
- } catch (CameraAccessException | IOException e) {
222
-
223
- e.printStackTrace();
224
-
225
- }
226
-
227
-
228
-
229
- }
230
-
231
-
232
-
233
- private void closePreviewSession() {
234
-
235
- Log.e(TAG,"closePreviewSession()");
236
-
237
- if (mPreviewSession != null) {
238
-
239
- mPreviewSession.close();
240
-
241
- mPreviewSession = null;
242
-
243
- }
244
-
245
- }
246
-
247
-
248
-
249
-
250
-
251
-
237
+ closePreviewSession();
238
+
239
+ }
240
+
241
+
242
+
243
+
244
+
245
+ private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
246
+
247
+
248
+
249
+ @Override
250
+
251
+ public void onOpened(@NonNull CameraDevice cameraDevice) {
252
+
253
+ Log.e(TAG, "onOpened");
254
+
255
+ mCameraDevice = cameraDevice;
256
+
257
+ startPreview();
258
+
259
+ mCameraOpenCloseLock.release();
260
+
261
+ //ここでクラス変数に値を入れて、その値が、何かで、これを呼ぶかどうかを決める
262
+
263
+ if (firstOpened.equals("first")) {
264
+
265
+ Log.e(TAG, "firstOpend");
266
+
267
+ startRecordingVideo();
268
+
269
+ firstOpened = "second";
270
+
271
+
272
+
273
+ //ここで、stop処理をしないといけない
274
+
275
+ Log.e(TAG, "STOPサセルーーーーーーーー");
276
+
277
+ stop();
278
+
279
+ mMediaRecorder.stop();
280
+
281
+ mMediaRecorder.reset();
282
+
283
+ }else{
284
+
285
+ Log.e(TAG, "secondOpened");
286
+
287
+ Timer mTimer = new Timer();
288
+
289
+ handler = new Handler();
290
+
291
+ mTimer.schedule(new TimerTask() {
292
+
293
+ @Override
294
+
295
+ public void run() {
296
+
297
+ // mHandlerを通じてUI Threadへ処理をキューイング
298
+
299
+ handler.post(new Runnable() {
300
+
301
+ public void run() {
302
+
303
+ Log.e(TAG, "STOPサセルーーーーーーーー");
304
+
305
+ stop();
306
+
307
+ mMediaRecorder.stop();
308
+
309
+ mMediaRecorder.reset();
310
+
311
+ //これは、302ぐらいのコードを持ってきた奴、nullでエラーになるから、かえる
312
+
313
+ //これでは、MediaRecorderを初期状態にできてない
314
+
315
+ // if (null != mMediaRecorder) {
316
+
317
+ // mMediaRecorder.release();
318
+
319
+ // mMediaRecorder = null;
320
+
321
+ // }
322
+
323
+ startRecordingVideo();
324
+
325
+ }
326
+
327
+ });
328
+
329
+ }
330
+
331
+ }, 10000, 10000);
332
+
333
+ }
334
+
335
+ }
336
+
337
+
338
+
339
+ private void closePreviewSession() {
340
+
341
+ Log.e(TAG,"closePreviewSession()");
342
+
343
+ if (mPreviewSession != null) {
344
+
345
+ mPreviewSession.close();
346
+
347
+ mPreviewSession = null;
348
+
349
+ }
350
+
351
+ }
352
+
353
+
354
+
355
+
356
+
357
+
358
+
359
+
360
+
361
+ @Override
362
+
363
+ public void onDisconnected(@NonNull CameraDevice cameraDevice) {
364
+
365
+ Log.e(TAG,"onDisConnected");
366
+
367
+ mCameraOpenCloseLock.release();
368
+
369
+ cameraDevice.close();
370
+
371
+ mCameraDevice = null;
372
+
373
+ }
374
+
375
+
376
+
377
+ @Override
378
+
379
+ public void onError(@NonNull CameraDevice cameraDevice, int error) {
380
+
381
+ Log.e(TAG,"onError");
382
+
383
+ mCameraOpenCloseLock.release();
384
+
385
+ cameraDevice.close();
386
+
387
+ mCameraDevice = null;
388
+
389
+ }
390
+
391
+
392
+
393
+ };
252
394
 
253
395
 
254
396
 

1

説明の追加

2017/08/08 12:26

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -253,3 +253,5 @@
253
253
 
254
254
 
255
255
  ```
256
+
257
+ [メインの質問のURL](https://teratail.com/questions/87492)