try using videoview - it is possible that the device you are using to display the video, does not support the format in mediaplayer object, i solved this problem with videoview
Using surfaceview and fragments to display video. Video doesn't show but I have sound,
-
01-06-2022 - |
Question
I have seen several posts with the same issue, but I still can't fix my issue. I am using surfaceview, fragments and mediaplayer to play a mp4 video. I get audio, but no video. The video does play with no issues outside of the application. I have tried different formats, but no luck. I am not using an emulator. I am using a Samsung Galaxy Tab2 to test. What am I missing to show video?
Here is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/video_surfaceView"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</SurfaceView>
<Button
android:id="@+id/playvideoplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PLAY "/>
<Button
android:id="@+id/pausevideoplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PAUSE"/>
<Button
android:id="@+id/stopvideoplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- STOP"/>
</LinearLayout>
Here is my code:
public class ChapterVideoFragment extends Fragment {
private static final String TAG = "ChapterVideoFragment";
private static final boolean VERBOSE = true;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private MediaPlayer mp = null;
private Button mPlayVideo,
mPauseVideo,
mStopVideo;
private boolean mPausing = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreateView +++");
View v = inflater.inflate(R.layout.fragment_video, parent, false);
mSurfaceView = (SurfaceView)v.findViewById(R.id.video_surfaceView);
mPlayVideo = (Button)v.findViewById(R.id.playvideoplayer);
mPauseVideo = (Button)v.findViewById(R.id.pausevideoplayer);
mStopVideo = (Button)v.findViewById(R.id.stopvideoplayer);
mp = MediaPlayer.create(getActivity(), R.raw.improvisation);
mPlayVideo.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
//Get the dimensions of the video
int videoWidth = mp.getVideoWidth();
int videoHeight = mp.getVideoHeight();
//Get the width of the screen
int screenWidth =
getActivity().getWindowManager().getDefaultDisplay().getWidth();
//Get the SurfaceView layout parameters
android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();
//Set the width of the SurfaceView to the width of the screen
lp.width = screenWidth;
//Set the height of the SurfaceView to match the aspect ratio of the video
//be sure to cast these as floats otherwise the calculation will likely be
0
lp.height = (int) (((float)videoHeight / (float)videoWidth) *
(float)screenWidth);
//Commit the layout parameters
mSurfaceView.setLayoutParams(lp);
//mp.setDisplay(mSurfaceView.getHolder());
mp.start();
}
});
Here is the code for SurfaceView:
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
if (VERBOSE) Log.v(TAG, "+++ surfaceCreated +++");
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
});
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow