11

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!

4

3 回答 3

4

我有一个类似的问题,我解决了它的变化:

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

对此(在片段中):

 MediaController mediaController = new MediaController(getActivity());

希望这可以帮助,...

编辑

看看这堂课

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

于 2012-11-29T17:35:32.773 回答
1

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.

于 2012-12-01T16:06:21.947 回答
0

MediaController mediaController = new MediaController(getActivity())从移动onResume()onCreateView

于 2016-04-16T07:28:46.547 回答