【问题标题】:How to run a background video in Android activity?如何在 Android 活动中运行背景视频?
【发布时间】:2019-10-27 03:00:40
【问题描述】:

我想在我的应用程序的主要活动中实现背景视频。最初,我尝试使用 VideoView 将此视频添加为普通视频,但没有设置使其覆盖整个屏幕,因此我尝试使用表面视图添加相同的视频,但出现了一些问题。应用一启动就停止。

以下是我的代码:-

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

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dip" />
</FrameLayout>

MainActivity.java

public class MainActivity extends Activity implements SurfaceHolder.Callback {

    private MediaPlayer mp = null;

    SurfaceView mSurfaceView=null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        mp = new MediaPlayer();
        mSurfaceView = (SurfaceView) findViewById(R.id.surface);
        mSurfaceView.getHolder().addCallback(this);


        mSurfaceView.getHolder().addCallback(this);





    @Override
    public void surfaceCreated(SurfaceHolder holder) {


        Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
                + R.raw.introduction);

        try {
            mp.setDataSource(String.valueOf(video));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mp.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

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

        mp.setDisplay(holder);



    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

    }
}

以下是相同的日志:-

2019-06-12 18:38:16.128 4261-4261/?我/艺术:不迟到 -Xcheck:jni (已经开启) 2019-06-12 18:38:16.129 4261-4261/? W/art:使用默认值的 X86 的意外 CPU 变体:x86 2019-06-12 18:38:16.504 4261-4261/com.example.liveinbliss W/系统:类加载器 引用的未知路径:/data/app/com.example.liveinbliss-1/lib/x86 2019-06-12 18:38:16.517 4261-4261/com.example.liveinbliss I/InstantRun:启动即时运行服务器:是主进程 2019-06-12 18:38:16.647 4261-4261/com.example.liveinbliss D/AndroidRuntime: 关闭虚拟机

--------- beginning of crash 2019-06-12 18:38:16.647 4261-4261/com.example.liveinbliss E/AndroidRuntime: FATAL EXCEPTION:

主要 进程:com.example.liveinbliss,PID:4261 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.liveinbliss/com.example.liveinbliss.MainActivity}: java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 引起:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上 在 com.example.liveinbliss.MainActivity.onCreate(MainActivity.java:48) 在 android.app.Activity.performCreate(Activity.java:6679) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)

    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)

    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

【问题讨论】:

    标签: android android-mediaplayer surfaceview android-videoview


    【解决方案1】:

    试试这个:

    public class MainActivity extends Activity implements SurfaceHolder.Callback {
    
        private MediaPlayer mp = null;
    
        SurfaceView mSurfaceView=null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mp = MediaPlayer.create(this, R.raw.perf); 
            mSurfaceView = (SurfaceView) findViewById(R.raw.introduction);
            mSurfaceView.getHolder().addCallback(this);
    
    
            mSurfaceView.getHolder().addCallback(this);
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
    
    
            Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
                    + R.raw.introduction);
    
            try {
                mp.setDataSource(String.valueOf(video));
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mp.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //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();
    
            mp.setDisplay(holder);
    
    
    
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多