Search code examples
androidandroid-fragmentsandroid-canvashardware-acceleration

VideoView is not displayed on a Fragment


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;

    @Override
    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;
    }


    @Override
    public void onPause() {
        super.onPause();

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

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

    @Override
    public void onResume() {
        super.onResume();

        mVideoView.setOnCompletionListener(this);
        mVideoView.setOnPreparedListener(this);
        mVideoView.setOnErrorListener(this);
        mVideoView.setKeepScreenOn(true);

        // Initialize the media controller
        MediaController mediaController = new MediaController(getActivity());
        mediaController.setMediaPlayer(mVideoView);
        // Set-up the video view
        mVideoView.setMediaController(mediaController);
        mVideoView.requestFocus();
        mVideoView.setVideoPath(mVideo.getUrl());

        if (mVideoView != null) {
            // Restore the video position
            mVideoView.seekTo(mPosition);
            mVideoView.requestFocus();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

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

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

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

    @Override
    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!


Solution

  • 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.