質問編集履歴
1
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,4 +11,257 @@
|
|
11
11
|
|
12
12
|
という流れでメソッドが動くという感じであっていますか?
|
13
13
|
|
14
|
-
ご教示いただければ幸いです。
|
14
|
+
ご教示いただければ幸いです。
|
15
|
+
```MainActivity.java
|
16
|
+
public class MainActivity extends AppCompatActivity {
|
17
|
+
|
18
|
+
//RYUKA ADD
|
19
|
+
//利用するパーミッション分設定
|
20
|
+
private final String[] REQUIRED_PERMISSIONS =
|
21
|
+
new String[]{"android.permission.CAMERA",
|
22
|
+
"android.permission.WRITE_EXTERNAL_STORAGE",
|
23
|
+
"android.permission.RECORD_AUDIO"};
|
24
|
+
private int REQUEST_CODE_PERMISSIONS = 10;
|
25
|
+
|
26
|
+
@Override
|
27
|
+
protected void onCreate(Bundle savedInstanceState) {
|
28
|
+
super.onCreate(savedInstanceState);
|
29
|
+
setContentView(R.layout.activity_main);
|
30
|
+
BottomNavigationView navView = findViewById(R.id.nav_view);
|
31
|
+
// Passing each menu ID as a set of Ids because each
|
32
|
+
// menu should be considered as top level destinations.
|
33
|
+
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
|
34
|
+
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
|
35
|
+
.build();
|
36
|
+
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
|
37
|
+
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
|
38
|
+
NavigationUI.setupWithNavController(navView, navController);
|
39
|
+
//homefragment表示
|
40
|
+
addFragment(new HomeFragment());
|
41
|
+
|
42
|
+
}
|
43
|
+
|
44
|
+
private void addFragment(Fragment fragment) {
|
45
|
+
FragmentManager manager = getSupportFragmentManager();
|
46
|
+
FragmentTransaction transaction = manager.beginTransaction();
|
47
|
+
transaction.add(R.id.nav_host_fragment, fragment);
|
48
|
+
transaction.commit();
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
}
|
53
|
+
```
|
54
|
+
```HomeFragment.java
|
55
|
+
public class HomeFragment extends Fragment {
|
56
|
+
|
57
|
+
private Button st_button;
|
58
|
+
int go = 0;
|
59
|
+
Random rand = new Random();
|
60
|
+
int SetTime = 7000;
|
61
|
+
SoundPool soundPool; // 効果音を鳴らす本体(コンポ)
|
62
|
+
int mp1; // 効果音データ(mp3)
|
63
|
+
int mp2; // 効果音データ(mp3)
|
64
|
+
int mp3; // 効果音データ(mp3)
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
71
|
+
|
72
|
+
View view = inflater.inflate(R.layout.fragment_home, container, false);
|
73
|
+
|
74
|
+
return view;
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
public void onViewCreated(final View view, Bundle savedInstanceState) {
|
79
|
+
super.onViewCreated(view, savedInstanceState);
|
80
|
+
st_button = view.findViewById(R.id.button);
|
81
|
+
st_button.setOnClickListener(new View.OnClickListener() {
|
82
|
+
@Override
|
83
|
+
public void onClick(View view) {
|
84
|
+
|
85
|
+
replaceFragment(new DashboardFragment());
|
86
|
+
String str = String.valueOf(num);
|
87
|
+
//ここにVideoFragmentのStartrecoadingを入れたい
|
88
|
+
}
|
89
|
+
});
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
private void replaceFragment(Fragment fragment) {
|
94
|
+
// フラグメントマネージャーの取得
|
95
|
+
FragmentManager manager = getFragmentManager();
|
96
|
+
FragmentTransaction transaction = manager.beginTransaction();
|
97
|
+
transaction.replace(R.id.container, fragment);
|
98
|
+
transaction.addToBackStack(null);
|
99
|
+
transaction.commit();
|
100
|
+
}
|
101
|
+
|
102
|
+
|
103
|
+
}
|
104
|
+
```
|
105
|
+
```DashboredFragment.java
|
106
|
+
public class DashboardFragment extends Fragment {
|
107
|
+
|
108
|
+
//RYUKA ADD
|
109
|
+
private VideoFragment camera2;
|
110
|
+
|
111
|
+
public View onCreateView(@NonNull LayoutInflater inflater,
|
112
|
+
ViewGroup container, Bundle savedInstanceState) {
|
113
|
+
|
114
|
+
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
//カメラフラグメントの生成&当フラグメントの子フラグメントとして起動
|
119
|
+
if (null == savedInstanceState) {
|
120
|
+
//カメラフラグメント生成
|
121
|
+
camera2 = VideoFragment.newInstance();
|
122
|
+
FragmentManager fragMan = getFragmentManager();
|
123
|
+
FragmentTransaction fragTransaction = fragMan.beginTransaction();
|
124
|
+
fragTransaction.add(R.id.container, camera2);
|
125
|
+
fragTransaction.commit();
|
126
|
+
getChildFragmentManager().beginTransaction().add(R.id.container, camera2).commit();
|
127
|
+
}
|
128
|
+
return root;
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
}
|
133
|
+
```
|
134
|
+
```VideoFragment.java
|
135
|
+
public class VideoFragment extends Fragment {
|
136
|
+
|
137
|
+
rivate TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
|
138
|
+
@Override
|
139
|
+
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
|
140
|
+
int width, int height) {
|
141
|
+
openCamera(width, height);
|
142
|
+
}
|
143
|
+
@Override
|
144
|
+
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
|
145
|
+
int width, int height) {
|
146
|
+
configureTransform(width, height);
|
147
|
+
}
|
148
|
+
@Override
|
149
|
+
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
|
150
|
+
return true;
|
151
|
+
}
|
152
|
+
@Override
|
153
|
+
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
|
154
|
+
}
|
155
|
+
};
|
156
|
+
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
|
157
|
+
@Override
|
158
|
+
public void onOpened(@NonNull CameraDevice cameraDevice) {
|
159
|
+
mCameraDevice = cameraDevice;
|
160
|
+
startPreview();
|
161
|
+
mCameraOpenCloseLock.release();
|
162
|
+
if (null != mTextureView) {
|
163
|
+
configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
@Override
|
168
|
+
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
|
169
|
+
mCameraOpenCloseLock.release();
|
170
|
+
cameraDevice.close();
|
171
|
+
mCameraDevice = null;
|
172
|
+
}
|
173
|
+
|
174
|
+
@Override
|
175
|
+
public void onError(@NonNull CameraDevice cameraDevice, int error) {
|
176
|
+
mCameraOpenCloseLock.release();
|
177
|
+
cameraDevice.close();
|
178
|
+
mCameraDevice = null;
|
179
|
+
Activity activity = getActivity();
|
180
|
+
if (null != activity) {
|
181
|
+
activity.finish();
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
};
|
186
|
+
public static VideoFragment newInstance() {
|
187
|
+
return new VideoFragment();
|
188
|
+
}
|
189
|
+
撮影動画のアスペクト比を指定(3*4)
|
190
|
+
プレビューサイズの設定
|
191
|
+
View生成時処理
|
192
|
+
View生成完了時処理
|
193
|
+
復帰時処理
|
194
|
+
終了時処理
|
195
|
+
バックグラウンド処理の開始
|
196
|
+
バックグラウンド処理の終了
|
197
|
+
カメラの起動
|
198
|
+
カメラの終了
|
199
|
+
プレビュー画面の開始
|
200
|
+
プレビュー画面の更新
|
201
|
+
プレビューのサイズを元に動画の回転情報を指定
|
202
|
+
MediaRecorder関連の初期設定処理
|
203
|
+
保存先情報とファイル名の設定
|
204
|
+
*/
|
205
|
+
|
206
|
+
|
207
|
+
public void startRecordingVideo() {
|
208
|
+
if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
|
209
|
+
return;
|
210
|
+
}
|
211
|
+
try {
|
212
|
+
closePreviewSession();
|
213
|
+
setUpMediaRecorder();
|
214
|
+
SurfaceTexture texture = mTextureView.getSurfaceTexture();
|
215
|
+
assert texture != null;
|
216
|
+
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
|
217
|
+
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
|
218
|
+
List<Surface> surfaces = new ArrayList<>();
|
219
|
+
|
220
|
+
//プレビュー画面の設定
|
221
|
+
Surface previewSurface = new Surface(texture);
|
222
|
+
surfaces.add(previewSurface);
|
223
|
+
mPreviewBuilder.addTarget(previewSurface);
|
224
|
+
|
225
|
+
//メディアレコーダー(録画処理)のセット
|
226
|
+
Surface recorderSurface = mMediaRecorder.getSurface();
|
227
|
+
surfaces.add(recorderSurface);
|
228
|
+
mPreviewBuilder.addTarget(recorderSurface);
|
229
|
+
|
230
|
+
//セッションの開始
|
231
|
+
mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
|
232
|
+
|
233
|
+
@Override
|
234
|
+
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
|
235
|
+
mPreviewSession = cameraCaptureSession;
|
236
|
+
updatePreview();
|
237
|
+
getActivity().runOnUiThread(new Runnable() {
|
238
|
+
@Override
|
239
|
+
public void run() {
|
240
|
+
//録画開始
|
241
|
+
mMediaRecorder.start();
|
242
|
+
}
|
243
|
+
});
|
244
|
+
}
|
245
|
+
|
246
|
+
@Override
|
247
|
+
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
|
248
|
+
Activity activity = getActivity();
|
249
|
+
if (null != activity) {
|
250
|
+
Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
|
251
|
+
}
|
252
|
+
}
|
253
|
+
}, mBackgroundHandler);
|
254
|
+
} catch (CameraAccessException | IOException e) {
|
255
|
+
e.printStackTrace();
|
256
|
+
}
|
257
|
+
|
258
|
+
}
|
259
|
+
|
260
|
+
private void closePreviewSession() {
|
261
|
+
if (mPreviewSession != null) {
|
262
|
+
mPreviewSession.close();
|
263
|
+
mPreviewSession = null;
|
264
|
+
}
|
265
|
+
}
|
266
|
+
}
|
267
|
+
```
|