Integrating video file in android app as app background

前端 未结 6 1426
感情败类
感情败类 2020-11-30 19:36

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 t

6条回答
  •  再見小時候
    2020-11-30 20:25

    Full version of the modified version of luigi23 with avoiding some crashes.

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

    nothing on the main activity. you might want to make it fullscreen by adding

      
    

    Create a file 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();
      }
    }
    

    add a "slideshow.mp4" in resources/raw

    modify the activity_main.xml with

    
    
    
      
    
      

    Some notes.

    Adding a video will make your apk huge so you might want to avoid this... From time to time unknown crashes occur even on high end phones (galaxy s6) It's essential to keep the file small.

提交回复
热议问题