Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 LinearLayout in main.xml. But while running the app, I saw only a black screen. Then I tried to use VideoView and called it like following:

  <VideoView     android:id="@+id/video"     android:layout_width="320px"     android:layout_height="240px"     android:layout_gravity="center"     android:background="@raw/hp"/> 

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

  public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          setContentView(R.layout.main);         VideoView video=(VideoView) findViewById(R.id.video);         video.start(); } 

But still I am not getting the video file there. My main propose is to use a bubble video as background and putting two bubble buttons on it and gives the user a feel 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.

like image 264
jyotiprakash Avatar asked Jan 12 '12 04:01

jyotiprakash


People also ask

How do I play a downloaded video in the background?

Just follow the steps given. Open Settings by tapping on the three-line menu on the top left of the screen. Scroll down and look for the background play option and check the option, and that is it. Now you will be able to run videos and audio in the background.

Which app can play video in the background?

Highlights of the Story For Android devices, you can play YouTube videos in the background via Google Chrome or picture in picture mode. For Apple smartphones, streaming videos with YouTube minimized can be done with the help of browsers like Opera and Dolphin.

How do I make videos play in the background on my Samsung?

All you have to do is go for any YouTube, play it, and go to Home. The video will stop playing and to play it in the background you just need to pull down the notification shade (in Android) or the Control Centre (in iOS) and resume playing the video. Now, the video will easily play in the background.


2 Answers

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:id="@+id/home_container"               android:layout_width="fill_parent"              android:layout_height="fill_parent">  <SurfaceView          android:id="@+id/surface"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:paddingTop="10dip" /> </Framelayout> 

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;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          mp = new MediaPlayer();         mSurfaceView = (SurfaceView) findViewById(R.id.surface);         mSurfaceView.getHolder().addCallback(this);         //...     } } 

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,

@Override public void surfaceCreated(SurfaceHolder holder) {  } 

And inside this method,add the below code,

@Override public void surfaceCreated(SurfaceHolder holder) {      Uri video = Uri.parse("android.resource://" + getPackageName() + "/"        + R.raw.your_raw_file);      mp.setDataSource(video);     mp.prepare();      //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     mSurfaceView.setLayoutParams(lp);              //Start video     mp.setDisplay(holder);     mp.start(); } 
like image 180
Andro Selva Avatar answered Sep 19 '22 01:09

Andro Selva


/**  * Created by zoid23 on 05/10/15.  */ public class IntroVideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback {      private static final String TAG = "INTRO_SF_VIDEO_CALLBACK";     private MediaPlayer mp;      @TargetApi(Build.VERSION_CODES.LOLLIPOP)     public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {         super(context, attrs, defStyleAttr, defStyleRes);         init();     }      public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {         super(context, attrs, defStyleAttr);         init();     }     public IntroVideoSurfaceView(Context context, AttributeSet attrs) {         super(context, attrs);         init();     }     public IntroVideoSurfaceView(Context context) {         super(context);         init();     }      private void init (){         mp = new MediaPlayer();         getHolder().addCallback(this);     }      @Override     public void surfaceCreated(SurfaceHolder holder) {         AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.intro);         try {             mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());             mp.prepare();         } catch (IOException e) {             e.printStackTrace();         }         int videoWidth = mp.getVideoWidth();         int videoHeight = mp.getVideoHeight();         int screenHeight = getHeight();         android.view.ViewGroup.LayoutParams lp = getLayoutParams();         lp.height = screenHeight;         lp.width = (int) (((float)videoWidth / (float)videoHeight) * (float)screenHeight);          setLayoutParams(lp);         mp.setDisplay(getHolder());         mp.setLooping(true);         mp.start();     }      @Override     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {     }      @Override     public void surfaceDestroyed(SurfaceHolder holder) {         mp.stop();     }  } 

Use IntroVideoSurfaceView on your xml and put your video in raw/intro.mp4

like image 27
luigi23 Avatar answered Sep 23 '22 01:09

luigi23