且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

集成的视频文件在Android应用程序的应用程序背景

更新时间:2023-10-25 17:58:16

好了我的朋友,首先你不能设置一个背景,你videoview并使其在屏幕的后台播放。

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.

首先,创建你的XML这样的表面观。

First of all, create a surface view 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>

现在,创建一个类像一个低于它可以实现SurfaceView,

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);
        //...
    }
}

现在添加类会要求未实现的方法。添加这些方法只需点击添加未实现的方法

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) {

}

和这个方法中,添加以下code,

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.start();
}