TheFlash
TheFlash

Reputation: 6027

VideoView flickering issue

I'm facing weird issue of flickering using VideoView. When activity starts, it causes minor flicker of fraction for a second. Then, video starts. It shows 2 black lines on the top and the bottom of the video. See the snap-shot below.

check snap shot

I have tested my application on 2 devices

1) Samsung n-8000(Tablet)

2) Lenovo a-800

video.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#ffffff"
 android:gravity="center">

 <VideoView 
    android:id="@+id/vvSplash"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="#00ffffff">
</VideoView>

</LinearLayout>

Activity code:

private VideoView vd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.video);
    vd = (VideoView) findViewById(R.id.vvSplash);
    playVideo();
}

private void playVideo() {
    Uri uri = Uri.parse("android.resource://" + getPackageName() +"/"+ R.raw.intro);
    vd.setVideoURI(uri);
    vd.setMediaController(null);
    vd.start();     
}

Any help would be appreciated. Thank you.

Upvotes: 9

Views: 9152

Answers (7)

Louis CAD
Louis CAD

Reputation: 11529

If your issue is VideoView flickering when the back button is pressed, just set your VideoView visibility to INVISIBLE or GONE in the onBackPressed() method of your Activity before calling the super implementation.

If the user can also leave through the "up" button, intercept android.R.home option item to hide the VideoView.

Here's an example in Kotlin:

override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
    android.R.id.home -> super.onOptionsItemSelected(item).also {
        video_view.visibility = GONE // Hide videoView before leaving to avoid flickering
    }
    R.id.action_share -> consume { shareVideo() } // Your regular menu options
    else -> super.onOptionsItemSelected(item)
}

override fun onBackPressed() {
    video_view.visibility = GONE // Hide videoView before leaving to avoid flickering
    super.onBackPressed()
}

Upvotes: 1

noamtm
noamtm

Reputation: 12953

Adapted the xml-based solution from https://stackoverflow.com/a/27307286/38557 to use Java code instead. Put this in the Fragment's onCreateView(), before inflating the layout:

// Add a SurfaceView to prevent flickering when the video is loaded later.
SurfaceView surfaceView = new SurfaceView(getActivity());
surfaceView.setVisibility(View.GONE);
container.addView(surfaceView, new ViewGroup.LayoutParams(0, 0));

It does the same thing, and can be used when you can't or don't want to change the XMLs.

Upvotes: 0

Haytham Osama
Haytham Osama

Reputation: 69

For anyone who's still facing the problem and don't get how to use the above answer. It simply just copies this code and paste it in the main activity layout that you're using the videoview in under the main (first) layout.

<SurfaceView
    android:layout_width="0px"
    android:layout_height="0px"
    android:visibility="gone" />

in my case

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Mainframlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/facebookbgcolor" >
<SurfaceView
    android:layout_width="0px"
    android:layout_height="0px"
    android:visibility="gone" />
.....rest of the layout

Upvotes: 0

hellaandrew
hellaandrew

Reputation: 833

For people in the future: This ugly bug seems to have been resolved in Marshmallow. At my work, we are developing with Xamarin (so, basically, all views are added in programmatically). We have a bunch of test devices, and I was mainly working with a Marshmallow device, so I never noticed this black flicker when building the page I was working on. After finally testing with a Lollipop device, I noticed this flicker.

Unfortunately I do not have a solution. We need our application to be as cross-platform as possible, so using a layout xml is discouraged /sadface.

Upvotes: 1

Oliver Hausler
Oliver Hausler

Reputation: 4977

As ridiculous as it sounds, but the answer is here:

I repeat the solution, kudos to those who found out:

<SurfaceView
    android:layout_width="0px"
    android:layout_height="0px"
    android:visibility="gone" />

It is VERY IMPORTANT that you add the surface view to the root view in your activity, not the immediate parent. Otherwise it won't work.

Upvotes: 11

Amit Jayaswal
Amit Jayaswal

Reputation: 1723

Just remove the following line from your xml.

android:background="#00ffffff"

It will help you :)

Upvotes: 0

Shakeeb Ayaz
Shakeeb Ayaz

Reputation: 6096

i also faced same problem but solved it by changing layout parameter of videoview from wrap content to match parent . Also you need to remove the background property of video view from XML. I hope it will work for you

Upvotes: 2

Related Questions