jyotiprakash jyotiprakash - 1 year ago 218
Android Question

Integrating video file in android app as app background

I need to use video as my background. First I placed the video file in drawable folder and called as background of

in main.xml. But while running the app, I saw only a black screen. Then I tried to use
and called it like following:


In my activity file I called it with following code snippet:

public void onCreate(Bundle savedInstanceState) {

VideoView video=(VideoView) findViewById(R.id.video);

But still I am not getting the video file there. My main propose is use to a bubble video as background and putting two bubble buttons on it and gives the user a fill like water view screen. Can anyone help me?

Also the video file I want to use from the res folder. Not from SD card or any outer media folder.

Answer Source

Well my friend, first of all you can't set a background to your VideoView and make it play in the background of your screen.

Please follow my steps and add your effort and you should be there.

Remove your video from drawable folder and add it to raw folder. Please google how to create a raw folder. It is simple though. And put your video file inside it.

First of all, create a SurfaceView in your xml like this.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 

        android:paddingTop="10dip" />

Now, create a class like the one below which can implement SurfaceView,

public class YourMovieActivity extends Activity implements SurfaceHolder.Callback {
    private MediaPlayer mp = null;
  SurfaceView mSurfaceView=null;
    public void onCreate(Bundle savedInstanceState) {

        mp = new MediaPlayer();
        mSurfaceView = (SurfaceView) findViewById(R.id.surface);

Now your class will ask for unimplemented methods to be added. Add those methods by just clicking on "Add unimplemented methods"

Now you will be able to see a auto generated method like this,

public void surfaceCreated(SurfaceHolder holder) {


And inside this method,add the below code,

public void surfaceCreated(SurfaceHolder holder) {

   Uri video = Uri.parse("android.resource://" + getPackageName() + "/" 
      + R.raw.your_raw_file);


    //Get the dimensions of the video
    int videoWidth = mp.getVideoWidth();
    int videoHeight = mp.getVideoHeight();

    //Get the width of the screen
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

    //Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();

    //Set the width of the SurfaceView to the width of the screen
    lp.width = screenWidth;

    //Set the height of the SurfaceView to match the aspect ratio of the video 
    //be sure to cast these as floats otherwise the calculation will likely be 0
    lp.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenWidth);

    //Commit the layout parameters

    //Start video
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download