別フラグメント内にあるメソッドをフラグメントから使用したいのですが、
使い方がわかりません。
こちらのサイトを参考に作成してみたのですが
フラグメントからフラグメントの場合の記述方法がわかりません。
別フラグメント
↓
メインアクティビティ
↓
フラグメント(別メソッドを組み込みたい)
という流れでメソッドが動くという感じであっていますか?
ご教示いただければ幸いです。
MainActivity.java
1public class MainActivity extends AppCompatActivity { 2 3 //RYUKA ADD 4 //利用するパーミッション分設定 5 private final String[] REQUIRED_PERMISSIONS = 6 new String[]{"android.permission.CAMERA", 7 "android.permission.WRITE_EXTERNAL_STORAGE", 8 "android.permission.RECORD_AUDIO"}; 9 private int REQUEST_CODE_PERMISSIONS = 10; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 BottomNavigationView navView = findViewById(R.id.nav_view); 16 // Passing each menu ID as a set of Ids because each 17 // menu should be considered as top level destinations. 18 AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( 19 R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications) 20 .build(); 21 NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); 22 NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); 23 NavigationUI.setupWithNavController(navView, navController); 24 //homefragment表示 25 addFragment(new HomeFragment()); 26 27 } 28 29 private void addFragment(Fragment fragment) { 30 FragmentManager manager = getSupportFragmentManager(); 31 FragmentTransaction transaction = manager.beginTransaction(); 32 transaction.add(R.id.nav_host_fragment, fragment); 33 transaction.commit(); 34 } 35 36 37}
HomeFragment.java
1public class HomeFragment extends Fragment { 2 3 private Button st_button; 4 int go = 0; 5 Random rand = new Random(); 6 int SetTime = 7000; 7 SoundPool soundPool; // 効果音を鳴らす本体(コンポ) 8 int mp1; // 効果音データ(mp3) 9 int mp2; // 効果音データ(mp3) 10 int mp3; // 効果音データ(mp3) 11 12 13 14 15 16 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 17 18 View view = inflater.inflate(R.layout.fragment_home, container, false); 19 20 return view; 21 } 22 23 24 public void onViewCreated(final View view, Bundle savedInstanceState) { 25 super.onViewCreated(view, savedInstanceState); 26 st_button = view.findViewById(R.id.button); 27 st_button.setOnClickListener(new View.OnClickListener() { 28 @Override 29 public void onClick(View view) { 30 31 replaceFragment(new DashboardFragment()); 32 String str = String.valueOf(num); 33 //ここにVideoFragmentのStartrecoadingを入れたい 34 } 35 }); 36 37 38 39 private void replaceFragment(Fragment fragment) { 40 // フラグメントマネージャーの取得 41 FragmentManager manager = getFragmentManager(); 42 FragmentTransaction transaction = manager.beginTransaction(); 43 transaction.replace(R.id.container, fragment); 44 transaction.addToBackStack(null); 45 transaction.commit(); 46 } 47 48 49}
DashboredFragment.java
1public class DashboardFragment extends Fragment { 2 3 //RYUKA ADD 4 private VideoFragment camera2; 5 6 public View onCreateView(@NonNull LayoutInflater inflater, 7 ViewGroup container, Bundle savedInstanceState) { 8 9 View root = inflater.inflate(R.layout.fragment_dashboard, container, false); 10 11 12 13 //カメラフラグメントの生成&当フラグメントの子フラグメントとして起動 14 if (null == savedInstanceState) { 15 //カメラフラグメント生成 16 camera2 = VideoFragment.newInstance(); 17 FragmentManager fragMan = getFragmentManager(); 18 FragmentTransaction fragTransaction = fragMan.beginTransaction(); 19 fragTransaction.add(R.id.container, camera2); 20 fragTransaction.commit(); 21 getChildFragmentManager().beginTransaction().add(R.id.container, camera2).commit(); 22 } 23 return root; 24 } 25 26 27}
VideoFragment.java
1public class VideoFragment extends Fragment { 2 3rivate TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() { 4 @Override 5 public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, 6 int width, int height) { 7 openCamera(width, height); 8 } 9 @Override 10 public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, 11 int width, int height) { 12 configureTransform(width, height); 13 } 14 @Override 15 public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { 16 return true; 17 } 18 @Override 19 public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { 20 } 21 }; 22 private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() { 23 @Override 24 public void onOpened(@NonNull CameraDevice cameraDevice) { 25 mCameraDevice = cameraDevice; 26 startPreview(); 27 mCameraOpenCloseLock.release(); 28 if (null != mTextureView) { 29 configureTransform(mTextureView.getWidth(), mTextureView.getHeight()); 30 } 31 } 32 33 @Override 34 public void onDisconnected(@NonNull CameraDevice cameraDevice) { 35 mCameraOpenCloseLock.release(); 36 cameraDevice.close(); 37 mCameraDevice = null; 38 } 39 40 @Override 41 public void onError(@NonNull CameraDevice cameraDevice, int error) { 42 mCameraOpenCloseLock.release(); 43 cameraDevice.close(); 44 mCameraDevice = null; 45 Activity activity = getActivity(); 46 if (null != activity) { 47 activity.finish(); 48 } 49 } 50 51 }; 52 public static VideoFragment newInstance() { 53 return new VideoFragment(); 54 } 55撮影動画のアスペクト比を指定(3*4) 56プレビューサイズの設定 57View生成時処理 58View生成完了時処理 59復帰時処理 60終了時処理 61バックグラウンド処理の開始 62バックグラウンド処理の終了 63カメラの起動 64カメラの終了 65プレビュー画面の開始 66プレビュー画面の更新 67プレビューのサイズを元に動画の回転情報を指定 68MediaRecorder関連の初期設定処理 69保存先情報とファイル名の設定 70 */ 71 72 73 public void startRecordingVideo() { 74 if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) { 75 return; 76 } 77 try { 78 closePreviewSession(); 79 setUpMediaRecorder(); 80 SurfaceTexture texture = mTextureView.getSurfaceTexture(); 81 assert texture != null; 82 texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); 83 mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD); 84 List<Surface> surfaces = new ArrayList<>(); 85 86 //プレビュー画面の設定 87 Surface previewSurface = new Surface(texture); 88 surfaces.add(previewSurface); 89 mPreviewBuilder.addTarget(previewSurface); 90 91 //メディアレコーダー(録画処理)のセット 92 Surface recorderSurface = mMediaRecorder.getSurface(); 93 surfaces.add(recorderSurface); 94 mPreviewBuilder.addTarget(recorderSurface); 95 96 //セッションの開始 97 mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() { 98 99 @Override 100 public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { 101 mPreviewSession = cameraCaptureSession; 102 updatePreview(); 103 getActivity().runOnUiThread(new Runnable() { 104 @Override 105 public void run() { 106 //録画開始 107 mMediaRecorder.start(); 108 } 109 }); 110 } 111 112 @Override 113 public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { 114 Activity activity = getActivity(); 115 if (null != activity) { 116 Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show(); 117 } 118 } 119 }, mBackgroundHandler); 120 } catch (CameraAccessException | IOException e) { 121 e.printStackTrace(); 122 } 123 124 } 125 126 private void closePreviewSession() { 127 if (mPreviewSession != null) { 128 mPreviewSession.close(); 129 mPreviewSession = null; 130 } 131 } 132}
回答1件
あなたの回答
tips
プレビュー