Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 395
Cata Avatar asked Nov 21 '12 08:11

Cata


People also ask

How to show DialogFragment in fragment in android?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

How to add fragment in activity XML?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

What is the use of VideoView in Android?

Displays a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

How to work with fragments in android studio?

We can use Android Studio support in the Design view for the MainActivity layout file to select a fragment from inside the Custom choices. Open the activity_main layout file in design view and, inside the Palete, click Fragment under the “Custom” section. You will be prompted to select a fragment.


2 Answers

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

EDIT

Look at this class

http://code.google.com/p/sinaweibo-honeycomb/source/browse/branches/sinaweibo-merged/src/com/lenovo/dll/SinaWeibo/VideoFragment.java?r=71

like image 119
jzafrilla Avatar answered Nov 08 '22 20:11

jzafrilla


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.

like image 34
CgodLEY Avatar answered Nov 08 '22 20:11

CgodLEY