Reputation: 28349
I'm using a fullscreen VideoView
and playing <1mb video files sitting in the raw folder.
For some reason, no matter what I do (and I've tried every kludge I can think of) before the video plays the player goes black for about 1/4 of a second.
I've tried setting the VideoView
's visibility to hidden and using the onPreparedListener
callback to not expose it till that comes back. I've tried just putting a general Timer on it to give it as much as a second before setting its visibility back to visible, I've tried seek()
ing to its 1 second position, I've even tried loading the video onCreate
(it's not needed till later in the app) so that it would be loaded and ready to go when the user gets to it, but no matter what I do, it INSISTS on sitting on flashing that blackness before it plays.
Would love some guidance on what I can do to have the VideoView
not show up till its really ready to start playing.
I'm happy to post code, but it's pretty generic VideoView
playback code. I'm not doing anything unusual.
Upvotes: 0
Views: 963
Reputation: 28349
So, the solution turned out to be that I was loading video into a VideoView whose visibility was not set to View.VISIBLE.
At least at the time of this writing (Android v.4.3) the VideoView itself MUST be visible in order to start the loading process. However, it's perfectly happy to be the child of a wrapper Layout
whose visibility is set to View.INVISIBLE (haven't tried View.GONE, but suspect that will be fine as well).
So... the key is to have an invisible wrapper Layout, set an OnPrepared listener on the VideoView, and when that fires, expose the wrapper layout. In my case I noticed that there was still a flicker, so I added another 400ms of delay with a Handler and a Runnable before exposing the parent Layout and it works perfectly.
<!-- THIS VIEW SHOULD BE INVISIBLE SO THAT WE CAN PRE-LOAD THE VIDEO WITHOUT THE BLACK SCREEN -->
<RelativeLayout
android:id="@+id/vidContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="230dp"
android:layout_marginBottom="200dp"
android:visibility="invisible"
>
<!-- THIS VIEW MUST BE VISIBLE IN ORDER TO LOAD VIDEO!!!!!! -->
<VideoView
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
and then in android
private void playVideo(int vidid) {
//vid id is whatever the video resource is, as in R.raw.myVideo
Uri vid = Uri.parse("android.resource://" + getPackageName() + "/"
+ vidid);
winnerVidPlayer = (VideoView) findViewById(R.id.videoView);
//Let the preparer fire and when it does, wait a little longer to avoid ANY flicker before exposing
//the wrapper Layout
winnerVidPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
new Handler().postDelayed(new Runnable(){
public void run(){
((RelativeLayout)findViewById(R.id.vidContainer)).setVisibility(View.VISIBLE);
}
},400);
}
});
winnerVidPlayer.setVideoURI(vid);
winnerVidPlayer.start();
}
Upvotes: 4