Reputation: 3529
In my activity, i have 2 VideoViews. i have to play different video files continuously one after the other.
To reduce the switching time, i planned flip the video views. i.e. one video view will be VISIBLE
, while other is GONE
. While the visible video view is playing, i can initalize (setVideoPath
) the other video view.
But based on Logs, i found that for the videoview with visibility GONE
, after calling setVideoPath
, theOnPreparedListener
is called only when the view becomes VISIBLE
. I.e. after the first videoview completes, i switch the visibility, then i get the OnPreparedListener
for the 2nd videoview.
To confirm my findings, i made both videoviews VISIBLE
and calledsetVideoPath
. In this case, both the OnPreparedListener
are called immediately.
Question:
Is the VideoView preparation (setVideoPath
) dependent on its Visibility?
Is there any way to prepare the videoview in the background (without showing it to user)?
Thanks!
Upvotes: 1
Views: 1305
Reputation: 12367
View has to be visible and laid out before it can be bound to camera application. ( Surface view calbacks shall be processed before you do anything ).
Upvotes: 0
Reputation: 4370
Digging through the source code for VideoView and SurfaceView it looks like most of the work of initializing the Media Player doesn't happen until after the view is visible. I don't see a way around that.
In particular, here's the part of SurfaceView.java that runs when the view becomes visible:
if (visible) {
// other stuff
if (visibleChanged) {
mIsCreating = true;
for (SurfaceHolder.Callback c : callbacks) {
c.surfaceCreated(mSurfaceHolder);
}
}
// other stuff
}
That calls this in VideoView.java:
SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback()
{
// other stuff
public void surfaceCreated(SurfaceHolder holder)
{
mSurfaceHolder = holder;
openVideo();
}
// other stuff
}
And openVideo() is where all the magic happens.
Upvotes: 0
Reputation: 33792
Is the VideoView preparation (setVideoPath) dependent on its Visibility?
Apparently yes. openVideo() requires that there is some visibility
public void setVideoURI(Uri uri) {
mUri = uri;
mStartWhenPrepared = false;
mSeekWhenPrepared = 0;
openVideo();
requestLayout();
invalidate();
}
Is there any way to prepare the videoview in the background (without showing it to user)?
Extend this VideoView
and handle prep yourself. Or just use your own VideoView
Upvotes: 1