我想在我的App的主要活动中实施背景视频。最初,我尝试使用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 /? I / art:不晚启用 -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 /系统:ClassLoader 引用的未知路径:/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)
答案 0 :(得分:0)
尝试一下:
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) {
}
}