質問編集履歴

9

説明の追加

2017/08/08 22:08

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,9 @@
18
18
 
19
19
  コードが書ききれなかったので、こちらに他のメソッドがあります。
20
20
 
21
- [このコードの後半のコード](https://teratail.com/questions/87493?modal=q-comp)
21
+ [このコードの後半のコードのURL](https://teratail.com/questions/87493?modal=q-comp)
22
+
23
+ //全体の流れ
22
24
 
23
25
  ```java
24
26
 
@@ -86,7 +88,7 @@
86
88
 
87
89
  ```
88
90
 
89
-
91
+ //後半のコード
90
92
 
91
93
  ```java
92
94
 

8

ずれの修正

2017/08/08 22:08

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
 
6
6
 
7
+
8
+
7
9
  しかし、動画を撮影してフォルダに保存して、MediaRecorderをリセットしたり、stopさせるところでエラーが出ます。
8
10
 
9
11
  検索したところ、このようなサイトが見つかったのですが、
@@ -12,6 +14,12 @@
12
14
 
13
15
  何故か、エラーが出てしまいます。
14
16
 
17
+
18
+
19
+ コードが書ききれなかったので、こちらに他のメソッドがあります。
20
+
21
+ [このコードの後半のコード](https://teratail.com/questions/87493?modal=q-comp)
22
+
15
23
  ```java
16
24
 
17
25
  D/MyService7: onStartCommand
@@ -359,9 +367,3 @@
359
367
 
360
368
 
361
369
  ```
362
-
363
-
364
-
365
- ### コードが書ききれなかったので、こちらに他のメソッドがあります。
366
-
367
- [このコードの後半のコード](https://teratail.com/questions/87493?modal=q-comp)

7

コードの変更

2017/08/08 22:07

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -84,331 +84,275 @@
84
84
 
85
85
 
86
86
 
87
- public class MyService7 extends Service {
87
+ public class MyService6 extends Service {
88
+
89
+
90
+
88
-
91
+ @Override
92
+
93
+ public int onStartCommand(Intent intent, int flags, int startId) {
94
+
95
+ Log.d(TAG, "onStartCommand");
96
+
97
+
98
+
89
- public MyService7() {
99
+ startBackgroundThread();
100
+
101
+ openCamera();
102
+
103
+ return super.onStartCommand(intent, flags, startId);
90
104
 
91
105
  }
92
106
 
93
107
 
94
108
 
95
- private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
109
+ private void startBackgroundThread() {
96
-
110
+
97
- private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
111
+ Log.e(TAG,"startBackgroundThread()");
98
-
99
- private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
112
+
100
-
101
- private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();
102
-
103
- private static final String TAG = "MyService7";
113
+ mBackgroundThread = new HandlerThread("CameraBackground");
104
-
105
-
106
-
107
- static {
114
+
108
-
109
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_0, 90);
115
+ mBackgroundThread.start();
110
-
111
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_90, 0);
116
+
112
-
113
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_180, 270);
117
+ mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
114
-
115
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_270, 180);
116
118
 
117
119
  }
118
120
 
119
121
 
120
122
 
123
+
124
+
125
+ @SuppressWarnings("MissingPermission")
126
+
127
+ private void openCamera() {
128
+
129
+
130
+
131
+ Log.e(TAG,"openCamera");
132
+
133
+ CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
134
+
121
- static {
135
+ try {
136
+
122
-
137
+ Log.d(TAG, "tryAcquire");
138
+
139
+ if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
140
+
141
+ throw new RuntimeException("Time out waiting to lock camera opening.");
142
+
143
+ }
144
+
145
+ String cameraId = manager.getCameraIdList()[0];
146
+
147
+
148
+
149
+ CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
150
+
151
+ StreamConfigurationMap map = characteristics
152
+
153
+ .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
154
+
155
+ mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
156
+
157
+ if (map == null) {
158
+
159
+ throw new RuntimeException("Cannot get available preview/video sizes");
160
+
161
+ }
162
+
163
+
164
+
165
+ mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
166
+
167
+ mMediaRecorder = new MediaRecorder();
168
+
123
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_0, 270);
169
+ manager.openCamera(cameraId, mStateCallback, null);
170
+
171
+
172
+
124
-
173
+ } catch (CameraAccessException e) {
174
+
125
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_90, 180);
175
+ Log.e(TAG,"CameraAccessException e");
126
-
176
+
127
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_180, 90);
177
+ } catch (NullPointerException e) {
128
-
178
+
129
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0);
179
+ Log.e(TAG,"NullPointerException e");
180
+
181
+ } catch (InterruptedException e) {
182
+
183
+ throw new RuntimeException("Interrupted while trying to lock camera opening.");
184
+
185
+ }
130
186
 
131
187
  }
132
188
 
133
189
 
134
190
 
135
- private CameraDevice mCameraDevice;
191
+
136
-
137
- private CameraCaptureSession mPreviewSession;
192
+
138
-
139
- private Size mVideoSize;
140
-
141
- private MediaRecorder mMediaRecorder;
193
+ private static Size chooseVideoSize(Size[] choices) {
142
-
143
- private boolean mIsRecordingVideo;
194
+
144
-
145
- private HandlerThread mBackgroundThread;
146
-
147
- private Handler mBackgroundHandler;
148
-
149
- private Semaphore mCameraOpenCloseLock = new Semaphore(1);
150
-
151
-
152
-
153
- Handler handler;
154
-
155
- WindowManager windowManager;
156
-
157
- View views;
158
-
159
- String firstOpened="first";
160
-
161
-
162
-
163
-
164
-
165
- @Override
166
-
167
- public int onStartCommand(Intent intent, int flags, int startId) {
168
-
169
- Log.d(TAG, "onStartCommand");
195
+ Log.e(TAG,"chooseVideoSize");
170
-
171
-
172
-
196
+
173
- startBackgroundThread();
197
+ for (Size size : choices) {
198
+
174
-
199
+ if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
200
+
175
- openCamera();
201
+ return size;
202
+
176
-
203
+ }
204
+
205
+ }
206
+
207
+ Log.e(TAG, "Couldn't find any suitable video size");
208
+
177
- return super.onStartCommand(intent, flags, startId);
209
+ return choices[choices.length - 1];
178
210
 
179
211
  }
180
212
 
181
213
 
182
214
 
183
- private void startBackgroundThread() {
184
-
185
- Log.e(TAG,"startBackgroundThread()");
186
-
187
- mBackgroundThread = new HandlerThread("CameraBackground");
188
-
189
- mBackgroundThread.start();
190
-
191
- mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
192
-
193
- }
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
- @SuppressWarnings("MissingPermission")
202
-
203
- private void openCamera() {
204
-
205
-
206
-
207
- Log.e(TAG,"openCamera");
208
-
209
- CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
210
-
211
- try {
212
-
213
- Log.d(TAG, "tryAcquire");
214
-
215
- if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
216
-
217
- throw new RuntimeException("Time out waiting to lock camera opening.");
215
+
216
+
217
+ private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
218
+
219
+
220
+
221
+ @Override
222
+
223
+ public void onOpened(@NonNull CameraDevice cameraDevice) {
224
+
225
+ Log.e(TAG,"onOpened");
226
+
227
+ mCameraDevice = cameraDevice;
228
+
229
+ // startPreview();
230
+
231
+ mCameraOpenCloseLock.release();
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+ if (firstOpened.equals("first")) {
240
+
241
+ Log.e(TAG, "firstOpend");
242
+
243
+ startRecordingVideo();
244
+
245
+ firstOpened = "second";
246
+
247
+ Timer mTimer = new Timer();
248
+
249
+ handler = new Handler();
250
+
251
+ mTimer.schedule(new TimerTask() {
252
+
253
+ @Override
254
+
255
+ public void run() {
256
+
257
+ // mHandlerを通じてUI Threadへ処理をキューイング
258
+
259
+ handler.post(new Runnable() {
260
+
261
+ public void run() {
262
+
263
+ stop();
264
+
265
+ //Log.e(TAG,"再start");
266
+
267
+ // startRecordingVideo();
268
+
269
+ }
270
+
271
+ });
272
+
273
+ }
274
+
275
+ }, 10000, 10000);
276
+
277
+
278
+
279
+ }else {
280
+
281
+ Log.e(TAG, "secondOpened");
282
+
283
+ Timer mTimer = new Timer();
284
+
285
+ handler = new Handler();
286
+
287
+ mTimer.schedule(new TimerTask() {
288
+
289
+ @Override
290
+
291
+ public void run() {
292
+
293
+ // mHandlerを通じてUI Threadへ処理をキューイング
294
+
295
+ handler.post(new Runnable() {
296
+
297
+ public void run() {
298
+
299
+ stop();
300
+
301
+ //Log.e(TAG,"再start");
302
+
303
+ // startRecordingVideo();
304
+
305
+ }
306
+
307
+ });
308
+
309
+ }
310
+
311
+ }, 10000, 10000);
218
312
 
219
313
  }
220
314
 
221
- String cameraId = manager.getCameraIdList()[0];
315
+
222
-
223
-
224
-
225
- CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
316
+
226
-
227
- StreamConfigurationMap map = characteristics
228
-
229
- .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
230
-
231
- mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
232
-
233
- if (map == null) {
234
-
235
- throw new RuntimeException("Cannot get available preview/video sizes");
236
-
237
- }
317
+ }
238
-
239
-
240
-
241
- mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
242
-
243
- mMediaRecorder = new MediaRecorder();
244
-
245
- manager.openCamera(cameraId, mStateCallback, null);
246
-
247
-
248
-
249
- } catch (CameraAccessException e) {
250
-
251
- Log.e(TAG,"CameraAccessException e");
252
-
253
- } catch (NullPointerException e) {
254
-
255
- Log.e(TAG,"NullPointerException e");
256
-
257
- } catch (InterruptedException e) {
258
-
259
- throw new RuntimeException("Interrupted while trying to lock camera opening.");
260
-
261
- }
262
-
263
- }
264
-
265
-
266
-
267
-
268
-
269
- private Integer mSensorOrientation;
270
-
271
- private String mNextVideoAbsolutePath;
272
-
273
- private CaptureRequest.Builder mPreviewBuilder;
274
-
275
-
276
-
277
-
278
-
279
- private static Size chooseVideoSize(Size[] choices) {
280
-
281
- Log.e(TAG,"chooseVideoSize");
282
-
283
- for (Size size : choices) {
284
-
285
- if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
286
-
287
- return size;
288
-
289
- }
290
-
291
- }
292
-
293
- Log.e(TAG, "Couldn't find any suitable video size");
294
-
295
- return choices[choices.length - 1];
296
-
297
- }
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
- private void startPreview() {
306
-
307
- Log.e(TAG,"startPreview");
308
-
309
- if (null == mCameraDevice) {
310
-
311
- return;
312
-
313
- }
314
-
315
- closePreviewSession();
316
-
317
- }
318
-
319
-
320
-
321
- private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
322
318
 
323
319
 
324
320
 
325
321
  @Override
326
322
 
327
- public void onOpened(@NonNull CameraDevice cameraDevice) {
323
+ public void onDisconnected(@NonNull CameraDevice cameraDevice) {
328
-
324
+
329
- Log.e(TAG, "onOpened");
325
+ Log.e(TAG,"onDisConnected");
330
-
331
- mCameraDevice = cameraDevice;
332
-
333
- startPreview();
334
326
 
335
327
  mCameraOpenCloseLock.release();
336
328
 
337
- //ここでクラス変数に値を入れて、その値が、何かで、これを呼ぶかどうかを決める
338
-
339
- if (firstOpened.equals("first")) {
340
-
341
- Log.e(TAG, "firstOpend");
342
-
343
- startRecordingVideo();
344
-
345
- firstOpened = "second";
346
-
347
-
348
-
349
- //ここで、stop処理をしないといけない
350
-
351
- Log.e(TAG, "STOPサセルーーーーーーーー");
352
-
353
- stop();
354
-
355
- mMediaRecorder.stop();
356
-
357
- mMediaRecorder.reset();
329
+ cameraDevice.close();
358
-
359
- }else{
330
+
360
-
361
- Log.e(TAG, "secondOpened");
362
-
363
- Timer mTimer = new Timer();
331
+ mCameraDevice = null;
364
-
332
+
365
- handler = new Handler();
333
+ }
366
-
367
- mTimer.schedule(new TimerTask() {
334
+
368
-
335
+
336
+
369
- @Override
337
+ @Override
370
-
371
- public void run() {
338
+
372
-
373
- // mHandlerを通じてUI Threadへ処理をキューイング
339
+ public void onError(@NonNull CameraDevice cameraDevice, int error) {
374
-
375
- handler.post(new Runnable() {
340
+
376
-
377
- public void run() {
378
-
379
- Log.e(TAG, "STOPサセルーーーーーーーー");
341
+ Log.e(TAG,"onError");
380
-
381
- stop();
342
+
382
-
383
- mMediaRecorder.stop();
343
+ mCameraOpenCloseLock.release();
384
-
344
+
385
- mMediaRecorder.reset();
345
+ cameraDevice.close();
386
-
387
- //これは、302ぐらいのコードを持ってきた奴、nullでエラーになるから、かえる
346
+
388
-
389
- //これでは、MediaRecorderを初期状態にできてない
390
-
391
- // if (null != mMediaRecorder) {
392
-
393
- // mMediaRecorder.release();
394
-
395
- // mMediaRecorder = null;
396
-
397
- // }
398
-
399
- startRecordingVideo();
347
+ mCameraDevice = null;
400
-
348
+
401
- }
349
+ }
402
-
350
+
351
+
352
+
403
- });
353
+ };
404
-
405
- }
354
+
406
-
407
- }, 10000, 10000);
355
+
408
-
409
- }
410
-
411
- }
412
356
 
413
357
 
414
358
 

6

コードの修正

2017/08/08 22:01

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -318,6 +318,98 @@
318
318
 
319
319
 
320
320
 
321
+ private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
322
+
323
+
324
+
325
+ @Override
326
+
327
+ public void onOpened(@NonNull CameraDevice cameraDevice) {
328
+
329
+ Log.e(TAG, "onOpened");
330
+
331
+ mCameraDevice = cameraDevice;
332
+
333
+ startPreview();
334
+
335
+ mCameraOpenCloseLock.release();
336
+
337
+ //ここでクラス変数に値を入れて、その値が、何かで、これを呼ぶかどうかを決める
338
+
339
+ if (firstOpened.equals("first")) {
340
+
341
+ Log.e(TAG, "firstOpend");
342
+
343
+ startRecordingVideo();
344
+
345
+ firstOpened = "second";
346
+
347
+
348
+
349
+ //ここで、stop処理をしないといけない
350
+
351
+ Log.e(TAG, "STOPサセルーーーーーーーー");
352
+
353
+ stop();
354
+
355
+ mMediaRecorder.stop();
356
+
357
+ mMediaRecorder.reset();
358
+
359
+ }else{
360
+
361
+ Log.e(TAG, "secondOpened");
362
+
363
+ Timer mTimer = new Timer();
364
+
365
+ handler = new Handler();
366
+
367
+ mTimer.schedule(new TimerTask() {
368
+
369
+ @Override
370
+
371
+ public void run() {
372
+
373
+ // mHandlerを通じてUI Threadへ処理をキューイング
374
+
375
+ handler.post(new Runnable() {
376
+
377
+ public void run() {
378
+
379
+ Log.e(TAG, "STOPサセルーーーーーーーー");
380
+
381
+ stop();
382
+
383
+ mMediaRecorder.stop();
384
+
385
+ mMediaRecorder.reset();
386
+
387
+ //これは、302ぐらいのコードを持ってきた奴、nullでエラーになるから、かえる
388
+
389
+ //これでは、MediaRecorderを初期状態にできてない
390
+
391
+ // if (null != mMediaRecorder) {
392
+
393
+ // mMediaRecorder.release();
394
+
395
+ // mMediaRecorder = null;
396
+
397
+ // }
398
+
399
+ startRecordingVideo();
400
+
401
+ }
402
+
403
+ });
404
+
405
+ }
406
+
407
+ }, 10000, 10000);
408
+
409
+ }
410
+
411
+ }
412
+
321
413
 
322
414
 
323
415
 

5

エラーコードの追加

2017/08/08 13:14

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- しかし、2回目の動画撮影を開始したところでエラーが出ます。
7
+ しかし、動画撮影してフォルダに保存して、MediaRecorderリセットしたり、stopさせるところでエラーが出ます。
8
8
 
9
9
  検索したところ、このようなサイトが見つかったのですが、
10
10
 
@@ -12,10 +12,76 @@
12
12
 
13
13
  何故か、エラーが出てしまいます。
14
14
 
15
-
16
-
17
15
  ```java
18
16
 
17
+ D/MyService7: onStartCommand
18
+
19
+ E/MyService7: startBackgroundThread()
20
+
21
+ E/MyService7: openCamera
22
+
23
+ D/MyService7: tryAcquire
24
+
25
+ E/MyService7: chooseVideoSize
26
+
27
+ E/MyService7: onOpened
28
+
29
+ E/MyService7: startPreview
30
+
31
+ E/MyService7: closePreviewSession
32
+
33
+ E/MyService7: firstOpend
34
+
35
+ E/MyService7: startRecordingVideo()
36
+
37
+ E/MyService7: try
38
+
39
+ E/MyService7: closePreviewSession
40
+
41
+ E/MyService7: setUpMediaRecorder
42
+
43
+ E/MyService7: getVideoFilePath
44
+
45
+ E/MyService7: /storage/emulated/0/Android/data/com.google.android.exoplayer2.demo/files/1502197772081.mp4
46
+
47
+ E/UncaughtException: java.lang.IllegalStateException
48
+
49
+ at android.media.MediaRecorder._setAudioSource(Native Method)
50
+
51
+ at android.media.MediaRecorder.setAudioSource(MediaRecorder.java:417)
52
+
53
+ at com.google.android.exoplayer2.demo.MyService7.setUpMediaRecorder(MyService7.java:308)
54
+
55
+ at com.google.android.exoplayer2.demo.MyService7.startRecordingVideo(MyService7.java:258)
56
+
57
+ at com.google.android.exoplayer2.demo.MyService7.access$300(MyService7.java:47)
58
+
59
+ at com.google.android.exoplayer2.demo.MyService7$1.onOpened(MyService7.java:176)
60
+
61
+ at android.hardware.camera2.impl.CameraDeviceImpl$1.run(CameraDeviceImpl.java:136)
62
+
63
+ at android.os.Handler.handleCallback(Handler.java:739)
64
+
65
+ at android.os.Handler.dispatchMessage(Handler.java:95)
66
+
67
+ at android.os.Looper.loop(Looper.java:148)
68
+
69
+ at android.app.ActivityThread.main(ActivityThread.java:5480)
70
+
71
+ at java.lang.reflect.Method.invoke(Native Method)
72
+
73
+ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
74
+
75
+ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
76
+
77
+
78
+
79
+ ```
80
+
81
+
82
+
83
+ ```java
84
+
19
85
 
20
86
 
21
87
  public class MyService7 extends Service {
@@ -254,157 +320,7 @@
254
320
 
255
321
 
256
322
 
257
- private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
323
+
258
-
259
-
260
-
261
- @Override
262
-
263
- public void onOpened(@NonNull CameraDevice cameraDevice) {
264
-
265
- Log.e(TAG, "onOpened");
266
-
267
- mCameraDevice = cameraDevice;
268
-
269
- startPreview();
270
-
271
- mCameraOpenCloseLock.release();
272
-
273
- //ここでクラス変数に値を入れて、その値が、何かで、これを呼ぶかどうかを決める
274
-
275
- if (firstOpened.equals("first")) {
276
-
277
- Log.e(TAG, "firstOpend");
278
-
279
- startRecordingVideo();
280
-
281
- firstOpened = "second";
282
-
283
-
284
-
285
- //ここで、stop処理をしないといけない
286
-
287
- Log.e(TAG, "STOPサセルーーーーーーーー");
288
-
289
- stop();
290
-
291
- mMediaRecorder.stop();
292
-
293
- mMediaRecorder.reset();
294
-
295
- }else{
296
-
297
- Log.e(TAG, "secondOpened");
298
-
299
- Timer mTimer = new Timer();
300
-
301
- handler = new Handler();
302
-
303
- mTimer.schedule(new TimerTask() {
304
-
305
- @Override
306
-
307
- public void run() {
308
-
309
- // mHandlerを通じてUI Threadへ処理をキューイング
310
-
311
- handler.post(new Runnable() {
312
-
313
- public void run() {
314
-
315
- Log.e(TAG, "STOPサセルーーーーーーーー");
316
-
317
- stop();
318
-
319
- mMediaRecorder.stop();
320
-
321
- mMediaRecorder.reset();
322
-
323
- //これは、302ぐらいのコードを持ってきた奴、nullでエラーになるから、かえる
324
-
325
- //これでは、MediaRecorderを初期状態にできてない
326
-
327
- // if (null != mMediaRecorder) {
328
-
329
- // mMediaRecorder.release();
330
-
331
- // mMediaRecorder = null;
332
-
333
- // }
334
-
335
- startRecordingVideo();
336
-
337
- }
338
-
339
- });
340
-
341
- }
342
-
343
- }, 10000, 10000);
344
-
345
- }
346
-
347
- }
348
-
349
-
350
-
351
- private void closePreviewSession() {
352
-
353
- Log.e(TAG,"closePreviewSession()");
354
-
355
- if (mPreviewSession != null) {
356
-
357
- mPreviewSession.close();
358
-
359
- mPreviewSession = null;
360
-
361
- }
362
-
363
- }
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
- @Override
374
-
375
- public void onDisconnected(@NonNull CameraDevice cameraDevice) {
376
-
377
- Log.e(TAG,"onDisConnected");
378
-
379
- mCameraOpenCloseLock.release();
380
-
381
- cameraDevice.close();
382
-
383
- mCameraDevice = null;
384
-
385
- }
386
-
387
-
388
-
389
- @Override
390
-
391
- public void onError(@NonNull CameraDevice cameraDevice, int error) {
392
-
393
- Log.e(TAG,"onError");
394
-
395
- mCameraOpenCloseLock.release();
396
-
397
- cameraDevice.close();
398
-
399
- mCameraDevice = null;
400
-
401
- }
402
-
403
-
404
-
405
- };
406
-
407
-
408
324
 
409
325
  ```
410
326
 

4

説明の変更

2017/08/08 13:13

投稿

giant
giant

スコア132

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

3

コードを呼び出される順番に変更

2017/08/08 12:31

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -10,272 +10,410 @@
10
10
 
11
11
  [StackOverflow:MediaRecorderインスタンスを再利用する際のIllegalStateException](https://stackoverflow.com/questions/33287621/illegalstateexception-when-reusing-mediarecorder-instance)
12
12
 
13
- 私のコドの場合何を呼び出すことが必要なのでしょうか?
13
+ 何故か、エラてしまいま
14
+
15
+
14
16
 
15
17
  ```java
16
18
 
17
- java.lang.IllegalStateException
18
-
19
- at android.media.MediaRecorder._setAudioSource(Native Method)
20
-
21
- at android.media.MediaRecorder.setAudioSource(MediaRecorder.java:417)
22
-
23
- at com.google.android.exoplayer2.demo.MyService7.setUpMediaRecorder(MyService7.java:321)
24
-
25
- at com.google.android.exoplayer2.demo.MyService7.startRecordingVideo(MyService7.java:430)
26
-
27
- at com.google.android.exoplayer2.demo.MyService7.access$300(MyService7.java:45)
28
-
29
- at com.google.android.exoplayer2.demo.MyService7$1.onOpened(MyService7.java:161)
30
-
31
- at android.hardware.camera2.impl.CameraDeviceImpl$1.run(CameraDeviceImpl.java:136)
32
-
33
- at android.os.Handler.handleCallback(Handler.java:739)
34
-
35
- at android.os.Handler.dispatchMessage(Handler.java:95)
36
-
37
- at android.os.Looper.loop(Looper.java:148)
38
-
39
- at android.app.ActivityThread.main(ActivityThread.java:5480)
40
-
41
- at java.lang.reflect.Method.invoke(Native Method)
42
-
43
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
44
-
45
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
19
+ private void closePreviewSession() {
20
+
21
+ Log.e(TAG,"closePreviewSession");
22
+
23
+ if (mPreviewSession != null) {
24
+
25
+ mPreviewSession.close();
26
+
27
+ mPreviewSession = null;
28
+
29
+ }
30
+
31
+ }
32
+
33
+
34
+
35
+
36
+
37
+ private void startRecordingVideo() {
38
+
39
+ Log.e(TAG,"startRecordingVideo()");
40
+
41
+ if (null == mCameraDevice) {
42
+
43
+ Log.e(TAG,"return");
44
+
45
+ return;
46
+
47
+ }
48
+
49
+ try {
50
+
51
+ Log.e(TAG,"try");
52
+
53
+ closePreviewSession();
54
+
55
+ setUpMediaRecorder();
56
+
57
+ mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
58
+
59
+ List<Surface> surfaces = new ArrayList<>();
60
+
61
+
62
+
63
+ Surface recorderSurface = mMediaRecorder.getSurface();
64
+
65
+ surfaces.add(recorderSurface);
66
+
67
+ mPreviewBuilder.addTarget(recorderSurface);
68
+
69
+ mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
70
+
71
+
72
+
73
+
74
+
75
+ @Override
76
+
77
+ public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
78
+
79
+ Log.e(TAG,"CameraCaptureSession:onCongigured");
80
+
81
+ mPreviewSession = cameraCaptureSession;
82
+
83
+ mBackgroundHandler.post(new Runnable() {
84
+
85
+ @Override
86
+
87
+ public void run() {
88
+
89
+ mIsRecordingVideo = true;
90
+
91
+ mMediaRecorder.start();
92
+
93
+ }
94
+
95
+ });
96
+
97
+ }
98
+
99
+
100
+
101
+ @Override
102
+
103
+ public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
104
+
105
+ Log.e(TAG,"onConfigureFailed");
106
+
107
+ }
108
+
109
+ }, mBackgroundHandler);
110
+
111
+ } catch (CameraAccessException | IOException e) {
112
+
113
+ e.printStackTrace();
114
+
115
+ }
116
+
117
+
118
+
119
+ }
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+ //もう一度closePreviewSession()が呼ばれる
128
+
129
+
130
+
131
+ private void setUpMediaRecorder() throws IOException {
132
+
133
+
134
+
135
+ Log.e(TAG,"setUpMediaRecorder");
136
+
137
+ // mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
138
+
139
+ mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
140
+
141
+ mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
142
+
143
+ if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
144
+
145
+ //mNextVideoAbsolutePath = getVideoFilePath(this);
146
+
147
+ mNextVideoAbsolutePath = getVideoFilePathy();
148
+
149
+ Log.e(TAG,mNextVideoAbsolutePath);
150
+
151
+ }
152
+
153
+
154
+
155
+ mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
156
+
157
+ mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
158
+
159
+ mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
160
+
161
+ mMediaRecorder.setVideoEncodingBitRate(10000000);
162
+
163
+ mMediaRecorder.setVideoFrameRate(30);
164
+
165
+ mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
166
+
167
+ mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
168
+
169
+ mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
170
+
171
+ WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
172
+
173
+ int rotation = windowManager.getDefaultDisplay().getRotation();
174
+
175
+ switch (mSensorOrientation) {
176
+
177
+ case SENSOR_ORIENTATION_DEFAULT_DEGREES:
178
+
179
+ mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
180
+
181
+ break;
182
+
183
+ case SENSOR_ORIENTATION_INVERSE_DEGREES:
184
+
185
+ mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
186
+
187
+ break;
188
+
189
+ }
190
+
191
+ mMediaRecorder.prepare();
192
+
193
+ }
194
+
195
+
196
+
197
+
198
+
199
+ public String getVideoFilePathy() {
200
+
201
+
202
+
203
+ Log.e(TAG,"getVideoFilePahy");
204
+
205
+ //もしmovieがDownload/movie以下に存在しなかったらダウンロード
206
+
207
+ // File filePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "checkMovie");
208
+
209
+ //File[] storedFiles = filePath.listFiles();
210
+
211
+
212
+
213
+ // File filePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "checkMovie");
214
+
215
+ File filePath=Environment.getExternalStorageDirectory();
216
+
217
+ String newfile=filePath.getAbsolutePath();
218
+
219
+ newfile = newfile + System.currentTimeMillis() + ".mp4";
220
+
221
+
222
+
223
+ return newfile;
224
+
225
+
226
+
227
+ }
228
+
229
+
230
+
231
+ //ここで、pathが表示される
232
+
233
+ //ファイルが、保存されてたら、logを出す。
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+ //この下が多分呼ばれる
242
+
243
+
244
+
245
+ private void stopRecordingVideo() {
246
+
247
+ Log.e(TAG,"stopRecordingVideo");
248
+
249
+ mIsRecordingVideo = false;
250
+
251
+
252
+
253
+ try{
254
+
255
+ mMediaRecorder.stop();
256
+
257
+ }catch(RuntimeException stopException){
258
+
259
+ //例外の例外らしい 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
260
+
261
+ }
262
+
263
+ mMediaRecorder.reset();
264
+
265
+ mNextVideoAbsolutePath = null;
266
+
267
+ startPreview();
268
+
269
+ }
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+ public void stop(){
278
+
279
+ Log.e(TAG,"stop()");
280
+
281
+ if (mIsRecordingVideo) {
282
+
283
+ stopRecordingVideo();
284
+
285
+ }
286
+
287
+ }
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+ @Override
296
+
297
+ public void onDestroy() {
298
+
299
+ Log.e(TAG,"onDestroy()");
300
+
301
+ // windowからViewを排除する
302
+
303
+ windowManager.removeView(views);
304
+
305
+
306
+
307
+ //onPause()のやつ2つ
308
+
309
+ closeCamera();
310
+
311
+ stopBackgroundThread();
312
+
313
+
314
+
315
+ // 削除作業を継続させる
316
+
317
+ super.onDestroy();
318
+
319
+ }
320
+
321
+
322
+
323
+ @Override
324
+
325
+ public IBinder onBind(Intent intent) {
326
+
327
+ Log.e(TAG, "onBind");
328
+
329
+ return null;
330
+
331
+ }
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+ private void closeCamera() {
340
+
341
+ Log.e(TAG,"closeCamera()");
342
+
343
+ try {
344
+
345
+ mCameraOpenCloseLock.acquire();
346
+
347
+ closePreviewSession();
348
+
349
+ if (null != mCameraDevice) {
350
+
351
+ mCameraDevice.close();
352
+
353
+ mCameraDevice = null;
354
+
355
+ }
356
+
357
+ if (null != mMediaRecorder) {
358
+
359
+ mMediaRecorder.release();
360
+
361
+ mMediaRecorder = null;
362
+
363
+ }
364
+
365
+ } catch (InterruptedException e) {
366
+
367
+ throw new RuntimeException("Interrupted while trying to lock camera closing.");
368
+
369
+ } finally {
370
+
371
+ mCameraOpenCloseLock.release();
372
+
373
+ }
374
+
375
+ }
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+ private void stopBackgroundThread() {
384
+
385
+ Log.e(TAG,"stopBackgroundThread");
386
+
387
+ mBackgroundThread.quitSafely();
388
+
389
+ try {
390
+
391
+ mBackgroundThread.join();
392
+
393
+ mBackgroundThread = null;
394
+
395
+ mBackgroundHandler = null;
396
+
397
+ } catch (InterruptedException e) {
398
+
399
+ e.printStackTrace();
400
+
401
+ }
402
+
403
+ }
404
+
405
+
406
+
407
+
408
+
409
+ }
410
+
411
+
46
412
 
47
413
  ```
48
414
 
49
-
50
-
51
- ```java
415
+
52
-
53
- public void stop(){
54
-
55
- Log.e(TAG,"stop()");
56
-
57
- if (mIsRecordingVideo) {
58
-
59
- stopRecordingVideo();
60
-
61
- }
62
-
63
- }
64
-
65
-
66
-
67
- private void stopRecordingVideo() {
68
-
69
- mIsRecordingVideo = false;
70
-
71
- mMediaRecorder.stop();
72
-
73
- mMediaRecorder.reset();
74
-
75
- mNextVideoAbsolutePath = null;
76
-
77
- startPreview();
78
-
79
- }
80
-
81
-
82
-
83
- private void startPreview() {
84
-
85
- if (null == mCameraDevice) {
86
-
87
- return;
88
-
89
- }
90
-
91
- closePreviewSession();
92
-
93
- }
94
-
95
-
96
-
97
-
98
-
99
- @Override
100
-
101
- public int onStartCommand(Intent intent, int flags, int startId) {
102
-
103
- startBackgroundThread();
104
-
105
- openCamera();
106
-
107
- return super.onStartCommand(intent, flags, startId);
108
-
109
- }
110
-
111
-
112
-
113
- @Override
114
-
115
- public void onDestroy() {
116
-
117
- super.onDestroy();
118
-
119
- windowManager.removeView(views);
120
-
121
- closeCamera();
122
-
123
- stopBackgroundThread();
124
-
125
- }
126
-
127
-
128
-
129
- private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
130
-
131
-
132
-
133
- @Override
134
-
135
- public void onOpened(@NonNull CameraDevice cameraDevice) {
136
-
137
- mCameraDevice = cameraDevice;
138
-
139
- startPreview();
140
-
141
- mCameraOpenCloseLock.release();
142
-
143
-
144
-
145
- Timer mTimer = new Timer();
146
-
147
- handler=new Handler();
148
-
149
- mTimer.schedule( new TimerTask(){
150
-
151
- @Override
152
-
153
- public void run() {
154
-
155
- handler.post( new Runnable() {
156
-
157
- public void run() {
158
-
159
- stop();
160
-
161
- mMediaRecorder.stop();
162
-
163
- mMediaRecorder.reset();
164
-
165
-
166
-
167
- //これでは、MediaRecorderを初期状態にできてない
168
-
169
- // if (null != mMediaRecorder) {
170
-
171
- // mMediaRecorder.release();
172
-
173
- // mMediaRecorder = null;
174
-
175
- // }
176
-
177
- startRecordingVideo();
178
-
179
- }
180
-
181
- });
182
-
183
- }
184
-
185
- }, 10000, 10000);
186
-
187
- }
188
-
189
-
190
-
191
-
192
-
193
- private void closePreviewSession() {
194
-
195
- Log.e(TAG,"closePreviewSession");
196
-
197
- if (mPreviewSession != null) {
198
-
199
- mPreviewSession.close();
200
-
201
- mPreviewSession = null;
202
-
203
- }
204
-
205
- }
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
- @Override
214
-
215
- public void onDisconnected(@NonNull CameraDevice cameraDevice) {
216
-
217
- Log.e(TAG,"onDisConnected");
218
-
219
- mCameraOpenCloseLock.release();
220
-
221
- cameraDevice.close();
222
-
223
- mCameraDevice = null;
224
-
225
- }
226
-
227
- };
228
-
229
-
230
-
231
- @SuppressWarnings("MissingPermission")
232
-
233
- private void openCamera() {
234
-
235
-
236
-
237
- //mMediaRecorderに関するコード以外、内容を省略
238
-
239
- mMediaRecorder = new MediaRecorder();
240
-
241
- manager.openCamera(cameraId, mStateCallback, null);
242
-
243
- }
244
-
245
-
246
-
247
- private void closeCamera() {
248
-
249
- mCameraOpenCloseLock.acquire();
250
-
251
- closePreviewSession();
252
-
253
- if (null != mCameraDevice) {
254
-
255
- mCameraDevice.close();
256
-
257
- mCameraDevice = null;
258
-
259
- }
260
-
261
- if (null != mMediaRecorder) {
262
-
263
- mMediaRecorder.release();
264
-
265
- mMediaRecorder = null;
266
-
267
- }
268
-
269
- }
270
-
271
- }
272
-
273
-
274
-
275
-
276
-
277
- ```
278
416
 
279
417
  ### コードが書ききれなかったので、こちらに他のメソッドがあります。
280
418
 
281
- [のコード](https://teratail.com/questions/87493?modal=q-comp)
419
+ [のコードの前半のコード](https://teratail.com/questions/87493?modal=q-comp)

2

説明の追加

2017/08/08 12:28

投稿

giant
giant

スコア132

test CHANGED
@@ -1 +1 @@
1
- MediaRecorderインスタンスを再利用する際のIllegalStateExceptionの解決
1
+ (コード)MediaRecorderインスタンスを再利用する際のIllegalStateExceptionの解決
test CHANGED
File without changes

1

説明の追加

2017/08/08 10:11

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -275,3 +275,7 @@
275
275
 
276
276
 
277
277
  ```
278
+
279
+ ### コードが書ききれなかったので、こちらに他のメソッドがあります。
280
+
281
+ [他のコード](https://teratail.com/questions/87493?modal=q-comp)