Home » Android » android – VideoView is not displayed on a Fragment

android – VideoView is not displayed on a Fragment

Posted by: admin May 14, 2020 Leave a comment


I have a problem in running a video in Samsung S3(Android 4.1.1), the issue seems to be because the videoview is on a fragment because if I put it on and activity, it works.
Also I found out that if I turn on the GPU hardware acceleration on, the video works.
I have also a game made by drawing on a SurfaceView and that view doesn’t work as well(only with GPU on)… The rest of the app content is displayed as it supposed to (buttons and other layouts).

I tested the app on Nexus S and on the emulator and it works fine, also on other devices..

Does anyone know what the problem could be?
Thank you!

And here is the code:

public class VideoFragment extends Fragment implements MediaPlayer.OnCompletionListener,
        MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {

    private Video mVideo;
    private VideoView mVideoView;
    // The video position
    private int mPosition;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View fragmentView = inflater.inflate(R.layout.screen_video, container, false);

        mVideoView = (VideoView) fragmentView.findViewById(R.id.VideoView);

        return fragmentView;

    public void onPause() {

        // Pause the video if it is playing
        if (mVideoView.isPlaying()) {

        // Save the current video position
        mPosition = mVideoView.getCurrentPosition();

    public void onResume() {


        // Initialize the media controller
        MediaController mediaController = new MediaController(getActivity());
        // Set-up the video view

        if (mVideoView != null) {
            // Restore the video position

    public void onDestroy() {

        // Clean-up
        if (mVideoView != null) {
            mVideoView = null;

    public void onCompletion(MediaPlayer mediaPlayer) {
        Log.e("VIDEO PLAY", "end video play");

    public void onPrepared(MediaPlayer mediaPlayer) {
        // Start the video view
        Log.e("VIDEO PLAY", "video ready for playback");

    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
        Log.e("VIDEO PLAY", "error: " + i);
        return true;


I don’t think it’s something related to context(Application or Activity).. because on all other devices the Video and the games are displayed..
Thanks for the help!

How to&Answers:

If hardware acceleration fixes your issue then I would enable it for that view/window on that device.

In general I’ve found that when code works on one device but not another it is typically caused by one of the following problems:

  1. Bug in the manufacturer’s API implementation
  2. Different interpretation of the API by the manufacturer
  3. Concurrency problem (e.g. race condition, improper synchronization) in your own code that happens to trigger more frequently on a particular device

As far as I can tell you seem to be using the UI thread appropriately so I would imagine your issue falls into one of the first two categories and you’ll just need to work around it.


I have a similar problem, and i solved it changing:

 MediaController mediaController = new MediaController(getActivity().getApplicationContext());

to this (In a fragmet):

 MediaController mediaController = new MediaController(getActivity());

Hope this helps,…


Look at this class



Move MediaController mediaController = new MediaController(getActivity()) from onResume() to onCreateView.