VideoView black flash before and after playing

前端 未结 14 2212
谎友^
谎友^ 2020-12-09 04:04

I have a VideoView which I want to use to play a movieclip. I use it like this to play it and it works.

VideoView vv = new VideoView(this);
vv.setVideoURI(Ur         


        
14条回答
  •  無奈伤痛
    2020-12-09 04:31

    Another solution that might work for people who are searching for this question:

    In my case, the video was a resource in the app (that is, I knew everything about it and it wasn't going to change) and its last 500 ms were the same frame, so I ended up doing the following:

    mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash));
    mVideoView.start();
    
    findViewById(android.R.id.content).postDelayed(new WaitForSplashScreenToFinish(), mVideoView.getDuration() - 1000);
    

    and the referenced class is:

    private class WaitForSplashScreenToFinish implements Runnable {
        @Override
        public void run() {
            if (mVideoView.isPlaying() && mVideoView.getCurrentPosition() >= mVideoView.getDuration() - 500) {
                mVideoView.pause();
    
                // Now do something else, like changing activity
            } else {
                findViewById(android.R.id.content).postDelayed(this, 100);
            }
        }
    }
    

    Explanation: Immediately after starting the video I create a Runnable and postDelayed it to the root view (android.R.id.content) to the duration of the video, minus the length at which I'm willing to pause the video (and a generous buffer, because postDelayed isn't guaranteed to play exactly after the requested time)

    Then, the runnable checks if the video arrived at its pause-time, if so it pauses it and does whatever else we want it to do. If it doesn't, it runs postDelayed again with itself, for a shortened time (100 ms in my case, but could be shorter)

    Granted, this is far from being an ideal solution, but it might help someone with a specific problem similar to the one that stumbled me for half a day :)

提交回复
热议问题