Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.37 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Integrating video file in android app as app background
  2. <VideoView
  3.  android:id="@+id/video"
  4.  android:layout_width="320px"
  5.     android:layout_height="240px"
  6.     android:layout_gravity="center"
  7.     android:background="@raw/hp"
  8.    />
  9.        
  10. public void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.  
  13.         setContentView(R.layout.main);
  14.         VideoView video=(VideoView) findViewById(R.id.video);
  15.         video.start();
  16. }
  17.        
  18. <?xml version="1.0" encoding="utf-8"?>
  19. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  20.             android:id="@+id/home_container"  
  21.             android:layout_width="fill_parent"
  22.             android:layout_height="fill_parent">
  23.  
  24. <SurfaceView
  25.         android:id="@+id/surface"
  26.         android:layout_width="fill_parent"
  27.         android:layout_height="wrap_content"
  28.         android:paddingTop="10dip" />
  29. </framelayout>
  30.        
  31. public class YourMovieActivity extends Activity implements SurfaceHolder.Callback {
  32.     private MediaPlayer mp = null;
  33.     //...
  34.   SurfaceView mSurfaceView=null;
  35.     @Override
  36.     public void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.  
  39.         mp = new MediaPlayer();
  40.         mSurfaceView = (SurfaceView) findViewById(R.id.surface);
  41.         //...
  42.     }
  43. }
  44.        
  45. @Override
  46. public void surfaceCreated(SurfaceHolder holder) {
  47.  
  48. }
  49.        
  50. @Override
  51. public void surfaceCreated(SurfaceHolder holder) {
  52.  
  53.  
  54.    Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
  55.       + R.raw.your_raw_file);
  56.  
  57.     mp.setDataSource(video);
  58.     mp.prepare();
  59.  
  60.     //Get the dimensions of the video
  61.     int videoWidth = mp.getVideoHeight();
  62.     int videoHeight = mp.getVideoWidth();
  63.  
  64.     //Get the width of the screen
  65.     int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
  66.  
  67.     //Get the SurfaceView layout parameters
  68.     android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();
  69.  
  70.     //Set the width of the SurfaceView to the width of the screen
  71.     lp.width = screenWidth;
  72.  
  73.     //Set the height of the SurfaceView to match the aspect ratio of the video
  74.     //be sure to cast these as floats otherwise the calculation will likely be 0
  75.     lp.height = (int) (((float)videoHeight.height / (float)videoWidth.width) * (float)screenWidth);
  76.  
  77.     //Commit the layout parameters
  78.     mSurfaceView.setLayoutParams(lp);        
  79.  
  80.     //Start video
  81.     mp.start();
  82. }