質問編集履歴

1

スペースをできるだけ無くした

2017/08/10 02:47

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -12,8 +12,6 @@
12
12
 
13
13
  }
14
14
 
15
-
16
-
17
15
  private CameraDevice mCameraDevice;
18
16
 
19
17
  private Size mVideoSize;
@@ -38,16 +36,12 @@
38
36
 
39
37
 
40
38
 
41
-
42
-
43
39
  @Override
44
40
 
45
41
  public int onStartCommand(Intent intent, int flags, int startId) {
46
42
 
47
43
  Log.e(TAG, "onStartCommand flags " + flags + " startId " + startId);
48
44
 
49
-
50
-
51
45
  openCamera();
52
46
 
53
47
  startBackgroundThread();
@@ -56,8 +50,6 @@
56
50
 
57
51
  }
58
52
 
59
-
60
-
61
53
  private void startBackgroundThread() {
62
54
 
63
55
  Log.e(TAG,"startBackgroundThread()");
@@ -72,8 +64,6 @@
72
64
 
73
65
 
74
66
 
75
-
76
-
77
67
  @SuppressWarnings("MissingPermission")
78
68
 
79
69
  private void openCamera() {
@@ -82,8 +72,6 @@
82
72
 
83
73
  CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
84
74
 
85
-
86
-
87
75
  try {
88
76
 
89
77
  Log.d(TAG, "tryAcquire");
@@ -114,16 +102,12 @@
114
102
 
115
103
  }
116
104
 
117
-
118
-
119
105
  mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
120
106
 
121
107
  mMediaRecorder = new MediaRecorder();
122
108
 
123
109
  manager.openCamera(cameraId, mStateCallback, null);
124
110
 
125
-
126
-
127
111
  } catch (CameraAccessException e) {
128
112
 
129
113
  Log.e(TAG, "CameraAccessException e");
@@ -142,8 +126,6 @@
142
126
 
143
127
 
144
128
 
145
-
146
-
147
129
  protected CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
148
130
 
149
131
  @Override
@@ -154,30 +136,20 @@
154
136
 
155
137
  mCameraDevice = cameraDevice;
156
138
 
157
-
158
-
159
139
  //startPreview();
160
140
 
161
141
  mCameraOpenCloseLock.release();
162
142
 
163
-
164
-
165
143
  //とりあえずここから
166
144
 
167
145
  try {
168
146
 
169
147
  setUpMediaRecorder();
170
148
 
171
-
172
-
173
149
  //早すぎるとまずい?それは、stop?
174
150
 
175
151
  startRecordingVideo();
176
152
 
177
-
178
-
179
-
180
-
181
153
  //ここで、10秒後にstopするようにした
182
154
 
183
155
  new Handler().postDelayed(new Runnable() {
@@ -188,34 +160,22 @@
188
160
 
189
161
  // TODO: ここで処理を実行する
190
162
 
191
-
192
-
193
-
194
-
195
163
  //mIsRecordingVideo = true;
196
164
 
197
165
  //mMediaRecorder.start();
198
166
 
199
-
200
-
201
167
  stopRecordingVideo();
202
168
 
203
169
  }
204
170
 
205
171
  }, 10000);
206
172
 
207
-
208
-
209
173
  }catch(Exception e){
210
174
 
211
-
212
-
213
175
  }
214
176
 
215
177
  //これを違うとこから呼び出す startRecordingVideo(); は消す
216
178
 
217
-
218
-
219
179
  }
220
180
 
221
181
  @Override
@@ -250,8 +210,6 @@
250
210
 
251
211
 
252
212
 
253
-
254
-
255
213
  private void startRecordingVideo() {
256
214
 
257
215
  Log.e(TAG, "startRecordingVideo()");
@@ -282,256 +240,216 @@
282
240
 
283
241
 
284
242
 
243
+ private Integer mSensorOrientation;
244
+
245
+ private String mNextVideoAbsolutePath;
246
+
247
+ private CaptureRequest.Builder mPreviewBuilder;
248
+
249
+ private void setUpMediaRecorder() throws IOException {
250
+
251
+ Log.e(TAG,"setUpMediaRecorder"); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
252
+
253
+ if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
254
+
255
+ mNextVideoAbsolutePath = getVideoFilePathy();
256
+
257
+ Log.e(TAG,mNextVideoAbsolutePath);
258
+
259
+ }
260
+
261
+ mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
262
+
263
+ mMediaRecorder.setVideoEncodingBitRate(10000000);
264
+
265
+ mMediaRecorder.setVideoFrameRate(30);
266
+
267
+ mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight()); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
268
+
269
+ WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
270
+
271
+ int rotation = windowManager.getDefaultDisplay().getRotation();
272
+
273
+ switch (mSensorOrientation) {
274
+
275
+ case SENSOR_ORIENTATION_DEFAULT_DEGREES:
276
+
277
+ mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
278
+
279
+ break;
280
+
281
+ case SENSOR_ORIENTATION_INVERSE_DEGREES:
282
+
283
+ mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
284
+
285
+ break;
286
+
287
+ }
288
+
289
+ mMediaRecorder.prepare();
290
+
291
+ }
292
+
293
+ private static Size chooseVideoSize(Size[] choices) {
294
+
295
+ Log.e(TAG,"chooseVideoSize");
296
+
297
+ for (Size size : choices) {
298
+
299
+ if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
300
+
301
+ return size;
302
+
303
+ }
304
+
305
+ }
306
+
307
+ Log.e(TAG, "Couldn't find any suitable video size");
308
+
309
+ return choices[choices.length - 1];
310
+
311
+ }
312
+
285
313
 
286
314
 
287
-
288
-
289
- private Integer mSensorOrientation;
290
-
291
- private String mNextVideoAbsolutePath;
292
-
293
- private CaptureRequest.Builder mPreviewBuilder;
294
-
295
-
296
-
297
- private void setUpMediaRecorder() throws IOException {
298
-
299
- Log.e(TAG,"setUpMediaRecorder");
300
-
301
-
302
-
303
- mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
304
-
305
- mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
306
-
307
- if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
308
-
309
- mNextVideoAbsolutePath = getVideoFilePathy();
310
-
311
- Log.e(TAG,mNextVideoAbsolutePath);
312
-
313
- }
314
-
315
- mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
316
-
317
- mMediaRecorder.setVideoEncodingBitRate(10000000);
318
-
319
- mMediaRecorder.setVideoFrameRate(30);
320
-
321
- mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
322
-
323
- mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
324
-
325
- WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
326
-
327
- int rotation = windowManager.getDefaultDisplay().getRotation();
328
-
329
- switch (mSensorOrientation) {
330
-
331
- case SENSOR_ORIENTATION_DEFAULT_DEGREES:
332
-
333
- mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
334
-
335
- break;
336
-
337
- case SENSOR_ORIENTATION_INVERSE_DEGREES:
338
-
339
- mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
340
-
341
- break;
342
-
343
- }
344
-
345
- mMediaRecorder.prepare();
346
-
347
- }
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
- private static Size chooseVideoSize(Size[] choices) {
358
-
359
- Log.e(TAG,"chooseVideoSize");
360
-
361
- for (Size size : choices) {
362
-
363
- if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
364
-
365
- return size;
366
-
367
- }
368
-
369
- }
370
-
371
- Log.e(TAG, "Couldn't find any suitable video size");
372
-
373
- return choices[choices.length - 1];
315
+ private void stopBackgroundThread() {
316
+
317
+ Log.e(TAG,"stopBackgroundThread");
318
+
319
+ mBackgroundThread.quitSafely();
320
+
321
+ try {
322
+
323
+ mBackgroundThread.join();
324
+
325
+ mBackgroundThread = null;
326
+
327
+ mBackgroundHandler = null;
328
+
329
+ } catch (InterruptedException e) {
330
+
331
+ e.printStackTrace();
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
+ @Override
380
+
381
+ public void onDestroy() {
382
+
383
+ Log.e(TAG,"onDestroy");
384
+
385
+ closeCamera();
386
+
387
+ stopBackgroundThread();
374
388
 
375
389
  }
376
390
 
377
391
 
378
392
 
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
- private void closeCamera() {
410
-
411
- Log.e(TAG,"closeCamera()");
412
-
413
- try {
414
-
415
- mCameraOpenCloseLock.acquire();
416
-
417
- //closePreviewSession();
418
-
419
- if (null != mCameraDevice) {
420
-
421
- mCameraDevice.close();
422
-
423
- mCameraDevice = null;
424
-
425
- }
426
-
427
- if (null != mMediaRecorder) {
428
-
429
- mMediaRecorder.release();
430
-
431
- mMediaRecorder = null;
432
-
433
- }
434
-
435
- } catch (InterruptedException e) {
436
-
437
- throw new RuntimeException("Interrupted while trying to lock camera closing.");
438
-
439
- } finally {
440
-
441
- mCameraOpenCloseLock.release();
442
-
443
- }
444
-
445
- }
446
-
447
-
448
-
449
-
450
-
451
-
393
+ public String getVideoFilePathy() {
394
+
395
+ Log.e(TAG, "getVideoFilePahy");
396
+
397
+
398
+
399
+ File filePathy;
400
+
401
+ String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/cameraMovie/";
402
+
403
+ filePathy = new File(path);
404
+
405
+ if (!filePathy.exists()) {
406
+
407
+ boolean result = filePathy.mkdir();
408
+
409
+ if (result) {
410
+
411
+ System.out.println("Success");
412
+
413
+ }
414
+
415
+ }
416
+
417
+ return filePathy.toString() + "/" + System.currentTimeMillis() + ".mp4";
418
+
419
+ }
420
+
421
+
422
+
423
+ //ストップは写真とは違う、これは、startされた10秒後にやる、さっきのとこ
424
+
425
+ private void stopRecordingVideo() {
426
+
427
+ Log.e(TAG,"stopRecordingVideo");
428
+
429
+ mIsRecordingVideo = false;
430
+
431
+ try{
432
+
433
+ mMediaRecorder.stop();
434
+
435
+ mMediaRecorder.reset();
436
+
437
+ }catch(RuntimeException stopException){
438
+
439
+ //例外の例外らしい 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
440
+
441
+ }
442
+
443
+ mNextVideoAbsolutePath = null;
444
+
445
+ //startPreview();
446
+
447
+ }
452
448
 
453
449
 
454
450
 
455
451
  @Override
456
452
 
457
- public void onDestroy() {
458
-
459
- Log.e(TAG,"onDestroy");
460
-
461
- closeCamera();
462
-
463
- stopBackgroundThread();
464
-
465
- }
466
-
467
-
468
-
469
-
470
-
471
- public String getVideoFilePathy() {
472
-
473
- Log.e(TAG, "getVideoFilePahy");
474
-
475
-
476
-
477
- File filePathy;
478
-
479
- String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/cameraMovie/";
480
-
481
- filePathy = new File(path);
482
-
483
- if (!filePathy.exists()) {
484
-
485
- boolean result = filePathy.mkdir();
486
-
487
- if (result) {
488
-
489
- System.out.println("Success");
490
-
491
- }
492
-
493
- }
494
-
495
- return filePathy.toString() + "/" + System.currentTimeMillis() + ".mp4";
496
-
497
- }
498
-
499
-
500
-
501
-
502
-
503
- //ストップは写真とは違う、これは、startされた10秒後にやる、さっきのとこ
504
-
505
- private void stopRecordingVideo() {
506
-
507
- Log.e(TAG,"stopRecordingVideo");
508
-
509
- mIsRecordingVideo = false;
510
-
511
- try{
512
-
513
- mMediaRecorder.stop();
514
-
515
- mMediaRecorder.reset();
516
-
517
- }catch(RuntimeException stopException){
518
-
519
- //例外の例外らしい 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
520
-
521
- }
522
-
523
- mNextVideoAbsolutePath = null;
524
-
525
- //startPreview();
526
-
527
- }
528
-
529
-
530
-
531
-
532
-
533
- @Override
534
-
535
453
  public IBinder onBind(Intent intent) {
536
454
 
537
455
  Log.e(TAG,"onBind");
@@ -544,8 +462,6 @@
544
462
 
545
463
  }
546
464
 
547
-
548
-
549
465
  ```
550
466
 
551
467
  ![イメージ説明](7f4b29d7ec44273bab6f8b00b81c2435.png)