在Android应用程序中集成视频文件作为应用程序背景

jyo*_*ash 36 android android-layout

我需要使用视频作为我的背景.首先,我将视频文件放在drawable文件夹中,并LinearLayout在main.xml中作为后台调用.但在运行应用程序时,我只看到了黑屏.然后我尝试使用VideoView并调用它如下:

  <VideoView
    android:id="@+id/video"
    android:layout_width="320px"
    android:layout_height="240px"
    android:layout_gravity="center"
    android:background="@raw/hp"/>
Run Code Online (Sandbox Code Playgroud)

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

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        VideoView video=(VideoView) findViewById(R.id.video);
        video.start();
}
Run Code Online (Sandbox Code Playgroud)

但我仍然没有收到视频文件.我的主要建议是使用气泡视频作为背景,并在其上放置两个气泡按钮,让用户感觉像水景屏幕.谁能帮我?

我想从res文件夹中使用的视频文件.不是来自SD卡或任何外部媒体文件夹.

And*_*lva 40

好吧,我的朋友,首先你不能设置你的背景,VideoView让它在你的屏幕背景下播放.

请按照我的步骤添加您的努力,您应该在那里.

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

首先,SurfaceView在你的xml中创建一个这样的.

<?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>
Run Code Online (Sandbox Code Playgroud)

现在,创建一个类似下面可以实现的类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);
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您的课程将要求添加未实现的方法.只需单击"添加未实现的方法"即可添加这些方法

现在您将能够看到这样的自动生成方法,

@Override
public void surfaceCreated(SurfaceHolder holder) {

}
Run Code Online (Sandbox Code Playgroud)

在此方法中,添加以下代码,

@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();
}
Run Code Online (Sandbox Code Playgroud)

  • 仅黑色背景 (8认同)
  • 这对我不起作用,希望有人能够提供更好的解决方案或有效的方法来集成视频文件. (3认同)
  • 我和@Mansuu有同样的问题.... 我有一个黑色的背景.有人弄明白这个问题吗? (2认同)

lui*_*i23 14

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

}
Run Code Online (Sandbox Code Playgroud)

在xml上使用IntroVideoSurfaceView并将视频放入 raw/intro.mp4


OWA*_*DVL 6

完整版的luigi23修改版避免了一些崩溃.

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
}
Run Code Online (Sandbox Code Playgroud)

主要活动没什么.您可能希望通过添加使其全屏

  <style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

创建一个文件IntroVideoSurfaceView.java

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;

public class IntroVideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

  private MediaPlayer mp;
  private boolean has_started = false;

  @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.slideshow);
    try {
      if (!has_started) {
        has_started = true;
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
      }

      mp.prepare();
      android.view.ViewGroup.LayoutParams lp = getLayoutParams();
      lp.height = getHeight();
      lp.width = getWidth();

      setLayoutParams(lp);
      mp.setDisplay(getHolder());
      mp.setLooping(true);
      mp.start();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  }

  @Override public void surfaceDestroyed(SurfaceHolder holder) {
    mp.stop();
  }
}
Run Code Online (Sandbox Code Playgroud)

在resources/raw中添加"slideshow.mp4"

使用修改activity_main.xml

<?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"
    >

  <com.androidadvance.videobackground.IntroVideoSurfaceView
      android:id="@+id/surface"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />

  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Press Me"
      android:id="@+id/button"
      android:layout_gravity="center_horizontal|bottom"
      android:layout_margin="16dp"
      />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

一些笔记.

添加视频会让你的apk变得庞大,所以你可能想要避免这种情况......有时甚至在高端手机上都会发生未知的崩溃(galaxy s6)保持文件小是必不可少的.

  • 此代码在创建和运行活动时有效.但是当我们进入下一个活动并回到这个时,它会失败并出现错误:android.media.MediaPlayer.prepare上的android.media.MediaPlayer._prepare(Native Method)中的java.lang.IllegalStateException(MediaPlayer.java:1135 ) 在 (2认同)