在android应用程序中集成视频文件作为应用程序背景 [英] Integrating video file in android app as app background

查看:23
本文介绍了在android应用程序中集成视频文件作为应用程序背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用视频作为背景.首先,我将视频文件放在 drawable 文件夹中,并在 main.xml 中作为 LinearLayout 的背景调用.但是在运行应用程序时,我只看到一个黑屏.然后我尝试使用 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"/>

在我的活动文件中,我使用以下代码段调用它:

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?

还有我想从 res 文件夹中使用的视频文件.不是来自 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.

从 drawable 文件夹中删除您的视频并将其添加到 raw 文件夹.请谷歌如何创建一个原始文件夹.虽然很简单.并将您的视频文件放入其中.

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 中创建一个 SurfaceView.

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>

现在,创建一个如下所示的类,它可以实现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);
        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();
}

这篇关于在android应用程序中集成视频文件作为应用程序背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆