集成的视频文件在Android应用程序的应用程序背景 [英] Integrating video file in android app as app background
问题描述
我需要使用视频作为我的背景。首先我把视频文件中绘制文件夹,并呼吁为的LinearLayout的main.xml中的背景。但在运行的应用程序,我只看到一个黑色的屏幕。然后我试图用VideoView,并要求它像以下内容:
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"
/>
在我的活动文件中我有以下code段称它:
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 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?
另外我想从资源文件夹中使用的视频文件。不从SD卡或任何外部媒体的文件夹。
Also the video file I want to use from the res folder. Not from SD card or any outer media folder.
推荐答案
好了我的朋友,首先你不能设置一个背景,你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();
}
这篇关于集成的视频文件在Android应用程序的应用程序背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!