Soggy_Toast Soggy_Toast - 3 years ago 72
Android Question

Android: Programmatically created VideoView is showing black screen

So I have a

VideoView
that I have made completely programmatically like so:

loadingVideo = new VideoView(getContext());
String videoPath = "android.resource://" + splashActivity.getPackageName() + "/" + R.raw.loading_video;
loadingVideo.setVideoURI(Uri.parse(videoPath));
loadingVideo.setLayoutParams(new FrameLayout.LayoutParams(Constants.PIXEL_WIDTH, Constants.PIXEL_HEIGHT));
loadingVideo.setTranslationX(0);
loadingVideo.setTranslationY(0);
loadingVideo.start();
this.widgetContainer.addView(loadingVideo);


I know the video is doing something because I later have an
OnCompletionListener
that does go off, the problem is that I don't ever actually see anything and just get a black screen. I draw the method here:

if(loadingVideo.isPlaying()) {
loadingVideo.draw(canvas);
System.out.println();
}


and the
draw(Canvas canvas)
method is refreshed at a rate of 60fps using this:

Canvas canvas = null;
SurfaceHolder sh = getHolder();
boolean unlocked = false;
try {
if(sh.getSurface().isValid()) {
canvas = sh.lockCanvas();
synchronized(sh) {
draw(canvas);
}
}
} catch(Exception e) {
if(canvas != null && sh.getSurface().isValid()) {
sh.unlockCanvasAndPost(canvas);
unlocked = true;
}
e.printStackTrace();
} finally {
if(!unlocked && canvas != null && sh.getSurface().isValid()) {
sh.unlockCanvasAndPost(canvas);
}
}


I don't understand why it isn't drawing or what I'm doing wrong.

Side note: I know that the way I'm doing this seems odd but I know it works because I've created other views this way in other projects and they've worked fine. I just can't get the video to show anything other than a black screen.

Answer Source
loadingVideo.setZOrderOnTop(true); 

before the

loadingVideo.start();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download