質問編集履歴

1

コードの追加

2021/02/18 07:15

投稿

ToaZI
ToaZI

スコア39

test CHANGED
File without changes
test CHANGED
@@ -25,3 +25,509 @@
25
25
 
26
26
 
27
27
  ご教示いただければ幸いです。
28
+
29
+ ```MainActivity.java
30
+
31
+ public class MainActivity extends AppCompatActivity {
32
+
33
+
34
+
35
+ //RYUKA ADD
36
+
37
+ //利用するパーミッション分設定
38
+
39
+ private final String[] REQUIRED_PERMISSIONS =
40
+
41
+ new String[]{"android.permission.CAMERA",
42
+
43
+ "android.permission.WRITE_EXTERNAL_STORAGE",
44
+
45
+ "android.permission.RECORD_AUDIO"};
46
+
47
+ private int REQUEST_CODE_PERMISSIONS = 10;
48
+
49
+
50
+
51
+ @Override
52
+
53
+ protected void onCreate(Bundle savedInstanceState) {
54
+
55
+ super.onCreate(savedInstanceState);
56
+
57
+ setContentView(R.layout.activity_main);
58
+
59
+ BottomNavigationView navView = findViewById(R.id.nav_view);
60
+
61
+ // Passing each menu ID as a set of Ids because each
62
+
63
+ // menu should be considered as top level destinations.
64
+
65
+ AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
66
+
67
+ R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
68
+
69
+ .build();
70
+
71
+ NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
72
+
73
+ NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
74
+
75
+ NavigationUI.setupWithNavController(navView, navController);
76
+
77
+ //homefragment表示
78
+
79
+ addFragment(new HomeFragment());
80
+
81
+
82
+
83
+ }
84
+
85
+
86
+
87
+ private void addFragment(Fragment fragment) {
88
+
89
+ FragmentManager manager = getSupportFragmentManager();
90
+
91
+ FragmentTransaction transaction = manager.beginTransaction();
92
+
93
+ transaction.add(R.id.nav_host_fragment, fragment);
94
+
95
+ transaction.commit();
96
+
97
+ }
98
+
99
+
100
+
101
+
102
+
103
+ }
104
+
105
+ ```
106
+
107
+ ```HomeFragment.java
108
+
109
+ public class HomeFragment extends Fragment {
110
+
111
+
112
+
113
+ private Button st_button;
114
+
115
+ int go = 0;
116
+
117
+ Random rand = new Random();
118
+
119
+ int SetTime = 7000;
120
+
121
+ SoundPool soundPool; // 効果音を鳴らす本体(コンポ)
122
+
123
+ int mp1; // 効果音データ(mp3)
124
+
125
+ int mp2; // 効果音データ(mp3)
126
+
127
+ int mp3; // 効果音データ(mp3)
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
140
+
141
+
142
+
143
+ View view = inflater.inflate(R.layout.fragment_home, container, false);
144
+
145
+
146
+
147
+ return view;
148
+
149
+ }
150
+
151
+
152
+
153
+
154
+
155
+ public void onViewCreated(final View view, Bundle savedInstanceState) {
156
+
157
+ super.onViewCreated(view, savedInstanceState);
158
+
159
+ st_button = view.findViewById(R.id.button);
160
+
161
+ st_button.setOnClickListener(new View.OnClickListener() {
162
+
163
+ @Override
164
+
165
+ public void onClick(View view) {
166
+
167
+
168
+
169
+ replaceFragment(new DashboardFragment());
170
+
171
+ String str = String.valueOf(num);
172
+
173
+ //ここにVideoFragmentのStartrecoadingを入れたい
174
+
175
+ }
176
+
177
+ });
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+ private void replaceFragment(Fragment fragment) {
186
+
187
+ // フラグメントマネージャーの取得
188
+
189
+ FragmentManager manager = getFragmentManager();
190
+
191
+ FragmentTransaction transaction = manager.beginTransaction();
192
+
193
+ transaction.replace(R.id.container, fragment);
194
+
195
+ transaction.addToBackStack(null);
196
+
197
+ transaction.commit();
198
+
199
+ }
200
+
201
+
202
+
203
+
204
+
205
+ }
206
+
207
+ ```
208
+
209
+ ```DashboredFragment.java
210
+
211
+ public class DashboardFragment extends Fragment {
212
+
213
+
214
+
215
+ //RYUKA ADD
216
+
217
+ private VideoFragment camera2;
218
+
219
+
220
+
221
+ public View onCreateView(@NonNull LayoutInflater inflater,
222
+
223
+ ViewGroup container, Bundle savedInstanceState) {
224
+
225
+
226
+
227
+ View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+ //カメラフラグメントの生成&当フラグメントの子フラグメントとして起動
236
+
237
+ if (null == savedInstanceState) {
238
+
239
+ //カメラフラグメント生成
240
+
241
+ camera2 = VideoFragment.newInstance();
242
+
243
+ FragmentManager fragMan = getFragmentManager();
244
+
245
+ FragmentTransaction fragTransaction = fragMan.beginTransaction();
246
+
247
+ fragTransaction.add(R.id.container, camera2);
248
+
249
+ fragTransaction.commit();
250
+
251
+ getChildFragmentManager().beginTransaction().add(R.id.container, camera2).commit();
252
+
253
+ }
254
+
255
+ return root;
256
+
257
+ }
258
+
259
+
260
+
261
+
262
+
263
+ }
264
+
265
+ ```
266
+
267
+ ```VideoFragment.java
268
+
269
+ public class VideoFragment extends Fragment {
270
+
271
+
272
+
273
+ rivate TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
274
+
275
+ @Override
276
+
277
+ public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
278
+
279
+ int width, int height) {
280
+
281
+ openCamera(width, height);
282
+
283
+ }
284
+
285
+ @Override
286
+
287
+ public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
288
+
289
+ int width, int height) {
290
+
291
+ configureTransform(width, height);
292
+
293
+ }
294
+
295
+ @Override
296
+
297
+ public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
298
+
299
+ return true;
300
+
301
+ }
302
+
303
+ @Override
304
+
305
+ public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
306
+
307
+ }
308
+
309
+ };
310
+
311
+ private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
312
+
313
+ @Override
314
+
315
+ public void onOpened(@NonNull CameraDevice cameraDevice) {
316
+
317
+ mCameraDevice = cameraDevice;
318
+
319
+ startPreview();
320
+
321
+ mCameraOpenCloseLock.release();
322
+
323
+ if (null != mTextureView) {
324
+
325
+ configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
326
+
327
+ }
328
+
329
+ }
330
+
331
+
332
+
333
+ @Override
334
+
335
+ public void onDisconnected(@NonNull CameraDevice cameraDevice) {
336
+
337
+ mCameraOpenCloseLock.release();
338
+
339
+ cameraDevice.close();
340
+
341
+ mCameraDevice = null;
342
+
343
+ }
344
+
345
+
346
+
347
+ @Override
348
+
349
+ public void onError(@NonNull CameraDevice cameraDevice, int error) {
350
+
351
+ mCameraOpenCloseLock.release();
352
+
353
+ cameraDevice.close();
354
+
355
+ mCameraDevice = null;
356
+
357
+ Activity activity = getActivity();
358
+
359
+ if (null != activity) {
360
+
361
+ activity.finish();
362
+
363
+ }
364
+
365
+ }
366
+
367
+
368
+
369
+ };
370
+
371
+ public static VideoFragment newInstance() {
372
+
373
+ return new VideoFragment();
374
+
375
+ }
376
+
377
+ 撮影動画のアスペクト比を指定(3*4)
378
+
379
+ プレビューサイズの設定
380
+
381
+ View生成時処理
382
+
383
+ View生成完了時処理
384
+
385
+ 復帰時処理
386
+
387
+ 終了時処理
388
+
389
+ バックグラウンド処理の開始
390
+
391
+ バックグラウンド処理の終了
392
+
393
+ カメラの起動
394
+
395
+ カメラの終了
396
+
397
+ プレビュー画面の開始
398
+
399
+ プレビュー画面の更新
400
+
401
+ プレビューのサイズを元に動画の回転情報を指定
402
+
403
+ MediaRecorder関連の初期設定処理
404
+
405
+ 保存先情報とファイル名の設定
406
+
407
+ */
408
+
409
+
410
+
411
+
412
+
413
+ public void startRecordingVideo() {
414
+
415
+ if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
416
+
417
+ return;
418
+
419
+ }
420
+
421
+ try {
422
+
423
+ closePreviewSession();
424
+
425
+ setUpMediaRecorder();
426
+
427
+ SurfaceTexture texture = mTextureView.getSurfaceTexture();
428
+
429
+ assert texture != null;
430
+
431
+ texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
432
+
433
+ mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
434
+
435
+ List<Surface> surfaces = new ArrayList<>();
436
+
437
+
438
+
439
+ //プレビュー画面の設定
440
+
441
+ Surface previewSurface = new Surface(texture);
442
+
443
+ surfaces.add(previewSurface);
444
+
445
+ mPreviewBuilder.addTarget(previewSurface);
446
+
447
+
448
+
449
+ //メディアレコーダー(録画処理)のセット
450
+
451
+ Surface recorderSurface = mMediaRecorder.getSurface();
452
+
453
+ surfaces.add(recorderSurface);
454
+
455
+ mPreviewBuilder.addTarget(recorderSurface);
456
+
457
+
458
+
459
+ //セッションの開始
460
+
461
+ mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
462
+
463
+
464
+
465
+ @Override
466
+
467
+ public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
468
+
469
+ mPreviewSession = cameraCaptureSession;
470
+
471
+ updatePreview();
472
+
473
+ getActivity().runOnUiThread(new Runnable() {
474
+
475
+ @Override
476
+
477
+ public void run() {
478
+
479
+ //録画開始
480
+
481
+ mMediaRecorder.start();
482
+
483
+ }
484
+
485
+ });
486
+
487
+ }
488
+
489
+
490
+
491
+ @Override
492
+
493
+ public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
494
+
495
+ Activity activity = getActivity();
496
+
497
+ if (null != activity) {
498
+
499
+ Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
500
+
501
+ }
502
+
503
+ }
504
+
505
+ }, mBackgroundHandler);
506
+
507
+ } catch (CameraAccessException | IOException e) {
508
+
509
+ e.printStackTrace();
510
+
511
+ }
512
+
513
+
514
+
515
+ }
516
+
517
+
518
+
519
+ private void closePreviewSession() {
520
+
521
+ if (mPreviewSession != null) {
522
+
523
+ mPreviewSession.close();
524
+
525
+ mPreviewSession = null;
526
+
527
+ }
528
+
529
+ }
530
+
531
+ }
532
+
533
+ ```