Lendl Leyba
Lendl Leyba

Reputation: 2287

Android VideoView SplashScreen

This is my splash.java, and I am using my videoView as the contentView. Is there any way that I can center the video inside the videoView which is the contentView?

Or what is the better thing to do?

package com.android;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.VideoView;

public class Splash extends Activity
{
    VideoView vidHolder;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        try
        {
            vidHolder = new VideoView(this);
            setContentView(vidHolder);
            Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash);
            vidHolder.setVideoURI(video);
            vidHolder.setOnCompletionListener(new OnCompletionListener()
            {
                public void onCompletion(MediaPlayer mp) {
                    jump();
            }});
            vidHolder.start();

        } catch(Exception ex) {
            jump();
        }
    }

    private void jump()
    {
        if(isFinishing())
            return;
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
}

Upvotes: 5

Views: 5225

Answers (1)

Guillaume Cottenceau
Guillaume Cottenceau

Reputation: 389

Use a layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
  <VideoView android:id="@+id/myvideo"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true"/>
</RelativeLayout>

Notice: you didn't ask for it, but your code is from an example seen elsewhere that will feature the infamous black flash before and after the video. You need to add vidHolder.setZOrderOnTop(true); to avoid that (after #setVideoUri).

Upvotes: 6

Related Questions