GymExercises应用Fragment中视频无法显示问题求助
Hey there! Let's tackle this video display issue in your GymExercises app's Fragment. Since you mentioned the code compiles but nothing shows up, let's go through the most common fixes and best practices for embedding a video without playback controls.
First, let's cover the key areas that usually cause this issue, depending on whether you're using VideoView or ExoPlayer (the two most common options for Android video playback).
1. If You're Using VideoView
Check Layout Configuration in fragment_bench.xml
Make sure your VideoView is properly sized and placed in the layout—if it's set to wrap_content with no video loaded yet, it might collapse to zero size. Try setting explicit dimensions or using match_parent with constraints:
<VideoView android:id="@+id/bench_video" android:layout_width="match_parent" android:layout_height="250dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" />
Verify Video Path/URI in BenchFragment.java
Ensure you're pointing to a valid video resource—either raw resource, local file, or remote URL. For raw resources:
VideoView videoView = view.findViewById(R.id.bench_video); Uri videoUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/" + R.raw.bench_press); videoView.setVideoURI(videoUri); // Auto-play since you don't want controls videoView.start();
Also, make sure the video file is properly placed in res/raw/ (note: don't include file extensions like .mp4 in the resource name).
Check Permissions (for Remote Videos)
If your video is hosted online, add internet permission to AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
2. If You're Using ExoPlayer (Recommended for Better Compatibility)
ExoPlayer is often more reliable than VideoView, especially for modern video formats. Here's a quick setup to display video without controls:
Add Dependency to build.gradle (Module level)
implementation "com.google.android.exoplayer:exoplayer-core:2.18.1" implementation "com.google.android.exoplayer:exoplayer-ui:2.18.1"
Update fragment_bench.xml
Use PlayerView but hide the controls:
<com.google.android.exoplayer2.ui.PlayerView android:id="@+id/bench_player_view" android:layout_width="match_parent" android:layout_height="250dp" app:show_buffering="when_playing" app:use_controller="false" /> <!-- This hides all playback controls -->
Initialize ExoPlayer in BenchFragment.java
private PlayerView playerView; private SimpleExoPlayer player; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_bench, container, false); playerView = view.findViewById(R.id.bench_player_view); return view; } @Override public void onStart() { super.onStart(); if (Util.SDK_INT >= 24) { initializePlayer(); } } @Override public void onResume() { super.onResume(); if ((Util.SDK_INT < 24 || player == null)) { initializePlayer(); } } @Override public void onPause() { super.onPause(); if (Util.SDK_INT < 24) { releasePlayer(); } } @Override public void onStop() { super.onStop(); if (Util.SDK_INT >= 24) { releasePlayer(); } } private void initializePlayer() { player = new SimpleExoPlayer.Builder(requireContext()).build(); playerView.setPlayer(player); // Load video from raw resource MediaItem mediaItem = MediaItem.fromUri("android.resource://" + requireContext().getPackageName() + "/" + R.raw.bench_press); player.setMediaItem(mediaItem); player.prepare(); player.play(); // Auto-start playback } private void releasePlayer() { if (player != null) { player.release(); player = null; } }
Common Pitfalls to Avoid
- Video File Format: Ensure your video is in a format supported by Android (MP4 with H.264/H.265 codec is safest).
- Fragment Lifecycle: If you're using VideoView, make sure you're managing playback correctly across lifecycle events (pause/resume) to avoid memory leaks or playback issues.
- Layout Visibility: Double-check that the video view isn't hidden by other views (e.g.,
android:visibility="gone"by mistake) or covered by a sibling view with higher elevation.
If none of these fix the issue, sharing your actual BenchFragment.java and fragment_bench.xml code would help narrow down the problem further!
内容的提问来源于stack exchange,提问作者user9313301

